python-tests.el 169 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304
  1. ;;; python-tests.el --- Test suite for python.el
  2. ;; Copyright (C) 2013-2017 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is free software: you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; GNU Emacs is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;;; Code:
  16. (require 'ert)
  17. (require 'python)
  18. ;; Dependencies for testing:
  19. (require 'electric)
  20. (require 'hideshow)
  21. (require 'tramp-sh)
  22. (defmacro python-tests-with-temp-buffer (contents &rest body)
  23. "Create a `python-mode' enabled temp buffer with CONTENTS.
  24. BODY is code to be executed within the temp buffer. Point is
  25. always located at the beginning of buffer."
  26. (declare (indent 1) (debug t))
  27. `(with-temp-buffer
  28. (let ((python-indent-guess-indent-offset nil))
  29. (python-mode)
  30. (insert ,contents)
  31. (goto-char (point-min))
  32. ,@body)))
  33. (defmacro python-tests-with-temp-file (contents &rest body)
  34. "Create a `python-mode' enabled file with CONTENTS.
  35. BODY is code to be executed within the temp buffer. Point is
  36. always located at the beginning of buffer."
  37. (declare (indent 1) (debug t))
  38. ;; temp-file never actually used for anything?
  39. `(let* ((temp-file (make-temp-file "python-tests" nil ".py"))
  40. (buffer (find-file-noselect temp-file))
  41. (python-indent-guess-indent-offset nil))
  42. (unwind-protect
  43. (with-current-buffer buffer
  44. (python-mode)
  45. (insert ,contents)
  46. (goto-char (point-min))
  47. ,@body)
  48. (and buffer (kill-buffer buffer))
  49. (delete-file temp-file))))
  50. (defun python-tests-look-at (string &optional num restore-point)
  51. "Move point at beginning of STRING in the current buffer.
  52. Optional argument NUM defaults to 1 and is an integer indicating
  53. how many occurrences must be found, when positive the search is
  54. done forwards, otherwise backwards. When RESTORE-POINT is
  55. non-nil the point is not moved but the position found is still
  56. returned. When searching forward and point is already looking at
  57. STRING, it is skipped so the next STRING occurrence is selected."
  58. (let* ((num (or num 1))
  59. (starting-point (point))
  60. (string (regexp-quote string))
  61. (search-fn (if (> num 0) #'re-search-forward #'re-search-backward))
  62. (deinc-fn (if (> num 0) #'1- #'1+))
  63. (found-point))
  64. (prog2
  65. (catch 'exit
  66. (while (not (= num 0))
  67. (when (and (> num 0)
  68. (looking-at string))
  69. ;; Moving forward and already looking at STRING, skip it.
  70. (forward-char (length (match-string-no-properties 0))))
  71. (and (not (funcall search-fn string nil t))
  72. (throw 'exit t))
  73. (when (> num 0)
  74. ;; `re-search-forward' leaves point at the end of the
  75. ;; occurrence, move back so point is at the beginning
  76. ;; instead.
  77. (forward-char (- (length (match-string-no-properties 0)))))
  78. (setq
  79. num (funcall deinc-fn num)
  80. found-point (point))))
  81. found-point
  82. (and restore-point (goto-char starting-point)))))
  83. (defun python-tests-self-insert (char-or-str)
  84. "Call `self-insert-command' for chars in CHAR-OR-STR."
  85. (let ((chars
  86. (cond
  87. ((characterp char-or-str)
  88. (list char-or-str))
  89. ((stringp char-or-str)
  90. (string-to-list char-or-str))
  91. ((not
  92. (cl-remove-if #'characterp char-or-str))
  93. char-or-str)
  94. (t (error "CHAR-OR-STR must be a char, string, or list of char")))))
  95. (mapc
  96. (lambda (char)
  97. (let ((last-command-event char))
  98. (call-interactively 'self-insert-command)))
  99. chars)))
  100. (defun python-tests-visible-string (&optional min max)
  101. "Return the buffer string excluding invisible overlays.
  102. Argument MIN and MAX delimit the region to be returned and
  103. default to `point-min' and `point-max' respectively."
  104. (let* ((min (or min (point-min)))
  105. (max (or max (point-max)))
  106. (buffer (current-buffer))
  107. (buffer-contents (buffer-substring-no-properties min max))
  108. (overlays
  109. (sort (overlays-in min max)
  110. (lambda (a b)
  111. (let ((overlay-end-a (overlay-end a))
  112. (overlay-end-b (overlay-end b)))
  113. (> overlay-end-a overlay-end-b))))))
  114. (with-temp-buffer
  115. (insert buffer-contents)
  116. (dolist (overlay overlays)
  117. (if (overlay-get overlay 'invisible)
  118. (delete-region (overlay-start overlay)
  119. (overlay-end overlay))))
  120. (buffer-substring-no-properties (point-min) (point-max)))))
  121. ;;; Tests for your tests, so you can test while you test.
  122. (ert-deftest python-tests-look-at-1 ()
  123. "Test forward movement."
  124. (python-tests-with-temp-buffer
  125. "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
  126. sed do eiusmod tempor incididunt ut labore et dolore magna
  127. aliqua."
  128. (let ((expected (save-excursion
  129. (dotimes (i 3)
  130. (re-search-forward "et" nil t))
  131. (forward-char -2)
  132. (point))))
  133. (should (= (python-tests-look-at "et" 3 t) expected))
  134. ;; Even if NUM is bigger than found occurrences the point of last
  135. ;; one should be returned.
  136. (should (= (python-tests-look-at "et" 6 t) expected))
  137. ;; If already looking at STRING, it should skip it.
  138. (dotimes (i 2) (re-search-forward "et"))
  139. (forward-char -2)
  140. (should (= (python-tests-look-at "et") expected)))))
  141. (ert-deftest python-tests-look-at-2 ()
  142. "Test backward movement."
  143. (python-tests-with-temp-buffer
  144. "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
  145. sed do eiusmod tempor incididunt ut labore et dolore magna
  146. aliqua."
  147. (let ((expected
  148. (save-excursion
  149. (re-search-forward "et" nil t)
  150. (forward-char -2)
  151. (point))))
  152. (dotimes (i 3)
  153. (re-search-forward "et" nil t))
  154. (should (= (python-tests-look-at "et" -3 t) expected))
  155. (should (= (python-tests-look-at "et" -6 t) expected)))))
  156. ;;; Bindings
  157. ;;; Python specialized rx
  158. ;;; Font-lock and syntax
  159. (ert-deftest python-syntax-after-python-backspace ()
  160. ;; `python-indent-dedent-line-backspace' garbles syntax
  161. :expected-result :failed
  162. (python-tests-with-temp-buffer
  163. "\"\"\""
  164. (goto-char (point-max))
  165. (python-indent-dedent-line-backspace 1)
  166. (should (string= (buffer-string) "\"\""))
  167. (should (null (nth 3 (syntax-ppss))))))
  168. ;;; Indentation
  169. ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
  170. (ert-deftest python-indent-pep8-1 ()
  171. "First pep8 case."
  172. (python-tests-with-temp-buffer
  173. "# Aligned with opening delimiter
  174. foo = long_function_name(var_one, var_two,
  175. var_three, var_four)
  176. "
  177. (should (eq (car (python-indent-context)) :no-indent))
  178. (should (= (python-indent-calculate-indentation) 0))
  179. (python-tests-look-at "foo = long_function_name(var_one, var_two,")
  180. (should (eq (car (python-indent-context)) :after-comment))
  181. (should (= (python-indent-calculate-indentation) 0))
  182. (python-tests-look-at "var_three, var_four)")
  183. (should (eq (car (python-indent-context)) :inside-paren))
  184. (should (= (python-indent-calculate-indentation) 25))))
  185. (ert-deftest python-indent-pep8-2 ()
  186. "Second pep8 case."
  187. (python-tests-with-temp-buffer
  188. "# More indentation included to distinguish this from the rest.
  189. def long_function_name(
  190. var_one, var_two, var_three,
  191. var_four):
  192. print (var_one)
  193. "
  194. (should (eq (car (python-indent-context)) :no-indent))
  195. (should (= (python-indent-calculate-indentation) 0))
  196. (python-tests-look-at "def long_function_name(")
  197. (should (eq (car (python-indent-context)) :after-comment))
  198. (should (= (python-indent-calculate-indentation) 0))
  199. (python-tests-look-at "var_one, var_two, var_three,")
  200. (should (eq (car (python-indent-context))
  201. :inside-paren-newline-start-from-block))
  202. (should (= (python-indent-calculate-indentation) 8))
  203. (python-tests-look-at "var_four):")
  204. (should (eq (car (python-indent-context))
  205. :inside-paren-newline-start-from-block))
  206. (should (= (python-indent-calculate-indentation) 8))
  207. (python-tests-look-at "print (var_one)")
  208. (should (eq (car (python-indent-context))
  209. :after-block-start))
  210. (should (= (python-indent-calculate-indentation) 4))))
  211. (ert-deftest python-indent-pep8-3 ()
  212. "Third pep8 case."
  213. (python-tests-with-temp-buffer
  214. "# Extra indentation is not necessary.
  215. foo = long_function_name(
  216. var_one, var_two,
  217. var_three, var_four)
  218. "
  219. (should (eq (car (python-indent-context)) :no-indent))
  220. (should (= (python-indent-calculate-indentation) 0))
  221. (python-tests-look-at "foo = long_function_name(")
  222. (should (eq (car (python-indent-context)) :after-comment))
  223. (should (= (python-indent-calculate-indentation) 0))
  224. (python-tests-look-at "var_one, var_two,")
  225. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  226. (should (= (python-indent-calculate-indentation) 4))
  227. (python-tests-look-at "var_three, var_four)")
  228. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  229. (should (= (python-indent-calculate-indentation) 4))))
  230. (ert-deftest python-indent-base-case ()
  231. "Check base case does not trigger errors."
  232. (python-tests-with-temp-buffer
  233. "
  234. "
  235. (goto-char (point-min))
  236. (should (eq (car (python-indent-context)) :no-indent))
  237. (should (= (python-indent-calculate-indentation) 0))
  238. (forward-line 1)
  239. (should (eq (car (python-indent-context)) :no-indent))
  240. (should (= (python-indent-calculate-indentation) 0))
  241. (forward-line 1)
  242. (should (eq (car (python-indent-context)) :no-indent))
  243. (should (= (python-indent-calculate-indentation) 0))))
  244. (ert-deftest python-indent-after-comment-1 ()
  245. "The most simple after-comment case that shouldn't fail."
  246. (python-tests-with-temp-buffer
  247. "# Contents will be modified to correct indentation
  248. class Blag(object):
  249. def _on_child_complete(self, child_future):
  250. if self.in_terminal_state():
  251. pass
  252. # We only complete when all our async children have entered a
  253. # terminal state. At that point, if any child failed, we fail
  254. # with the exception with which the first child failed.
  255. "
  256. (python-tests-look-at "# We only complete")
  257. (should (eq (car (python-indent-context)) :after-block-end))
  258. (should (= (python-indent-calculate-indentation) 8))
  259. (python-tests-look-at "# terminal state")
  260. (should (eq (car (python-indent-context)) :after-comment))
  261. (should (= (python-indent-calculate-indentation) 8))
  262. (python-tests-look-at "# with the exception")
  263. (should (eq (car (python-indent-context)) :after-comment))
  264. ;; This one indents relative to previous block, even given the fact
  265. ;; that it was under-indented.
  266. (should (= (python-indent-calculate-indentation) 4))
  267. (python-tests-look-at "# terminal state" -1)
  268. ;; It doesn't hurt to check again.
  269. (should (eq (car (python-indent-context)) :after-comment))
  270. (python-indent-line)
  271. (should (= (current-indentation) 8))
  272. (python-tests-look-at "# with the exception")
  273. (should (eq (car (python-indent-context)) :after-comment))
  274. ;; Now everything should be lined up.
  275. (should (= (python-indent-calculate-indentation) 8))))
  276. (ert-deftest python-indent-after-comment-2 ()
  277. "Test after-comment in weird cases."
  278. (python-tests-with-temp-buffer
  279. "# Contents will be modified to correct indentation
  280. def func(arg):
  281. # I don't do much
  282. return arg
  283. # This comment is badly indented because the user forced so.
  284. # At this line python.el wont dedent, user is always right.
  285. comment_wins_over_ender = True
  286. # yeah, that.
  287. "
  288. (python-tests-look-at "# I don't do much")
  289. (should (eq (car (python-indent-context)) :after-block-start))
  290. (should (= (python-indent-calculate-indentation) 4))
  291. (python-tests-look-at "return arg")
  292. ;; Comment here just gets ignored, this line is not a comment so
  293. ;; the rules won't apply here.
  294. (should (eq (car (python-indent-context)) :after-block-start))
  295. (should (= (python-indent-calculate-indentation) 4))
  296. (python-tests-look-at "# This comment is badly indented")
  297. (should (eq (car (python-indent-context)) :after-block-end))
  298. ;; The return keyword do make indentation lose a level...
  299. (should (= (python-indent-calculate-indentation) 0))
  300. ;; ...but the current indentation was forced by the user.
  301. (python-tests-look-at "# At this line python.el wont dedent")
  302. (should (eq (car (python-indent-context)) :after-comment))
  303. (should (= (python-indent-calculate-indentation) 4))
  304. ;; Should behave the same for blank lines: potentially a comment.
  305. (forward-line 1)
  306. (should (eq (car (python-indent-context)) :after-comment))
  307. (should (= (python-indent-calculate-indentation) 4))
  308. (python-tests-look-at "comment_wins_over_ender")
  309. ;; The comment won over the ender because the user said so.
  310. (should (eq (car (python-indent-context)) :after-comment))
  311. (should (= (python-indent-calculate-indentation) 4))
  312. ;; The indentation calculated fine for the assignment, but the user
  313. ;; choose to force it back to the first column. Next line should
  314. ;; be aware of that.
  315. (python-tests-look-at "# yeah, that.")
  316. (should (eq (car (python-indent-context)) :after-line))
  317. (should (= (python-indent-calculate-indentation) 0))))
  318. (ert-deftest python-indent-after-comment-3 ()
  319. "Test after-comment in buggy case."
  320. (python-tests-with-temp-buffer
  321. "
  322. class A(object):
  323. def something(self, arg):
  324. if True:
  325. return arg
  326. # A comment
  327. @adecorator
  328. def method(self, a, b):
  329. pass
  330. "
  331. (python-tests-look-at "@adecorator")
  332. (should (eq (car (python-indent-context)) :after-comment))
  333. (should (= (python-indent-calculate-indentation) 4))))
  334. (ert-deftest python-indent-inside-paren-1 ()
  335. "The most simple inside-paren case that shouldn't fail."
  336. (python-tests-with-temp-buffer
  337. "
  338. data = {
  339. 'key':
  340. {
  341. 'objlist': [
  342. {
  343. 'pk': 1,
  344. 'name': 'first',
  345. },
  346. {
  347. 'pk': 2,
  348. 'name': 'second',
  349. }
  350. ]
  351. }
  352. }
  353. "
  354. (python-tests-look-at "data = {")
  355. (should (eq (car (python-indent-context)) :no-indent))
  356. (should (= (python-indent-calculate-indentation) 0))
  357. (python-tests-look-at "'key':")
  358. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  359. (should (= (python-indent-calculate-indentation) 4))
  360. (python-tests-look-at "{")
  361. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  362. (should (= (python-indent-calculate-indentation) 4))
  363. (python-tests-look-at "'objlist': [")
  364. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  365. (should (= (python-indent-calculate-indentation) 8))
  366. (python-tests-look-at "{")
  367. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  368. (should (= (python-indent-calculate-indentation) 12))
  369. (python-tests-look-at "'pk': 1,")
  370. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  371. (should (= (python-indent-calculate-indentation) 16))
  372. (python-tests-look-at "'name': 'first',")
  373. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  374. (should (= (python-indent-calculate-indentation) 16))
  375. (python-tests-look-at "},")
  376. (should (eq (car (python-indent-context))
  377. :inside-paren-at-closing-nested-paren))
  378. (should (= (python-indent-calculate-indentation) 12))
  379. (python-tests-look-at "{")
  380. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  381. (should (= (python-indent-calculate-indentation) 12))
  382. (python-tests-look-at "'pk': 2,")
  383. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  384. (should (= (python-indent-calculate-indentation) 16))
  385. (python-tests-look-at "'name': 'second',")
  386. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  387. (should (= (python-indent-calculate-indentation) 16))
  388. (python-tests-look-at "}")
  389. (should (eq (car (python-indent-context))
  390. :inside-paren-at-closing-nested-paren))
  391. (should (= (python-indent-calculate-indentation) 12))
  392. (python-tests-look-at "]")
  393. (should (eq (car (python-indent-context))
  394. :inside-paren-at-closing-nested-paren))
  395. (should (= (python-indent-calculate-indentation) 8))
  396. (python-tests-look-at "}")
  397. (should (eq (car (python-indent-context))
  398. :inside-paren-at-closing-nested-paren))
  399. (should (= (python-indent-calculate-indentation) 4))
  400. (python-tests-look-at "}")
  401. (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
  402. (should (= (python-indent-calculate-indentation) 0))))
  403. (ert-deftest python-indent-inside-paren-2 ()
  404. "Another more compact paren group style."
  405. (python-tests-with-temp-buffer
  406. "
  407. data = {'key': {
  408. 'objlist': [
  409. {'pk': 1,
  410. 'name': 'first'},
  411. {'pk': 2,
  412. 'name': 'second'}
  413. ]
  414. }}
  415. "
  416. (python-tests-look-at "data = {")
  417. (should (eq (car (python-indent-context)) :no-indent))
  418. (should (= (python-indent-calculate-indentation) 0))
  419. (python-tests-look-at "'objlist': [")
  420. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  421. (should (= (python-indent-calculate-indentation) 4))
  422. (python-tests-look-at "{'pk': 1,")
  423. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  424. (should (= (python-indent-calculate-indentation) 8))
  425. (python-tests-look-at "'name': 'first'},")
  426. (should (eq (car (python-indent-context)) :inside-paren))
  427. (should (= (python-indent-calculate-indentation) 9))
  428. (python-tests-look-at "{'pk': 2,")
  429. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  430. (should (= (python-indent-calculate-indentation) 8))
  431. (python-tests-look-at "'name': 'second'}")
  432. (should (eq (car (python-indent-context)) :inside-paren))
  433. (should (= (python-indent-calculate-indentation) 9))
  434. (python-tests-look-at "]")
  435. (should (eq (car (python-indent-context))
  436. :inside-paren-at-closing-nested-paren))
  437. (should (= (python-indent-calculate-indentation) 4))
  438. (python-tests-look-at "}}")
  439. (should (eq (car (python-indent-context))
  440. :inside-paren-at-closing-nested-paren))
  441. (should (= (python-indent-calculate-indentation) 0))
  442. (python-tests-look-at "}")
  443. (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
  444. (should (= (python-indent-calculate-indentation) 0))))
  445. (ert-deftest python-indent-inside-paren-3 ()
  446. "The simplest case possible."
  447. (python-tests-with-temp-buffer
  448. "
  449. data = ('these',
  450. 'are',
  451. 'the',
  452. 'tokens')
  453. "
  454. (python-tests-look-at "data = ('these',")
  455. (should (eq (car (python-indent-context)) :no-indent))
  456. (should (= (python-indent-calculate-indentation) 0))
  457. (forward-line 1)
  458. (should (eq (car (python-indent-context)) :inside-paren))
  459. (should (= (python-indent-calculate-indentation) 8))
  460. (forward-line 1)
  461. (should (eq (car (python-indent-context)) :inside-paren))
  462. (should (= (python-indent-calculate-indentation) 8))
  463. (forward-line 1)
  464. (should (eq (car (python-indent-context)) :inside-paren))
  465. (should (= (python-indent-calculate-indentation) 8))))
  466. (ert-deftest python-indent-inside-paren-4 ()
  467. "Respect indentation of first column."
  468. (python-tests-with-temp-buffer
  469. "
  470. data = [ [ 'these', 'are'],
  471. ['the', 'tokens' ] ]
  472. "
  473. (python-tests-look-at "data = [ [ 'these', 'are'],")
  474. (should (eq (car (python-indent-context)) :no-indent))
  475. (should (= (python-indent-calculate-indentation) 0))
  476. (forward-line 1)
  477. (should (eq (car (python-indent-context)) :inside-paren))
  478. (should (= (python-indent-calculate-indentation) 9))))
  479. (ert-deftest python-indent-inside-paren-5 ()
  480. "Test when :inside-paren initial parens are skipped in context start."
  481. (python-tests-with-temp-buffer
  482. "
  483. while ((not some_condition) and
  484. another_condition):
  485. do_something_interesting(
  486. with_some_arg)
  487. "
  488. (python-tests-look-at "while ((not some_condition) and")
  489. (should (eq (car (python-indent-context)) :no-indent))
  490. (should (= (python-indent-calculate-indentation) 0))
  491. (forward-line 1)
  492. (should (eq (car (python-indent-context)) :inside-paren))
  493. (should (= (python-indent-calculate-indentation) 7))
  494. (forward-line 1)
  495. (should (eq (car (python-indent-context)) :after-block-start))
  496. (should (= (python-indent-calculate-indentation) 4))
  497. (forward-line 1)
  498. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  499. (should (= (python-indent-calculate-indentation) 8))))
  500. (ert-deftest python-indent-inside-paren-6 ()
  501. "This should be aligned.."
  502. (python-tests-with-temp-buffer
  503. "
  504. CHOICES = (('some', 'choice'),
  505. ('another', 'choice'),
  506. ('more', 'choices'))
  507. "
  508. (python-tests-look-at "CHOICES = (('some', 'choice'),")
  509. (should (eq (car (python-indent-context)) :no-indent))
  510. (should (= (python-indent-calculate-indentation) 0))
  511. (forward-line 1)
  512. (should (eq (car (python-indent-context)) :inside-paren))
  513. (should (= (python-indent-calculate-indentation) 11))
  514. (forward-line 1)
  515. (should (eq (car (python-indent-context)) :inside-paren))
  516. (should (= (python-indent-calculate-indentation) 11))))
  517. (ert-deftest python-indent-inside-paren-7 ()
  518. "Test for Bug#21762."
  519. (python-tests-with-temp-buffer
  520. "import re as myre\nvar = [\n"
  521. (goto-char (point-max))
  522. ;; This signals an error if the test fails
  523. (should (eq (car (python-indent-context)) :inside-paren-newline-start))))
  524. (ert-deftest python-indent-after-block-1 ()
  525. "The most simple after-block case that shouldn't fail."
  526. (python-tests-with-temp-buffer
  527. "
  528. def foo(a, b, c=True):
  529. "
  530. (should (eq (car (python-indent-context)) :no-indent))
  531. (should (= (python-indent-calculate-indentation) 0))
  532. (goto-char (point-max))
  533. (should (eq (car (python-indent-context)) :after-block-start))
  534. (should (= (python-indent-calculate-indentation) 4))))
  535. (ert-deftest python-indent-after-block-2 ()
  536. "A weird (malformed) multiline block statement."
  537. (python-tests-with-temp-buffer
  538. "
  539. def foo(a, b, c={
  540. 'a':
  541. }):
  542. "
  543. (goto-char (point-max))
  544. (should (eq (car (python-indent-context)) :after-block-start))
  545. (should (= (python-indent-calculate-indentation) 4))))
  546. (ert-deftest python-indent-after-block-3 ()
  547. "A weird (malformed) sample, usually found in python shells."
  548. (python-tests-with-temp-buffer
  549. "
  550. In [1]:
  551. def func():
  552. pass
  553. In [2]:
  554. something
  555. "
  556. (python-tests-look-at "pass")
  557. (should (eq (car (python-indent-context)) :after-block-start))
  558. (should (= (python-indent-calculate-indentation) 4))
  559. (python-tests-look-at "something")
  560. (end-of-line)
  561. (should (eq (car (python-indent-context)) :after-line))
  562. (should (= (python-indent-calculate-indentation) 0))))
  563. (ert-deftest python-indent-after-async-block-1 ()
  564. "Test PEP492 async def."
  565. (python-tests-with-temp-buffer
  566. "
  567. async def foo(a, b, c=True):
  568. "
  569. (should (eq (car (python-indent-context)) :no-indent))
  570. (should (= (python-indent-calculate-indentation) 0))
  571. (goto-char (point-max))
  572. (should (eq (car (python-indent-context)) :after-block-start))
  573. (should (= (python-indent-calculate-indentation) 4))))
  574. (ert-deftest python-indent-after-async-block-2 ()
  575. "Test PEP492 async with."
  576. (python-tests-with-temp-buffer
  577. "
  578. async with foo(a) as mgr:
  579. "
  580. (should (eq (car (python-indent-context)) :no-indent))
  581. (should (= (python-indent-calculate-indentation) 0))
  582. (goto-char (point-max))
  583. (should (eq (car (python-indent-context)) :after-block-start))
  584. (should (= (python-indent-calculate-indentation) 4))))
  585. (ert-deftest python-indent-after-async-block-3 ()
  586. "Test PEP492 async for."
  587. (python-tests-with-temp-buffer
  588. "
  589. async for a in sequencer():
  590. "
  591. (should (eq (car (python-indent-context)) :no-indent))
  592. (should (= (python-indent-calculate-indentation) 0))
  593. (goto-char (point-max))
  594. (should (eq (car (python-indent-context)) :after-block-start))
  595. (should (= (python-indent-calculate-indentation) 4))))
  596. (ert-deftest python-indent-after-backslash-1 ()
  597. "The most common case."
  598. (python-tests-with-temp-buffer
  599. "
  600. from foo.bar.baz import something, something_1 \\\\
  601. something_2 something_3, \\\\
  602. something_4, something_5
  603. "
  604. (python-tests-look-at "from foo.bar.baz import something, something_1")
  605. (should (eq (car (python-indent-context)) :no-indent))
  606. (should (= (python-indent-calculate-indentation) 0))
  607. (python-tests-look-at "something_2 something_3,")
  608. (should (eq (car (python-indent-context)) :after-backslash-first-line))
  609. (should (= (python-indent-calculate-indentation) 4))
  610. (python-tests-look-at "something_4, something_5")
  611. (should (eq (car (python-indent-context)) :after-backslash))
  612. (should (= (python-indent-calculate-indentation) 4))
  613. (goto-char (point-max))
  614. (should (eq (car (python-indent-context)) :after-line))
  615. (should (= (python-indent-calculate-indentation) 0))))
  616. (ert-deftest python-indent-after-backslash-2 ()
  617. "A pretty extreme complicated case."
  618. (python-tests-with-temp-buffer
  619. "
  620. objects = Thing.objects.all() \\\\
  621. .filter(
  622. type='toy',
  623. status='bought'
  624. ) \\\\
  625. .aggregate(
  626. Sum('amount')
  627. ) \\\\
  628. .values_list()
  629. "
  630. (python-tests-look-at "objects = Thing.objects.all()")
  631. (should (eq (car (python-indent-context)) :no-indent))
  632. (should (= (python-indent-calculate-indentation) 0))
  633. (python-tests-look-at ".filter(")
  634. (should (eq (car (python-indent-context))
  635. :after-backslash-dotted-continuation))
  636. (should (= (python-indent-calculate-indentation) 23))
  637. (python-tests-look-at "type='toy',")
  638. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  639. (should (= (python-indent-calculate-indentation) 27))
  640. (python-tests-look-at "status='bought'")
  641. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  642. (should (= (python-indent-calculate-indentation) 27))
  643. (python-tests-look-at ") \\\\")
  644. (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
  645. (should (= (python-indent-calculate-indentation) 23))
  646. (python-tests-look-at ".aggregate(")
  647. (should (eq (car (python-indent-context))
  648. :after-backslash-dotted-continuation))
  649. (should (= (python-indent-calculate-indentation) 23))
  650. (python-tests-look-at "Sum('amount')")
  651. (should (eq (car (python-indent-context)) :inside-paren-newline-start))
  652. (should (= (python-indent-calculate-indentation) 27))
  653. (python-tests-look-at ") \\\\")
  654. (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
  655. (should (= (python-indent-calculate-indentation) 23))
  656. (python-tests-look-at ".values_list()")
  657. (should (eq (car (python-indent-context))
  658. :after-backslash-dotted-continuation))
  659. (should (= (python-indent-calculate-indentation) 23))
  660. (forward-line 1)
  661. (should (eq (car (python-indent-context)) :after-line))
  662. (should (= (python-indent-calculate-indentation) 0))))
  663. (ert-deftest python-indent-after-backslash-3 ()
  664. "Backslash continuation from block start."
  665. (python-tests-with-temp-buffer
  666. "
  667. with open('/path/to/some/file/you/want/to/read') as file_1, \\\\
  668. open('/path/to/some/file/being/written', 'w') as file_2:
  669. file_2.write(file_1.read())
  670. "
  671. (python-tests-look-at
  672. "with open('/path/to/some/file/you/want/to/read') as file_1, \\\\")
  673. (should (eq (car (python-indent-context)) :no-indent))
  674. (should (= (python-indent-calculate-indentation) 0))
  675. (python-tests-look-at
  676. "open('/path/to/some/file/being/written', 'w') as file_2")
  677. (should (eq (car (python-indent-context))
  678. :after-backslash-block-continuation))
  679. (should (= (python-indent-calculate-indentation) 5))
  680. (python-tests-look-at "file_2.write(file_1.read())")
  681. (should (eq (car (python-indent-context)) :after-block-start))
  682. (should (= (python-indent-calculate-indentation) 4))))
  683. (ert-deftest python-indent-after-backslash-4 ()
  684. "Backslash continuation from assignment."
  685. (python-tests-with-temp-buffer
  686. "
  687. super_awful_assignment = some_calculation() and \\\\
  688. another_calculation() and \\\\
  689. some_final_calculation()
  690. "
  691. (python-tests-look-at
  692. "super_awful_assignment = some_calculation() and \\\\")
  693. (should (eq (car (python-indent-context)) :no-indent))
  694. (should (= (python-indent-calculate-indentation) 0))
  695. (python-tests-look-at "another_calculation() and \\\\")
  696. (should (eq (car (python-indent-context))
  697. :after-backslash-assignment-continuation))
  698. (should (= (python-indent-calculate-indentation) 25))
  699. (python-tests-look-at "some_final_calculation()")
  700. (should (eq (car (python-indent-context)) :after-backslash))
  701. (should (= (python-indent-calculate-indentation) 25))))
  702. (ert-deftest python-indent-after-backslash-5 ()
  703. "Dotted continuation bizarre example."
  704. (python-tests-with-temp-buffer
  705. "
  706. def delete_all_things():
  707. Thing \\\\
  708. .objects.all() \\\\
  709. .delete()
  710. "
  711. (python-tests-look-at "Thing \\\\")
  712. (should (eq (car (python-indent-context)) :after-block-start))
  713. (should (= (python-indent-calculate-indentation) 4))
  714. (python-tests-look-at ".objects.all() \\\\")
  715. (should (eq (car (python-indent-context)) :after-backslash-first-line))
  716. (should (= (python-indent-calculate-indentation) 8))
  717. (python-tests-look-at ".delete()")
  718. (should (eq (car (python-indent-context))
  719. :after-backslash-dotted-continuation))
  720. (should (= (python-indent-calculate-indentation) 16))))
  721. (ert-deftest python-indent-block-enders-1 ()
  722. "Test de-indentation for pass keyword."
  723. (python-tests-with-temp-buffer
  724. "
  725. Class foo(object):
  726. def bar(self):
  727. if self.baz:
  728. return (1,
  729. 2,
  730. 3)
  731. else:
  732. pass
  733. "
  734. (python-tests-look-at "3)")
  735. (forward-line 1)
  736. (should (= (python-indent-calculate-indentation) 8))
  737. (python-tests-look-at "pass")
  738. (forward-line 1)
  739. (should (eq (car (python-indent-context)) :after-block-end))
  740. (should (= (python-indent-calculate-indentation) 8))))
  741. (ert-deftest python-indent-block-enders-2 ()
  742. "Test de-indentation for return keyword."
  743. (python-tests-with-temp-buffer
  744. "
  745. Class foo(object):
  746. '''raise lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
  747. eiusmod tempor incididunt ut labore et dolore magna aliqua.
  748. '''
  749. def bar(self):
  750. \"return (1, 2, 3).\"
  751. if self.baz:
  752. return (1,
  753. 2,
  754. 3)
  755. "
  756. (python-tests-look-at "def")
  757. (should (= (python-indent-calculate-indentation) 4))
  758. (python-tests-look-at "if")
  759. (should (= (python-indent-calculate-indentation) 8))
  760. (python-tests-look-at "return")
  761. (should (= (python-indent-calculate-indentation) 12))
  762. (goto-char (point-max))
  763. (should (eq (car (python-indent-context)) :after-block-end))
  764. (should (= (python-indent-calculate-indentation) 8))))
  765. (ert-deftest python-indent-block-enders-3 ()
  766. "Test de-indentation for continue keyword."
  767. (python-tests-with-temp-buffer
  768. "
  769. for element in lst:
  770. if element is None:
  771. continue
  772. "
  773. (python-tests-look-at "if")
  774. (should (= (python-indent-calculate-indentation) 4))
  775. (python-tests-look-at "continue")
  776. (should (= (python-indent-calculate-indentation) 8))
  777. (forward-line 1)
  778. (should (eq (car (python-indent-context)) :after-block-end))
  779. (should (= (python-indent-calculate-indentation) 4))))
  780. (ert-deftest python-indent-block-enders-4 ()
  781. "Test de-indentation for break keyword."
  782. (python-tests-with-temp-buffer
  783. "
  784. for element in lst:
  785. if element is None:
  786. break
  787. "
  788. (python-tests-look-at "if")
  789. (should (= (python-indent-calculate-indentation) 4))
  790. (python-tests-look-at "break")
  791. (should (= (python-indent-calculate-indentation) 8))
  792. (forward-line 1)
  793. (should (eq (car (python-indent-context)) :after-block-end))
  794. (should (= (python-indent-calculate-indentation) 4))))
  795. (ert-deftest python-indent-block-enders-5 ()
  796. "Test de-indentation for raise keyword."
  797. (python-tests-with-temp-buffer
  798. "
  799. for element in lst:
  800. if element is None:
  801. raise ValueError('Element cannot be None')
  802. "
  803. (python-tests-look-at "if")
  804. (should (= (python-indent-calculate-indentation) 4))
  805. (python-tests-look-at "raise")
  806. (should (= (python-indent-calculate-indentation) 8))
  807. (forward-line 1)
  808. (should (eq (car (python-indent-context)) :after-block-end))
  809. (should (= (python-indent-calculate-indentation) 4))))
  810. (ert-deftest python-indent-dedenters-1 ()
  811. "Test de-indentation for the elif keyword."
  812. (python-tests-with-temp-buffer
  813. "
  814. if save:
  815. try:
  816. write_to_disk(data)
  817. finally:
  818. cleanup()
  819. elif
  820. "
  821. (python-tests-look-at "elif\n")
  822. (should (eq (car (python-indent-context)) :at-dedenter-block-start))
  823. (should (= (python-indent-calculate-indentation) 0))
  824. (should (= (python-indent-calculate-indentation t) 0))))
  825. (ert-deftest python-indent-dedenters-2 ()
  826. "Test de-indentation for the else keyword."
  827. (python-tests-with-temp-buffer
  828. "
  829. if save:
  830. try:
  831. write_to_disk(data)
  832. except IOError:
  833. msg = 'Error saving to disk'
  834. message(msg)
  835. logger.exception(msg)
  836. except Exception:
  837. if hide_details:
  838. logger.exception('Unhandled exception')
  839. else
  840. finally:
  841. data.free()
  842. "
  843. (python-tests-look-at "else\n")
  844. (should (eq (car (python-indent-context)) :at-dedenter-block-start))
  845. (should (= (python-indent-calculate-indentation) 8))
  846. (python-indent-line t)
  847. (should (= (python-indent-calculate-indentation t) 4))
  848. (python-indent-line t)
  849. (should (= (python-indent-calculate-indentation t) 0))
  850. (python-indent-line t)
  851. (should (= (python-indent-calculate-indentation t) 8))))
  852. (ert-deftest python-indent-dedenters-3 ()
  853. "Test de-indentation for the except keyword."
  854. (python-tests-with-temp-buffer
  855. "
  856. if save:
  857. try:
  858. write_to_disk(data)
  859. except
  860. "
  861. (python-tests-look-at "except\n")
  862. (should (eq (car (python-indent-context)) :at-dedenter-block-start))
  863. (should (= (python-indent-calculate-indentation) 4))
  864. (python-indent-line t)
  865. (should (= (python-indent-calculate-indentation t) 4))))
  866. (ert-deftest python-indent-dedenters-4 ()
  867. "Test de-indentation for the finally keyword."
  868. (python-tests-with-temp-buffer
  869. "
  870. if save:
  871. try:
  872. write_to_disk(data)
  873. finally
  874. "
  875. (python-tests-look-at "finally\n")
  876. (should (eq (car (python-indent-context)) :at-dedenter-block-start))
  877. (should (= (python-indent-calculate-indentation) 4))
  878. (python-indent-line t)
  879. (should (= (python-indent-calculate-indentation) 4))))
  880. (ert-deftest python-indent-dedenters-5 ()
  881. "Test invalid levels are skipped in a complex example."
  882. (python-tests-with-temp-buffer
  883. "
  884. if save:
  885. try:
  886. write_to_disk(data)
  887. except IOError:
  888. msg = 'Error saving to disk'
  889. message(msg)
  890. logger.exception(msg)
  891. finally:
  892. if cleanup:
  893. do_cleanup()
  894. else
  895. "
  896. (python-tests-look-at "else\n")
  897. (should (eq (car (python-indent-context)) :at-dedenter-block-start))
  898. (should (= (python-indent-calculate-indentation) 8))
  899. (should (= (python-indent-calculate-indentation t) 0))
  900. (python-indent-line t)
  901. (should (= (python-indent-calculate-indentation t) 8))))
  902. (ert-deftest python-indent-dedenters-6 ()
  903. "Test indentation is zero when no opening block for dedenter."
  904. (python-tests-with-temp-buffer
  905. "
  906. try:
  907. # if save:
  908. write_to_disk(data)
  909. else
  910. "
  911. (python-tests-look-at "else\n")
  912. (should (eq (car (python-indent-context)) :at-dedenter-block-start))
  913. (should (= (python-indent-calculate-indentation) 0))
  914. (should (= (python-indent-calculate-indentation t) 0))))
  915. (ert-deftest python-indent-dedenters-7 ()
  916. "Test indentation case from Bug#15163."
  917. (python-tests-with-temp-buffer
  918. "
  919. if a:
  920. if b:
  921. pass
  922. else:
  923. pass
  924. else:
  925. "
  926. (python-tests-look-at "else:" 2)
  927. (should (eq (car (python-indent-context)) :at-dedenter-block-start))
  928. (should (= (python-indent-calculate-indentation) 0))
  929. (should (= (python-indent-calculate-indentation t) 0))))
  930. (ert-deftest python-indent-dedenters-8 ()
  931. "Test indentation for Bug#18432."
  932. (python-tests-with-temp-buffer
  933. "
  934. if (a == 1 or
  935. a == 2):
  936. pass
  937. elif (a == 3 or
  938. a == 4):
  939. "
  940. (python-tests-look-at "elif (a == 3 or")
  941. (should (eq (car (python-indent-context)) :at-dedenter-block-start))
  942. (should (= (python-indent-calculate-indentation) 0))
  943. (should (= (python-indent-calculate-indentation t) 0))
  944. (python-tests-look-at "a == 4):\n")
  945. (should (eq (car (python-indent-context)) :inside-paren))
  946. (should (= (python-indent-calculate-indentation) 6))
  947. (python-indent-line)
  948. (should (= (python-indent-calculate-indentation t) 4))
  949. (python-indent-line t)
  950. (should (= (python-indent-calculate-indentation t) 0))
  951. (python-indent-line t)
  952. (should (= (python-indent-calculate-indentation t) 6))))
  953. (ert-deftest python-indent-inside-string-1 ()
  954. "Test indentation for strings."
  955. (python-tests-with-temp-buffer
  956. "
  957. multiline = '''
  958. bunch
  959. of
  960. lines
  961. '''
  962. "
  963. (python-tests-look-at "multiline = '''")
  964. (should (eq (car (python-indent-context)) :no-indent))
  965. (should (= (python-indent-calculate-indentation) 0))
  966. (python-tests-look-at "bunch")
  967. (should (eq (car (python-indent-context)) :inside-string))
  968. (should (= (python-indent-calculate-indentation) 0))
  969. (python-tests-look-at "of")
  970. (should (eq (car (python-indent-context)) :inside-string))
  971. (should (= (python-indent-calculate-indentation) 0))
  972. (python-tests-look-at "lines")
  973. (should (eq (car (python-indent-context)) :inside-string))
  974. (should (= (python-indent-calculate-indentation) 0))
  975. (python-tests-look-at "'''")
  976. (should (eq (car (python-indent-context)) :inside-string))
  977. (should (= (python-indent-calculate-indentation) 0))))
  978. (ert-deftest python-indent-inside-string-2 ()
  979. "Test indentation for docstrings."
  980. (python-tests-with-temp-buffer
  981. "
  982. def fn(a, b, c=True):
  983. '''docstring
  984. bunch
  985. of
  986. lines
  987. '''
  988. "
  989. (python-tests-look-at "'''docstring")
  990. (should (eq (car (python-indent-context)) :after-block-start))
  991. (should (= (python-indent-calculate-indentation) 4))
  992. (python-tests-look-at "bunch")
  993. (should (eq (car (python-indent-context)) :inside-docstring))
  994. (should (= (python-indent-calculate-indentation) 4))
  995. (python-tests-look-at "of")
  996. (should (eq (car (python-indent-context)) :inside-docstring))
  997. ;; Any indentation deeper than the base-indent must remain unmodified.
  998. (should (= (python-indent-calculate-indentation) 8))
  999. (python-tests-look-at "lines")
  1000. (should (eq (car (python-indent-context)) :inside-docstring))
  1001. (should (= (python-indent-calculate-indentation) 4))
  1002. (python-tests-look-at "'''")
  1003. (should (eq (car (python-indent-context)) :inside-docstring))
  1004. (should (= (python-indent-calculate-indentation) 4))))
  1005. (ert-deftest python-indent-inside-string-3 ()
  1006. "Test indentation for nested strings."
  1007. (python-tests-with-temp-buffer
  1008. "
  1009. def fn(a, b, c=True):
  1010. some_var = '''
  1011. bunch
  1012. of
  1013. lines
  1014. '''
  1015. "
  1016. (python-tests-look-at "some_var = '''")
  1017. (should (eq (car (python-indent-context)) :after-block-start))
  1018. (should (= (python-indent-calculate-indentation) 4))
  1019. (python-tests-look-at "bunch")
  1020. (should (eq (car (python-indent-context)) :inside-string))
  1021. (should (= (python-indent-calculate-indentation) 4))
  1022. (python-tests-look-at "of")
  1023. (should (eq (car (python-indent-context)) :inside-string))
  1024. (should (= (python-indent-calculate-indentation) 4))
  1025. (python-tests-look-at "lines")
  1026. (should (eq (car (python-indent-context)) :inside-string))
  1027. (should (= (python-indent-calculate-indentation) 4))
  1028. (python-tests-look-at "'''")
  1029. (should (eq (car (python-indent-context)) :inside-string))
  1030. (should (= (python-indent-calculate-indentation) 4))))
  1031. (ert-deftest python-indent-electric-colon-1 ()
  1032. "Test indentation case from Bug#18228."
  1033. (python-tests-with-temp-buffer
  1034. "
  1035. def a():
  1036. pass
  1037. def b()
  1038. "
  1039. (python-tests-look-at "def b()")
  1040. (goto-char (line-end-position))
  1041. (python-tests-self-insert ":")
  1042. (should (= (current-indentation) 0))))
  1043. (ert-deftest python-indent-electric-colon-2 ()
  1044. "Test indentation case for dedenter."
  1045. (python-tests-with-temp-buffer
  1046. "
  1047. if do:
  1048. something()
  1049. else
  1050. "
  1051. (python-tests-look-at "else")
  1052. (goto-char (line-end-position))
  1053. (python-tests-self-insert ":")
  1054. (should (= (current-indentation) 0))))
  1055. (ert-deftest python-indent-electric-colon-3 ()
  1056. "Test indentation case for multi-line dedenter."
  1057. (python-tests-with-temp-buffer
  1058. "
  1059. if do:
  1060. something()
  1061. elif (this
  1062. and
  1063. that)
  1064. "
  1065. (python-tests-look-at "that)")
  1066. (goto-char (line-end-position))
  1067. (python-tests-self-insert ":")
  1068. (python-tests-look-at "elif" -1)
  1069. (should (= (current-indentation) 0))
  1070. (python-tests-look-at "and")
  1071. (should (= (current-indentation) 6))
  1072. (python-tests-look-at "that)")
  1073. (should (= (current-indentation) 6))))
  1074. (ert-deftest python-indent-region-1 ()
  1075. "Test indentation case from Bug#18843."
  1076. (let ((contents "
  1077. def foo ():
  1078. try:
  1079. pass
  1080. except:
  1081. pass
  1082. "))
  1083. (python-tests-with-temp-buffer
  1084. contents
  1085. (python-indent-region (point-min) (point-max))
  1086. (should (string= (buffer-substring-no-properties (point-min) (point-max))
  1087. contents)))))
  1088. (ert-deftest python-indent-region-2 ()
  1089. "Test region indentation on comments."
  1090. (let ((contents "
  1091. def f():
  1092. if True:
  1093. pass
  1094. # This is
  1095. # some multiline
  1096. # comment
  1097. "))
  1098. (python-tests-with-temp-buffer
  1099. contents
  1100. (python-indent-region (point-min) (point-max))
  1101. (should (string= (buffer-substring-no-properties (point-min) (point-max))
  1102. contents)))))
  1103. (ert-deftest python-indent-region-3 ()
  1104. "Test region indentation on comments."
  1105. (let ((contents "
  1106. def f():
  1107. if True:
  1108. pass
  1109. # This is
  1110. # some multiline
  1111. # comment
  1112. ")
  1113. (expected "
  1114. def f():
  1115. if True:
  1116. pass
  1117. # This is
  1118. # some multiline
  1119. # comment
  1120. "))
  1121. (python-tests-with-temp-buffer
  1122. contents
  1123. (python-indent-region (point-min) (point-max))
  1124. (should (string= (buffer-substring-no-properties (point-min) (point-max))
  1125. expected)))))
  1126. (ert-deftest python-indent-region-4 ()
  1127. "Test region indentation block starts, dedenters and enders."
  1128. (let ((contents "
  1129. def f():
  1130. if True:
  1131. a = 5
  1132. else:
  1133. a = 10
  1134. return a
  1135. ")
  1136. (expected "
  1137. def f():
  1138. if True:
  1139. a = 5
  1140. else:
  1141. a = 10
  1142. return a
  1143. "))
  1144. (python-tests-with-temp-buffer
  1145. contents
  1146. (python-indent-region (point-min) (point-max))
  1147. (should (string= (buffer-substring-no-properties (point-min) (point-max))
  1148. expected)))))
  1149. (ert-deftest python-indent-region-5 ()
  1150. "Test region indentation for docstrings."
  1151. (let ((contents "
  1152. def f():
  1153. '''
  1154. this is
  1155. a multiline
  1156. string
  1157. '''
  1158. x = \\
  1159. '''
  1160. this is an arbitrarily
  1161. indented multiline
  1162. string
  1163. '''
  1164. ")
  1165. (expected "
  1166. def f():
  1167. '''
  1168. this is
  1169. a multiline
  1170. string
  1171. '''
  1172. x = \\
  1173. '''
  1174. this is an arbitrarily
  1175. indented multiline
  1176. string
  1177. '''
  1178. "))
  1179. (python-tests-with-temp-buffer
  1180. contents
  1181. (python-indent-region (point-min) (point-max))
  1182. (should (string= (buffer-substring-no-properties (point-min) (point-max))
  1183. expected)))))
  1184. ;;; Mark
  1185. (ert-deftest python-mark-defun-1 ()
  1186. """Test `python-mark-defun' with point at defun symbol start."""
  1187. (python-tests-with-temp-buffer
  1188. "
  1189. def foo(x):
  1190. return x
  1191. class A:
  1192. pass
  1193. class B:
  1194. def __init__(self):
  1195. self.b = 'b'
  1196. def fun(self):
  1197. return self.b
  1198. class C:
  1199. '''docstring'''
  1200. "
  1201. (let ((expected-mark-beginning-position
  1202. (progn
  1203. (python-tests-look-at "class A:")
  1204. (1- (point))))
  1205. (expected-mark-end-position-1
  1206. (save-excursion
  1207. (python-tests-look-at "pass")
  1208. (forward-line)
  1209. (point)))
  1210. (expected-mark-end-position-2
  1211. (save-excursion
  1212. (python-tests-look-at "return self.b")
  1213. (forward-line)
  1214. (point)))
  1215. (expected-mark-end-position-3
  1216. (save-excursion
  1217. (python-tests-look-at "'''docstring'''")
  1218. (forward-line)
  1219. (point))))
  1220. ;; Select class A only, with point at bol.
  1221. (python-mark-defun 1)
  1222. (should (= (point) expected-mark-beginning-position))
  1223. (should (= (marker-position (mark-marker))
  1224. expected-mark-end-position-1))
  1225. ;; expand to class B, start position should remain the same.
  1226. (python-mark-defun 1)
  1227. (should (= (point) expected-mark-beginning-position))
  1228. (should (= (marker-position (mark-marker))
  1229. expected-mark-end-position-2))
  1230. ;; expand to class C, start position should remain the same.
  1231. (python-mark-defun 1)
  1232. (should (= (point) expected-mark-beginning-position))
  1233. (should (= (marker-position (mark-marker))
  1234. expected-mark-end-position-3)))))
  1235. (ert-deftest python-mark-defun-2 ()
  1236. """Test `python-mark-defun' with point at nested defun symbol start."""
  1237. (python-tests-with-temp-buffer
  1238. "
  1239. def foo(x):
  1240. return x
  1241. class A:
  1242. pass
  1243. class B:
  1244. def __init__(self):
  1245. self.b = 'b'
  1246. def fun(self):
  1247. return self.b
  1248. class C:
  1249. '''docstring'''
  1250. "
  1251. (let ((expected-mark-beginning-position
  1252. (progn
  1253. (python-tests-look-at "def __init__(self):")
  1254. (1- (line-beginning-position))))
  1255. (expected-mark-end-position-1
  1256. (save-excursion
  1257. (python-tests-look-at "self.b = 'b'")
  1258. (forward-line)
  1259. (point)))
  1260. (expected-mark-end-position-2
  1261. (save-excursion
  1262. (python-tests-look-at "return self.b")
  1263. (forward-line)
  1264. (point)))
  1265. (expected-mark-end-position-3
  1266. (save-excursion
  1267. (python-tests-look-at "'''docstring'''")
  1268. (forward-line)
  1269. (point))))
  1270. ;; Select B.__init only, with point at its start.
  1271. (python-mark-defun 1)
  1272. (should (= (point) expected-mark-beginning-position))
  1273. (should (= (marker-position (mark-marker))
  1274. expected-mark-end-position-1))
  1275. ;; expand to B.fun, start position should remain the same.
  1276. (python-mark-defun 1)
  1277. (should (= (point) expected-mark-beginning-position))
  1278. (should (= (marker-position (mark-marker))
  1279. expected-mark-end-position-2))
  1280. ;; expand to class C, start position should remain the same.
  1281. (python-mark-defun 1)
  1282. (should (= (point) expected-mark-beginning-position))
  1283. (should (= (marker-position (mark-marker))
  1284. expected-mark-end-position-3)))))
  1285. (ert-deftest python-mark-defun-3 ()
  1286. """Test `python-mark-defun' with point inside defun symbol."""
  1287. (python-tests-with-temp-buffer
  1288. "
  1289. def foo(x):
  1290. return x
  1291. class A:
  1292. pass
  1293. class B:
  1294. def __init__(self):
  1295. self.b = 'b'
  1296. def fun(self):
  1297. return self.b
  1298. class C:
  1299. '''docstring'''
  1300. "
  1301. (let ((expected-mark-beginning-position
  1302. (progn
  1303. (python-tests-look-at "def fun(self):")
  1304. (python-tests-look-at "(self):")
  1305. (1- (line-beginning-position))))
  1306. (expected-mark-end-position
  1307. (save-excursion
  1308. (python-tests-look-at "return self.b")
  1309. (forward-line)
  1310. (point))))
  1311. ;; Should select B.fun, despite point is inside the defun symbol.
  1312. (python-mark-defun 1)
  1313. (should (= (point) expected-mark-beginning-position))
  1314. (should (= (marker-position (mark-marker))
  1315. expected-mark-end-position)))))
  1316. ;;; Navigation
  1317. (ert-deftest python-nav-beginning-of-defun-1 ()
  1318. (python-tests-with-temp-buffer
  1319. "
  1320. def decoratorFunctionWithArguments(arg1, arg2, arg3):
  1321. '''print decorated function call data to stdout.
  1322. Usage:
  1323. @decoratorFunctionWithArguments('arg1', 'arg2')
  1324. def func(a, b, c=True):
  1325. pass
  1326. '''
  1327. def wwrap(f):
  1328. print 'Inside wwrap()'
  1329. def wrapped_f(*args):
  1330. print 'Inside wrapped_f()'
  1331. print 'Decorator arguments:', arg1, arg2, arg3
  1332. f(*args)
  1333. print 'After f(*args)'
  1334. return wrapped_f
  1335. return wwrap
  1336. "
  1337. (python-tests-look-at "return wrap")
  1338. (should (= (save-excursion
  1339. (python-nav-beginning-of-defun)
  1340. (point))
  1341. (save-excursion
  1342. (python-tests-look-at "def wrapped_f(*args):" -1)
  1343. (beginning-of-line)
  1344. (point))))
  1345. (python-tests-look-at "def wrapped_f(*args):" -1)
  1346. (should (= (save-excursion
  1347. (python-nav-beginning-of-defun)
  1348. (point))
  1349. (save-excursion
  1350. (python-tests-look-at "def wwrap(f):" -1)
  1351. (beginning-of-line)
  1352. (point))))
  1353. (python-tests-look-at "def wwrap(f):" -1)
  1354. (should (= (save-excursion
  1355. (python-nav-beginning-of-defun)
  1356. (point))
  1357. (save-excursion
  1358. (python-tests-look-at "def decoratorFunctionWithArguments" -1)
  1359. (beginning-of-line)
  1360. (point))))))
  1361. (ert-deftest python-nav-beginning-of-defun-2 ()
  1362. (python-tests-with-temp-buffer
  1363. "
  1364. class C(object):
  1365. def m(self):
  1366. self.c()
  1367. def b():
  1368. pass
  1369. def a():
  1370. pass
  1371. def c(self):
  1372. pass
  1373. "
  1374. ;; Nested defuns, are handled with care.
  1375. (python-tests-look-at "def c(self):")
  1376. (should (= (save-excursion
  1377. (python-nav-beginning-of-defun)
  1378. (point))
  1379. (save-excursion
  1380. (python-tests-look-at "def m(self):" -1)
  1381. (beginning-of-line)
  1382. (point))))
  1383. ;; Defuns on same levels should be respected.
  1384. (python-tests-look-at "def a():" -1)
  1385. (should (= (save-excursion
  1386. (python-nav-beginning-of-defun)
  1387. (point))
  1388. (save-excursion
  1389. (python-tests-look-at "def b():" -1)
  1390. (beginning-of-line)
  1391. (point))))
  1392. ;; Jump to a top level defun.
  1393. (python-tests-look-at "def b():" -1)
  1394. (should (= (save-excursion
  1395. (python-nav-beginning-of-defun)
  1396. (point))
  1397. (save-excursion
  1398. (python-tests-look-at "def m(self):" -1)
  1399. (beginning-of-line)
  1400. (point))))
  1401. ;; Jump to a top level defun again.
  1402. (python-tests-look-at "def m(self):" -1)
  1403. (should (= (save-excursion
  1404. (python-nav-beginning-of-defun)
  1405. (point))
  1406. (save-excursion
  1407. (python-tests-look-at "class C(object):" -1)
  1408. (beginning-of-line)
  1409. (point))))))
  1410. (ert-deftest python-nav-beginning-of-defun-3 ()
  1411. (python-tests-with-temp-buffer
  1412. "
  1413. class C(object):
  1414. async def m(self):
  1415. return await self.c()
  1416. async def c(self):
  1417. pass
  1418. "
  1419. (python-tests-look-at "self.c()")
  1420. (should (= (save-excursion
  1421. (python-nav-beginning-of-defun)
  1422. (point))
  1423. (save-excursion
  1424. (python-tests-look-at "async def m" -1)
  1425. (beginning-of-line)
  1426. (point))))))
  1427. (ert-deftest python-nav-end-of-defun-1 ()
  1428. (python-tests-with-temp-buffer
  1429. "
  1430. class C(object):
  1431. def m(self):
  1432. self.c()
  1433. def b():
  1434. pass
  1435. def a():
  1436. pass
  1437. def c(self):
  1438. pass
  1439. "
  1440. (should (= (save-excursion
  1441. (python-tests-look-at "class C(object):")
  1442. (python-nav-end-of-defun)
  1443. (point))
  1444. (save-excursion
  1445. (point-max))))
  1446. (should (= (save-excursion
  1447. (python-tests-look-at "def m(self):")
  1448. (python-nav-end-of-defun)
  1449. (point))
  1450. (save-excursion
  1451. (python-tests-look-at "def c(self):")
  1452. (forward-line -1)
  1453. (point))))
  1454. (should (= (save-excursion
  1455. (python-tests-look-at "def b():")
  1456. (python-nav-end-of-defun)
  1457. (point))
  1458. (save-excursion
  1459. (python-tests-look-at "def b():")
  1460. (forward-line 2)
  1461. (point))))
  1462. (should (= (save-excursion
  1463. (python-tests-look-at "def c(self):")
  1464. (python-nav-end-of-defun)
  1465. (point))
  1466. (save-excursion
  1467. (point-max))))))
  1468. (ert-deftest python-nav-end-of-defun-2 ()
  1469. (python-tests-with-temp-buffer
  1470. "
  1471. def decoratorFunctionWithArguments(arg1, arg2, arg3):
  1472. '''print decorated function call data to stdout.
  1473. Usage:
  1474. @decoratorFunctionWithArguments('arg1', 'arg2')
  1475. def func(a, b, c=True):
  1476. pass
  1477. '''
  1478. def wwrap(f):
  1479. print 'Inside wwrap()'
  1480. def wrapped_f(*args):
  1481. print 'Inside wrapped_f()'
  1482. print 'Decorator arguments:', arg1, arg2, arg3
  1483. f(*args)
  1484. print 'After f(*args)'
  1485. return wrapped_f
  1486. return wwrap
  1487. "
  1488. (should (= (save-excursion
  1489. (python-tests-look-at "def decoratorFunctionWithArguments")
  1490. (python-nav-end-of-defun)
  1491. (point))
  1492. (save-excursion
  1493. (point-max))))
  1494. (should (= (save-excursion
  1495. (python-tests-look-at "@decoratorFunctionWithArguments")
  1496. (python-nav-end-of-defun)
  1497. (point))
  1498. (save-excursion
  1499. (point-max))))
  1500. (should (= (save-excursion
  1501. (python-tests-look-at "def wwrap(f):")
  1502. (python-nav-end-of-defun)
  1503. (point))
  1504. (save-excursion
  1505. (python-tests-look-at "return wwrap")
  1506. (line-beginning-position))))
  1507. (should (= (save-excursion
  1508. (python-tests-look-at "def wrapped_f(*args):")
  1509. (python-nav-end-of-defun)
  1510. (point))
  1511. (save-excursion
  1512. (python-tests-look-at "return wrapped_f")
  1513. (line-beginning-position))))
  1514. (should (= (save-excursion
  1515. (python-tests-look-at "f(*args)")
  1516. (python-nav-end-of-defun)
  1517. (point))
  1518. (save-excursion
  1519. (python-tests-look-at "return wrapped_f")
  1520. (line-beginning-position))))))
  1521. (ert-deftest python-nav-backward-defun-1 ()
  1522. (python-tests-with-temp-buffer
  1523. "
  1524. class A(object): # A
  1525. def a(self): # a
  1526. pass
  1527. def b(self): # b
  1528. pass
  1529. class B(object): # B
  1530. class C(object): # C
  1531. def d(self): # d
  1532. pass
  1533. # def e(self): # e
  1534. # pass
  1535. def c(self): # c
  1536. pass
  1537. # def d(self): # d
  1538. # pass
  1539. "
  1540. (goto-char (point-max))
  1541. (should (= (save-excursion (python-nav-backward-defun))
  1542. (python-tests-look-at " def c(self): # c" -1)))
  1543. (should (= (save-excursion (python-nav-backward-defun))
  1544. (python-tests-look-at " def d(self): # d" -1)))
  1545. (should (= (save-excursion (python-nav-backward-defun))
  1546. (python-tests-look-at " class C(object): # C" -1)))
  1547. (should (= (save-excursion (python-nav-backward-defun))
  1548. (python-tests-look-at " class B(object): # B" -1)))
  1549. (should (= (save-excursion (python-nav-backward-defun))
  1550. (python-tests-look-at " def b(self): # b" -1)))
  1551. (should (= (save-excursion (python-nav-backward-defun))
  1552. (python-tests-look-at " def a(self): # a" -1)))
  1553. (should (= (save-excursion (python-nav-backward-defun))
  1554. (python-tests-look-at "class A(object): # A" -1)))
  1555. (should (not (python-nav-backward-defun)))))
  1556. (ert-deftest python-nav-backward-defun-2 ()
  1557. (python-tests-with-temp-buffer
  1558. "
  1559. def decoratorFunctionWithArguments(arg1, arg2, arg3):
  1560. '''print decorated function call data to stdout.
  1561. Usage:
  1562. @decoratorFunctionWithArguments('arg1', 'arg2')
  1563. def func(a, b, c=True):
  1564. pass
  1565. '''
  1566. def wwrap(f):
  1567. print 'Inside wwrap()'
  1568. def wrapped_f(*args):
  1569. print 'Inside wrapped_f()'
  1570. print 'Decorator arguments:', arg1, arg2, arg3
  1571. f(*args)
  1572. print 'After f(*args)'
  1573. return wrapped_f
  1574. return wwrap
  1575. "
  1576. (goto-char (point-max))
  1577. (should (= (save-excursion (python-nav-backward-defun))
  1578. (python-tests-look-at " def wrapped_f(*args):" -1)))
  1579. (should (= (save-excursion (python-nav-backward-defun))
  1580. (python-tests-look-at " def wwrap(f):" -1)))
  1581. (should (= (save-excursion (python-nav-backward-defun))
  1582. (python-tests-look-at "def decoratorFunctionWithArguments(arg1, arg2, arg3):" -1)))
  1583. (should (not (python-nav-backward-defun)))))
  1584. (ert-deftest python-nav-backward-defun-3 ()
  1585. (python-tests-with-temp-buffer
  1586. "
  1587. '''
  1588. def u(self):
  1589. pass
  1590. def v(self):
  1591. pass
  1592. def w(self):
  1593. pass
  1594. '''
  1595. class A(object):
  1596. pass
  1597. "
  1598. (goto-char (point-min))
  1599. (let ((point (python-tests-look-at "class A(object):")))
  1600. (should (not (python-nav-backward-defun)))
  1601. (should (= point (point))))))
  1602. (ert-deftest python-nav-forward-defun-1 ()
  1603. (python-tests-with-temp-buffer
  1604. "
  1605. class A(object): # A
  1606. def a(self): # a
  1607. pass
  1608. def b(self): # b
  1609. pass
  1610. class B(object): # B
  1611. class C(object): # C
  1612. def d(self): # d
  1613. pass
  1614. # def e(self): # e
  1615. # pass
  1616. def c(self): # c
  1617. pass
  1618. # def d(self): # d
  1619. # pass
  1620. "
  1621. (goto-char (point-min))
  1622. (should (= (save-excursion (python-nav-forward-defun))
  1623. (python-tests-look-at "(object): # A")))
  1624. (should (= (save-excursion (python-nav-forward-defun))
  1625. (python-tests-look-at "(self): # a")))
  1626. (should (= (save-excursion (python-nav-forward-defun))
  1627. (python-tests-look-at "(self): # b")))
  1628. (should (= (save-excursion (python-nav-forward-defun))
  1629. (python-tests-look-at "(object): # B")))
  1630. (should (= (save-excursion (python-nav-forward-defun))
  1631. (python-tests-look-at "(object): # C")))
  1632. (should (= (save-excursion (python-nav-forward-defun))
  1633. (python-tests-look-at "(self): # d")))
  1634. (should (= (save-excursion (python-nav-forward-defun))
  1635. (python-tests-look-at "(self): # c")))
  1636. (should (not (python-nav-forward-defun)))))
  1637. (ert-deftest python-nav-forward-defun-2 ()
  1638. (python-tests-with-temp-buffer
  1639. "
  1640. def decoratorFunctionWithArguments(arg1, arg2, arg3):
  1641. '''print decorated function call data to stdout.
  1642. Usage:
  1643. @decoratorFunctionWithArguments('arg1', 'arg2')
  1644. def func(a, b, c=True):
  1645. pass
  1646. '''
  1647. def wwrap(f):
  1648. print 'Inside wwrap()'
  1649. def wrapped_f(*args):
  1650. print 'Inside wrapped_f()'
  1651. print 'Decorator arguments:', arg1, arg2, arg3
  1652. f(*args)
  1653. print 'After f(*args)'
  1654. return wrapped_f
  1655. return wwrap
  1656. "
  1657. (goto-char (point-min))
  1658. (should (= (save-excursion (python-nav-forward-defun))
  1659. (python-tests-look-at "(arg1, arg2, arg3):")))
  1660. (should (= (save-excursion (python-nav-forward-defun))
  1661. (python-tests-look-at "(f):")))
  1662. (should (= (save-excursion (python-nav-forward-defun))
  1663. (python-tests-look-at "(*args):")))
  1664. (should (not (python-nav-forward-defun)))))
  1665. (ert-deftest python-nav-forward-defun-3 ()
  1666. (python-tests-with-temp-buffer
  1667. "
  1668. class A(object):
  1669. pass
  1670. '''
  1671. def u(self):
  1672. pass
  1673. def v(self):
  1674. pass
  1675. def w(self):
  1676. pass
  1677. '''
  1678. "
  1679. (goto-char (point-min))
  1680. (let ((point (python-tests-look-at "(object):")))
  1681. (should (not (python-nav-forward-defun)))
  1682. (should (= point (point))))))
  1683. (ert-deftest python-nav-beginning-of-statement-1 ()
  1684. (python-tests-with-temp-buffer
  1685. "
  1686. v1 = 123 + \
  1687. 456 + \
  1688. 789
  1689. v2 = (value1,
  1690. value2,
  1691. value3,
  1692. value4)
  1693. v3 = ('this is a string'
  1694. 'that is continued'
  1695. 'between lines'
  1696. 'within a paren',
  1697. # this is a comment, yo
  1698. 'continue previous line')
  1699. v4 = '''
  1700. a very long
  1701. string
  1702. '''
  1703. "
  1704. (python-tests-look-at "v2 =")
  1705. (python-util-forward-comment -1)
  1706. (should (= (save-excursion
  1707. (python-nav-beginning-of-statement)
  1708. (point))
  1709. (python-tests-look-at "v1 =" -1 t)))
  1710. (python-tests-look-at "v3 =")
  1711. (python-util-forward-comment -1)
  1712. (should (= (save-excursion
  1713. (python-nav-beginning-of-statement)
  1714. (point))
  1715. (python-tests-look-at "v2 =" -1 t)))
  1716. (python-tests-look-at "v4 =")
  1717. (python-util-forward-comment -1)
  1718. (should (= (save-excursion
  1719. (python-nav-beginning-of-statement)
  1720. (point))
  1721. (python-tests-look-at "v3 =" -1 t)))
  1722. (goto-char (point-max))
  1723. (python-util-forward-comment -1)
  1724. (should (= (save-excursion
  1725. (python-nav-beginning-of-statement)
  1726. (point))
  1727. (python-tests-look-at "v4 =" -1 t)))))
  1728. (ert-deftest python-nav-end-of-statement-1 ()
  1729. (python-tests-with-temp-buffer
  1730. "
  1731. v1 = 123 + \
  1732. 456 + \
  1733. 789
  1734. v2 = (value1,
  1735. value2,
  1736. value3,
  1737. value4)
  1738. v3 = ('this is a string'
  1739. 'that is continued'
  1740. 'between lines'
  1741. 'within a paren',
  1742. # this is a comment, yo
  1743. 'continue previous line')
  1744. v4 = '''
  1745. a very long
  1746. string
  1747. '''
  1748. "
  1749. (python-tests-look-at "v1 =")
  1750. (should (= (save-excursion
  1751. (python-nav-end-of-statement)
  1752. (point))
  1753. (save-excursion
  1754. (python-tests-look-at "789")
  1755. (line-end-position))))
  1756. (python-tests-look-at "v2 =")
  1757. (should (= (save-excursion
  1758. (python-nav-end-of-statement)
  1759. (point))
  1760. (save-excursion
  1761. (python-tests-look-at "value4)")
  1762. (line-end-position))))
  1763. (python-tests-look-at "v3 =")
  1764. (should (= (save-excursion
  1765. (python-nav-end-of-statement)
  1766. (point))
  1767. (save-excursion
  1768. (python-tests-look-at
  1769. "'continue previous line')")
  1770. (line-end-position))))
  1771. (python-tests-look-at "v4 =")
  1772. (should (= (save-excursion
  1773. (python-nav-end-of-statement)
  1774. (point))
  1775. (save-excursion
  1776. (goto-char (point-max))
  1777. (python-util-forward-comment -1)
  1778. (point))))))
  1779. (ert-deftest python-nav-forward-statement-1 ()
  1780. (python-tests-with-temp-buffer
  1781. "
  1782. v1 = 123 + \
  1783. 456 + \
  1784. 789
  1785. v2 = (value1,
  1786. value2,
  1787. value3,
  1788. value4)
  1789. v3 = ('this is a string'
  1790. 'that is continued'
  1791. 'between lines'
  1792. 'within a paren',
  1793. # this is a comment, yo
  1794. 'continue previous line')
  1795. v4 = '''
  1796. a very long
  1797. string
  1798. '''
  1799. "
  1800. (python-tests-look-at "v1 =")
  1801. (should (= (save-excursion
  1802. (python-nav-forward-statement)
  1803. (point))
  1804. (python-tests-look-at "v2 =")))
  1805. (should (= (save-excursion
  1806. (python-nav-forward-statement)
  1807. (point))
  1808. (python-tests-look-at "v3 =")))
  1809. (should (= (save-excursion
  1810. (python-nav-forward-statement)
  1811. (point))
  1812. (python-tests-look-at "v4 =")))
  1813. (should (= (save-excursion
  1814. (python-nav-forward-statement)
  1815. (point))
  1816. (point-max)))))
  1817. (ert-deftest python-nav-backward-statement-1 ()
  1818. (python-tests-with-temp-buffer
  1819. "
  1820. v1 = 123 + \
  1821. 456 + \
  1822. 789
  1823. v2 = (value1,
  1824. value2,
  1825. value3,
  1826. value4)
  1827. v3 = ('this is a string'
  1828. 'that is continued'
  1829. 'between lines'
  1830. 'within a paren',
  1831. # this is a comment, yo
  1832. 'continue previous line')
  1833. v4 = '''
  1834. a very long
  1835. string
  1836. '''
  1837. "
  1838. (goto-char (point-max))
  1839. (should (= (save-excursion
  1840. (python-nav-backward-statement)
  1841. (point))
  1842. (python-tests-look-at "v4 =" -1)))
  1843. (should (= (save-excursion
  1844. (python-nav-backward-statement)
  1845. (point))
  1846. (python-tests-look-at "v3 =" -1)))
  1847. (should (= (save-excursion
  1848. (python-nav-backward-statement)
  1849. (point))
  1850. (python-tests-look-at "v2 =" -1)))
  1851. (should (= (save-excursion
  1852. (python-nav-backward-statement)
  1853. (point))
  1854. (python-tests-look-at "v1 =" -1)))))
  1855. (ert-deftest python-nav-backward-statement-2 ()
  1856. :expected-result :failed
  1857. (python-tests-with-temp-buffer
  1858. "
  1859. v1 = 123 + \
  1860. 456 + \
  1861. 789
  1862. v2 = (value1,
  1863. value2,
  1864. value3,
  1865. value4)
  1866. "
  1867. ;; FIXME: For some reason `python-nav-backward-statement' is moving
  1868. ;; back two sentences when starting from 'value4)'.
  1869. (goto-char (point-max))
  1870. (python-util-forward-comment -1)
  1871. (should (= (save-excursion
  1872. (python-nav-backward-statement)
  1873. (point))
  1874. (python-tests-look-at "v2 =" -1 t)))))
  1875. (ert-deftest python-nav-beginning-of-block-1 ()
  1876. (python-tests-with-temp-buffer
  1877. "
  1878. def decoratorFunctionWithArguments(arg1, arg2, arg3):
  1879. '''print decorated function call data to stdout.
  1880. Usage:
  1881. @decoratorFunctionWithArguments('arg1', 'arg2')
  1882. def func(a, b, c=True):
  1883. pass
  1884. '''
  1885. def wwrap(f):
  1886. print 'Inside wwrap()'
  1887. def wrapped_f(*args):
  1888. print 'Inside wrapped_f()'
  1889. print 'Decorator arguments:', arg1, arg2, arg3
  1890. f(*args)
  1891. print 'After f(*args)'
  1892. return wrapped_f
  1893. return wwrap
  1894. "
  1895. (python-tests-look-at "return wwrap")
  1896. (should (= (save-excursion
  1897. (python-nav-beginning-of-block)
  1898. (point))
  1899. (python-tests-look-at "def decoratorFunctionWithArguments" -1)))
  1900. (python-tests-look-at "print 'Inside wwrap()'")
  1901. (should (= (save-excursion
  1902. (python-nav-beginning-of-block)
  1903. (point))
  1904. (python-tests-look-at "def wwrap(f):" -1)))
  1905. (python-tests-look-at "print 'After f(*args)'")
  1906. (end-of-line)
  1907. (should (= (save-excursion
  1908. (python-nav-beginning-of-block)
  1909. (point))
  1910. (python-tests-look-at "def wrapped_f(*args):" -1)))
  1911. (python-tests-look-at "return wrapped_f")
  1912. (should (= (save-excursion
  1913. (python-nav-beginning-of-block)
  1914. (point))
  1915. (python-tests-look-at "def wwrap(f):" -1)))))
  1916. (ert-deftest python-nav-end-of-block-1 ()
  1917. (python-tests-with-temp-buffer
  1918. "
  1919. def decoratorFunctionWithArguments(arg1, arg2, arg3):
  1920. '''print decorated function call data to stdout.
  1921. Usage:
  1922. @decoratorFunctionWithArguments('arg1', 'arg2')
  1923. def func(a, b, c=True):
  1924. pass
  1925. '''
  1926. def wwrap(f):
  1927. print 'Inside wwrap()'
  1928. def wrapped_f(*args):
  1929. print 'Inside wrapped_f()'
  1930. print 'Decorator arguments:', arg1, arg2, arg3
  1931. f(*args)
  1932. print 'After f(*args)'
  1933. return wrapped_f
  1934. return wwrap
  1935. "
  1936. (python-tests-look-at "def decoratorFunctionWithArguments")
  1937. (should (= (save-excursion
  1938. (python-nav-end-of-block)
  1939. (point))
  1940. (save-excursion
  1941. (goto-char (point-max))
  1942. (python-util-forward-comment -1)
  1943. (point))))
  1944. (python-tests-look-at "def wwrap(f):")
  1945. (should (= (save-excursion
  1946. (python-nav-end-of-block)
  1947. (point))
  1948. (save-excursion
  1949. (python-tests-look-at "return wrapped_f")
  1950. (line-end-position))))
  1951. (end-of-line)
  1952. (should (= (save-excursion
  1953. (python-nav-end-of-block)
  1954. (point))
  1955. (save-excursion
  1956. (python-tests-look-at "return wrapped_f")
  1957. (line-end-position))))
  1958. (python-tests-look-at "f(*args)")
  1959. (should (= (save-excursion
  1960. (python-nav-end-of-block)
  1961. (point))
  1962. (save-excursion
  1963. (python-tests-look-at "print 'After f(*args)'")
  1964. (line-end-position))))))
  1965. (ert-deftest python-nav-forward-block-1 ()
  1966. "This also accounts as a test for `python-nav-backward-block'."
  1967. (python-tests-with-temp-buffer
  1968. "
  1969. if request.user.is_authenticated():
  1970. # def block():
  1971. # pass
  1972. try:
  1973. profile = request.user.get_profile()
  1974. except Profile.DoesNotExist:
  1975. profile = Profile.objects.create(user=request.user)
  1976. else:
  1977. if profile.stats:
  1978. profile.recalculate_stats()
  1979. else:
  1980. profile.clear_stats()
  1981. finally:
  1982. profile.views += 1
  1983. profile.save()
  1984. "
  1985. (should (= (save-excursion (python-nav-forward-block))
  1986. (python-tests-look-at "if request.user.is_authenticated():")))
  1987. (should (= (save-excursion (python-nav-forward-block))
  1988. (python-tests-look-at "try:")))
  1989. (should (= (save-excursion (python-nav-forward-block))
  1990. (python-tests-look-at "except Profile.DoesNotExist:")))
  1991. (should (= (save-excursion (python-nav-forward-block))
  1992. (python-tests-look-at "else:")))
  1993. (should (= (save-excursion (python-nav-forward-block))
  1994. (python-tests-look-at "if profile.stats:")))
  1995. (should (= (save-excursion (python-nav-forward-block))
  1996. (python-tests-look-at "else:")))
  1997. (should (= (save-excursion (python-nav-forward-block))
  1998. (python-tests-look-at "finally:")))
  1999. ;; When point is at the last block, leave it there and return nil
  2000. (should (not (save-excursion (python-nav-forward-block))))
  2001. ;; Move backwards, and even if the number of moves is less than the
  2002. ;; provided argument return the point.
  2003. (should (= (save-excursion (python-nav-forward-block -10))
  2004. (python-tests-look-at
  2005. "if request.user.is_authenticated():" -1)))))
  2006. (ert-deftest python-nav-forward-sexp-1 ()
  2007. (python-tests-with-temp-buffer
  2008. "
  2009. a()
  2010. b()
  2011. c()
  2012. "
  2013. (python-tests-look-at "a()")
  2014. (python-nav-forward-sexp)
  2015. (should (looking-at "$"))
  2016. (should (save-excursion
  2017. (beginning-of-line)
  2018. (looking-at "a()")))
  2019. (python-nav-forward-sexp)
  2020. (should (looking-at "$"))
  2021. (should (save-excursion
  2022. (beginning-of-line)
  2023. (looking-at "b()")))
  2024. (python-nav-forward-sexp)
  2025. (should (looking-at "$"))
  2026. (should (save-excursion
  2027. (beginning-of-line)
  2028. (looking-at "c()")))
  2029. ;; The default behavior when next to a paren should do what lisp
  2030. ;; does and, otherwise `blink-matching-open' breaks.
  2031. (python-nav-forward-sexp -1)
  2032. (should (looking-at "()"))
  2033. (should (save-excursion
  2034. (beginning-of-line)
  2035. (looking-at "c()")))
  2036. (end-of-line)
  2037. ;; Skipping parens should jump to `bolp'
  2038. (python-nav-forward-sexp -1 nil t)
  2039. (should (looking-at "c()"))
  2040. (forward-line -1)
  2041. (end-of-line)
  2042. ;; b()
  2043. (python-nav-forward-sexp -1)
  2044. (should (looking-at "()"))
  2045. (python-nav-forward-sexp -1)
  2046. (should (looking-at "b()"))
  2047. (end-of-line)
  2048. (python-nav-forward-sexp -1 nil t)
  2049. (should (looking-at "b()"))
  2050. (forward-line -1)
  2051. (end-of-line)
  2052. ;; a()
  2053. (python-nav-forward-sexp -1)
  2054. (should (looking-at "()"))
  2055. (python-nav-forward-sexp -1)
  2056. (should (looking-at "a()"))
  2057. (end-of-line)
  2058. (python-nav-forward-sexp -1 nil t)
  2059. (should (looking-at "a()"))))
  2060. (ert-deftest python-nav-forward-sexp-2 ()
  2061. (python-tests-with-temp-buffer
  2062. "
  2063. def func():
  2064. if True:
  2065. aaa = bbb
  2066. ccc = ddd
  2067. eee = fff
  2068. return ggg
  2069. "
  2070. (python-tests-look-at "aa =")
  2071. (python-nav-forward-sexp)
  2072. (should (looking-at " = bbb"))
  2073. (python-nav-forward-sexp)
  2074. (should (looking-at "$"))
  2075. (should (save-excursion
  2076. (back-to-indentation)
  2077. (looking-at "aaa = bbb")))
  2078. (python-nav-forward-sexp)
  2079. (should (looking-at "$"))
  2080. (should (save-excursion
  2081. (back-to-indentation)
  2082. (looking-at "ccc = ddd")))
  2083. (python-nav-forward-sexp)
  2084. (should (looking-at "$"))
  2085. (should (save-excursion
  2086. (back-to-indentation)
  2087. (looking-at "eee = fff")))
  2088. (python-nav-forward-sexp)
  2089. (should (looking-at "$"))
  2090. (should (save-excursion
  2091. (back-to-indentation)
  2092. (looking-at "return ggg")))
  2093. (python-nav-forward-sexp -1)
  2094. (should (looking-at "def func():"))))
  2095. (ert-deftest python-nav-forward-sexp-3 ()
  2096. (python-tests-with-temp-buffer
  2097. "
  2098. from some_module import some_sub_module
  2099. from another_module import another_sub_module
  2100. def another_statement():
  2101. pass
  2102. "
  2103. (python-tests-look-at "some_module")
  2104. (python-nav-forward-sexp)
  2105. (should (looking-at " import"))
  2106. (python-nav-forward-sexp)
  2107. (should (looking-at " some_sub_module"))
  2108. (python-nav-forward-sexp)
  2109. (should (looking-at "$"))
  2110. (should
  2111. (save-excursion
  2112. (back-to-indentation)
  2113. (looking-at
  2114. "from some_module import some_sub_module")))
  2115. (python-nav-forward-sexp)
  2116. (should (looking-at "$"))
  2117. (should
  2118. (save-excursion
  2119. (back-to-indentation)
  2120. (looking-at
  2121. "from another_module import another_sub_module")))
  2122. (python-nav-forward-sexp)
  2123. (should (looking-at "$"))
  2124. (should
  2125. (save-excursion
  2126. (back-to-indentation)
  2127. (looking-at
  2128. "pass")))
  2129. (python-nav-forward-sexp -1)
  2130. (should (looking-at "def another_statement():"))
  2131. (python-nav-forward-sexp -1)
  2132. (should (looking-at "from another_module import another_sub_module"))
  2133. (python-nav-forward-sexp -1)
  2134. (should (looking-at "from some_module import some_sub_module"))))
  2135. (ert-deftest python-nav-forward-sexp-safe-1 ()
  2136. (python-tests-with-temp-buffer
  2137. "
  2138. profile = Profile.objects.create(user=request.user)
  2139. profile.notify()
  2140. "
  2141. (python-tests-look-at "profile =")
  2142. (python-nav-forward-sexp-safe 1)
  2143. (should (looking-at "$"))
  2144. (beginning-of-line 1)
  2145. (python-tests-look-at "user=request.user")
  2146. (python-nav-forward-sexp-safe -1)
  2147. (should (looking-at "(user=request.user)"))
  2148. (python-nav-forward-sexp-safe -4)
  2149. (should (looking-at "profile ="))
  2150. (python-tests-look-at "user=request.user")
  2151. (python-nav-forward-sexp-safe 3)
  2152. (should (looking-at ")"))
  2153. (python-nav-forward-sexp-safe 1)
  2154. (should (looking-at "$"))
  2155. (python-nav-forward-sexp-safe 1)
  2156. (should (looking-at "$"))))
  2157. (ert-deftest python-nav-up-list-1 ()
  2158. (python-tests-with-temp-buffer
  2159. "
  2160. def f():
  2161. if True:
  2162. return [i for i in range(3)]
  2163. "
  2164. (python-tests-look-at "3)]")
  2165. (python-nav-up-list)
  2166. (should (looking-at "]"))
  2167. (python-nav-up-list)
  2168. (should (looking-at "$"))))
  2169. (ert-deftest python-nav-backward-up-list-1 ()
  2170. :expected-result :failed
  2171. (python-tests-with-temp-buffer
  2172. "
  2173. def f():
  2174. if True:
  2175. return [i for i in range(3)]
  2176. "
  2177. (python-tests-look-at "3)]")
  2178. (python-nav-backward-up-list)
  2179. (should (looking-at "(3)\\]"))
  2180. (python-nav-backward-up-list)
  2181. (should (looking-at
  2182. "\\[i for i in range(3)\\]"))
  2183. ;; FIXME: Need to move to beginning-of-statement.
  2184. (python-nav-backward-up-list)
  2185. (should (looking-at
  2186. "return \\[i for i in range(3)\\]"))
  2187. (python-nav-backward-up-list)
  2188. (should (looking-at "if True:"))
  2189. (python-nav-backward-up-list)
  2190. (should (looking-at "def f():"))))
  2191. (ert-deftest python-indent-dedent-line-backspace-1 ()
  2192. "Check de-indentation on first call. Bug#18319."
  2193. (python-tests-with-temp-buffer
  2194. "
  2195. if True:
  2196. x ()
  2197. if False:
  2198. "
  2199. (python-tests-look-at "if False:")
  2200. (call-interactively #'python-indent-dedent-line-backspace)
  2201. (should (zerop (current-indentation)))
  2202. ;; XXX: This should be a call to `undo' but it's triggering errors.
  2203. (insert " ")
  2204. (should (= (current-indentation) 4))
  2205. (call-interactively #'python-indent-dedent-line-backspace)
  2206. (should (zerop (current-indentation)))))
  2207. (ert-deftest python-indent-dedent-line-backspace-2 ()
  2208. "Check de-indentation with tabs. Bug#19730."
  2209. (let ((tab-width 8))
  2210. (python-tests-with-temp-buffer
  2211. "
  2212. if x:
  2213. \tabcdefg
  2214. "
  2215. (python-tests-look-at "abcdefg")
  2216. (goto-char (line-end-position))
  2217. (call-interactively #'python-indent-dedent-line-backspace)
  2218. (should
  2219. (string= (buffer-substring-no-properties
  2220. (line-beginning-position) (line-end-position))
  2221. "\tabcdef")))))
  2222. (ert-deftest python-indent-dedent-line-backspace-3 ()
  2223. "Paranoid check of de-indentation with tabs. Bug#19730."
  2224. (let ((tab-width 8))
  2225. (python-tests-with-temp-buffer
  2226. "
  2227. if x:
  2228. \tif y:
  2229. \t abcdefg
  2230. "
  2231. (python-tests-look-at "abcdefg")
  2232. (goto-char (line-end-position))
  2233. (call-interactively #'python-indent-dedent-line-backspace)
  2234. (should
  2235. (string= (buffer-substring-no-properties
  2236. (line-beginning-position) (line-end-position))
  2237. "\t abcdef"))
  2238. (back-to-indentation)
  2239. (call-interactively #'python-indent-dedent-line-backspace)
  2240. (should
  2241. (string= (buffer-substring-no-properties
  2242. (line-beginning-position) (line-end-position))
  2243. "\tabcdef"))
  2244. (call-interactively #'python-indent-dedent-line-backspace)
  2245. (should
  2246. (string= (buffer-substring-no-properties
  2247. (line-beginning-position) (line-end-position))
  2248. " abcdef"))
  2249. (call-interactively #'python-indent-dedent-line-backspace)
  2250. (should
  2251. (string= (buffer-substring-no-properties
  2252. (line-beginning-position) (line-end-position))
  2253. "abcdef")))))
  2254. (ert-deftest python-bob-infloop-avoid ()
  2255. "Test that strings at BOB don't confuse syntax analysis. Bug#24905"
  2256. (python-tests-with-temp-buffer
  2257. " \"\n"
  2258. (goto-char (point-min))
  2259. (font-lock-fontify-buffer)))
  2260. ;;; Shell integration
  2261. (defvar python-tests-shell-interpreter "python")
  2262. (ert-deftest python-shell-get-process-name-1 ()
  2263. "Check process name calculation sans `buffer-file-name'."
  2264. (python-tests-with-temp-buffer
  2265. ""
  2266. (should (string= (python-shell-get-process-name nil)
  2267. python-shell-buffer-name))
  2268. (should (string= (python-shell-get-process-name t)
  2269. (format "%s[%s]" python-shell-buffer-name (buffer-name))))))
  2270. (ert-deftest python-shell-get-process-name-2 ()
  2271. "Check process name calculation with `buffer-file-name'."
  2272. (python-tests-with-temp-file
  2273. ""
  2274. ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
  2275. ;; should be respected.
  2276. (should (string= (python-shell-get-process-name nil)
  2277. python-shell-buffer-name))
  2278. (should (string=
  2279. (python-shell-get-process-name t)
  2280. (format "%s[%s]" python-shell-buffer-name (buffer-name))))))
  2281. (ert-deftest python-shell-internal-get-process-name-1 ()
  2282. "Check the internal process name is buffer-unique sans `buffer-file-name'."
  2283. (python-tests-with-temp-buffer
  2284. ""
  2285. (should (string= (python-shell-internal-get-process-name)
  2286. (format "%s[%s]" python-shell-internal-buffer-name (buffer-name))))))
  2287. (ert-deftest python-shell-internal-get-process-name-2 ()
  2288. "Check the internal process name is buffer-unique with `buffer-file-name'."
  2289. (python-tests-with-temp-file
  2290. ""
  2291. (should (string= (python-shell-internal-get-process-name)
  2292. (format "%s[%s]" python-shell-internal-buffer-name (buffer-name))))))
  2293. (ert-deftest python-shell-calculate-command-1 ()
  2294. "Check the command to execute is calculated correctly.
  2295. Using `python-shell-interpreter' and
  2296. `python-shell-interpreter-args'."
  2297. (skip-unless (executable-find python-tests-shell-interpreter))
  2298. (let ((python-shell-interpreter (executable-find
  2299. python-tests-shell-interpreter))
  2300. (python-shell-interpreter-args "-B"))
  2301. (should (string=
  2302. (format "%s %s"
  2303. (shell-quote-argument python-shell-interpreter)
  2304. python-shell-interpreter-args)
  2305. (python-shell-calculate-command)))))
  2306. (ert-deftest python-shell-calculate-pythonpath-1 ()
  2307. "Test PYTHONPATH calculation."
  2308. (let ((process-environment '("PYTHONPATH=/path0"))
  2309. (python-shell-extra-pythonpaths '("/path1" "/path2")))
  2310. (should (string= (python-shell-calculate-pythonpath)
  2311. (concat "/path1" path-separator
  2312. "/path2" path-separator "/path0")))))
  2313. (ert-deftest python-shell-calculate-pythonpath-2 ()
  2314. "Test existing paths are moved to front."
  2315. (let ((process-environment
  2316. (list (concat "PYTHONPATH=/path0" path-separator "/path1")))
  2317. (python-shell-extra-pythonpaths '("/path1" "/path2")))
  2318. (should (string= (python-shell-calculate-pythonpath)
  2319. (concat "/path1" path-separator
  2320. "/path2" path-separator "/path0")))))
  2321. (ert-deftest python-shell-calculate-process-environment-1 ()
  2322. "Test `python-shell-process-environment' modification."
  2323. (let* ((python-shell-process-environment
  2324. '("TESTVAR1=value1" "TESTVAR2=value2"))
  2325. (process-environment (python-shell-calculate-process-environment)))
  2326. (should (equal (getenv "TESTVAR1") "value1"))
  2327. (should (equal (getenv "TESTVAR2") "value2"))))
  2328. (ert-deftest python-shell-calculate-process-environment-2 ()
  2329. "Test `python-shell-extra-pythonpaths' modification."
  2330. (let* ((process-environment process-environment)
  2331. (original-pythonpath (setenv "PYTHONPATH" "/path0"))
  2332. (python-shell-extra-pythonpaths '("/path1" "/path2"))
  2333. (process-environment (python-shell-calculate-process-environment)))
  2334. (should (equal (getenv "PYTHONPATH")
  2335. (concat "/path1" path-separator
  2336. "/path2" path-separator "/path0")))))
  2337. (ert-deftest python-shell-calculate-process-environment-3 ()
  2338. "Test `python-shell-virtualenv-root' modification."
  2339. (let* ((python-shell-virtualenv-root "/env")
  2340. (process-environment
  2341. (let (process-environment process-environment)
  2342. (setenv "PYTHONHOME" "/home")
  2343. (setenv "VIRTUAL_ENV")
  2344. (python-shell-calculate-process-environment))))
  2345. (should (not (getenv "PYTHONHOME")))
  2346. (should (string= (getenv "VIRTUAL_ENV") "/env"))))
  2347. (ert-deftest python-shell-calculate-process-environment-4 ()
  2348. "Test PYTHONUNBUFFERED when `python-shell-unbuffered' is non-nil."
  2349. (let* ((python-shell-unbuffered t)
  2350. (process-environment
  2351. (let ((process-environment process-environment))
  2352. (setenv "PYTHONUNBUFFERED")
  2353. (python-shell-calculate-process-environment))))
  2354. (should (string= (getenv "PYTHONUNBUFFERED") "1"))))
  2355. (ert-deftest python-shell-calculate-process-environment-5 ()
  2356. "Test PYTHONUNBUFFERED when `python-shell-unbuffered' is nil."
  2357. (let* ((python-shell-unbuffered nil)
  2358. (process-environment
  2359. (let ((process-environment process-environment))
  2360. (setenv "PYTHONUNBUFFERED")
  2361. (python-shell-calculate-process-environment))))
  2362. (should (not (getenv "PYTHONUNBUFFERED")))))
  2363. (ert-deftest python-shell-calculate-process-environment-6 ()
  2364. "Test PYTHONUNBUFFERED=1 when `python-shell-unbuffered' is nil."
  2365. (let* ((python-shell-unbuffered nil)
  2366. (process-environment
  2367. (let ((process-environment process-environment))
  2368. (setenv "PYTHONUNBUFFERED" "1")
  2369. (python-shell-calculate-process-environment))))
  2370. ;; User default settings must remain untouched:
  2371. (should (string= (getenv "PYTHONUNBUFFERED") "1"))))
  2372. (ert-deftest python-shell-calculate-process-environment-7 ()
  2373. "Test no side-effects on `process-environment'."
  2374. (let* ((python-shell-process-environment
  2375. '("TESTVAR1=value1" "TESTVAR2=value2"))
  2376. (python-shell-virtualenv-root "/env")
  2377. (python-shell-unbuffered t)
  2378. (python-shell-extra-pythonpaths'("/path1" "/path2"))
  2379. (original-process-environment (copy-sequence process-environment)))
  2380. (python-shell-calculate-process-environment)
  2381. (should (equal process-environment original-process-environment))))
  2382. (ert-deftest python-shell-calculate-process-environment-8 ()
  2383. "Test no side-effects on `tramp-remote-process-environment'."
  2384. (let* ((default-directory "/ssh::/example/dir/")
  2385. (python-shell-process-environment
  2386. '("TESTVAR1=value1" "TESTVAR2=value2"))
  2387. (python-shell-virtualenv-root "/env")
  2388. (python-shell-unbuffered t)
  2389. (python-shell-extra-pythonpaths'("/path1" "/path2"))
  2390. (original-process-environment
  2391. (copy-sequence tramp-remote-process-environment)))
  2392. (python-shell-calculate-process-environment)
  2393. (should (equal tramp-remote-process-environment original-process-environment))))
  2394. (ert-deftest python-shell-calculate-exec-path-1 ()
  2395. "Test `python-shell-exec-path' modification."
  2396. (let* ((exec-path '("/path0"))
  2397. (python-shell-exec-path '("/path1" "/path2"))
  2398. (new-exec-path (python-shell-calculate-exec-path)))
  2399. (should (equal new-exec-path '("/path1" "/path2" "/path0")))))
  2400. (ert-deftest python-shell-calculate-exec-path-2 ()
  2401. "Test `python-shell-virtualenv-root' modification."
  2402. (let* ((exec-path '("/path0"))
  2403. (python-shell-virtualenv-root "/env")
  2404. (new-exec-path (python-shell-calculate-exec-path)))
  2405. (should (equal new-exec-path
  2406. (list (expand-file-name "/env/bin") "/path0")))))
  2407. (ert-deftest python-shell-calculate-exec-path-3 ()
  2408. "Test complete `python-shell-virtualenv-root' modification."
  2409. (let* ((exec-path '("/path0"))
  2410. (python-shell-exec-path '("/path1" "/path2"))
  2411. (python-shell-virtualenv-root "/env")
  2412. (new-exec-path (python-shell-calculate-exec-path)))
  2413. (should (equal new-exec-path
  2414. (list (expand-file-name "/env/bin")
  2415. "/path1" "/path2" "/path0")))))
  2416. (ert-deftest python-shell-calculate-exec-path-4 ()
  2417. "Test complete `python-shell-virtualenv-root' with remote."
  2418. (let* ((default-directory "/ssh::/example/dir/")
  2419. (python-shell-remote-exec-path '("/path0"))
  2420. (python-shell-exec-path '("/path1" "/path2"))
  2421. (python-shell-virtualenv-root "/env")
  2422. (new-exec-path (python-shell-calculate-exec-path)))
  2423. (should (equal new-exec-path
  2424. (list (expand-file-name "/env/bin")
  2425. "/path1" "/path2" "/path0")))))
  2426. (ert-deftest python-shell-calculate-exec-path-5 ()
  2427. "Test no side-effects on `exec-path'."
  2428. (let* ((exec-path '("/path0"))
  2429. (python-shell-exec-path '("/path1" "/path2"))
  2430. (python-shell-virtualenv-root "/env")
  2431. (original-exec-path (copy-sequence exec-path)))
  2432. (python-shell-calculate-exec-path)
  2433. (should (equal exec-path original-exec-path))))
  2434. (ert-deftest python-shell-calculate-exec-path-6 ()
  2435. "Test no side-effects on `python-shell-remote-exec-path'."
  2436. (let* ((default-directory "/ssh::/example/dir/")
  2437. (python-shell-remote-exec-path '("/path0"))
  2438. (python-shell-exec-path '("/path1" "/path2"))
  2439. (python-shell-virtualenv-root "/env")
  2440. (original-exec-path (copy-sequence python-shell-remote-exec-path)))
  2441. (python-shell-calculate-exec-path)
  2442. (should (equal python-shell-remote-exec-path original-exec-path))))
  2443. (ert-deftest python-shell-with-environment-1 ()
  2444. "Test environment with local `default-directory'."
  2445. (let* ((exec-path '("/path0"))
  2446. (python-shell-exec-path '("/path1" "/path2"))
  2447. (original-exec-path exec-path)
  2448. (python-shell-virtualenv-root "/env"))
  2449. (python-shell-with-environment
  2450. (should (equal exec-path
  2451. (list (expand-file-name "/env/bin")
  2452. "/path1" "/path2" "/path0")))
  2453. (should (not (getenv "PYTHONHOME")))
  2454. (should (string= (getenv "VIRTUAL_ENV") "/env")))
  2455. (should (equal exec-path original-exec-path))))
  2456. (ert-deftest python-shell-with-environment-2 ()
  2457. "Test environment with remote `default-directory'."
  2458. (let* ((default-directory "/ssh::/example/dir/")
  2459. (python-shell-remote-exec-path '("/remote1" "/remote2"))
  2460. (python-shell-exec-path '("/path1" "/path2"))
  2461. (tramp-remote-process-environment '("EMACS=t"))
  2462. (original-process-environment (copy-sequence tramp-remote-process-environment))
  2463. (python-shell-virtualenv-root "/env"))
  2464. (python-shell-with-environment
  2465. (should (equal (python-shell-calculate-exec-path)
  2466. (list (expand-file-name "/env/bin")
  2467. "/path1" "/path2" "/remote1" "/remote2")))
  2468. (let ((process-environment (python-shell-calculate-process-environment)))
  2469. (should (not (getenv "PYTHONHOME")))
  2470. (should (string= (getenv "VIRTUAL_ENV") "/env"))
  2471. (should (equal tramp-remote-process-environment process-environment))))
  2472. (should (equal tramp-remote-process-environment original-process-environment))))
  2473. (ert-deftest python-shell-with-environment-3 ()
  2474. "Test `python-shell-with-environment' is idempotent."
  2475. (let* ((python-shell-extra-pythonpaths '("/example/dir/"))
  2476. (python-shell-exec-path '("path1" "path2"))
  2477. (python-shell-virtualenv-root "/home/user/env")
  2478. (single-call
  2479. (python-shell-with-environment
  2480. (list exec-path process-environment)))
  2481. (nested-call
  2482. (python-shell-with-environment
  2483. (python-shell-with-environment
  2484. (list exec-path process-environment)))))
  2485. (should (equal single-call nested-call))))
  2486. (ert-deftest python-shell-make-comint-1 ()
  2487. "Check comint creation for global shell buffer."
  2488. (skip-unless (executable-find python-tests-shell-interpreter))
  2489. ;; The interpreter can get killed too quickly to allow it to clean
  2490. ;; up the tempfiles that the default python-shell-setup-codes create,
  2491. ;; so it leaves tempfiles behind, which is a minor irritation.
  2492. (let* ((python-shell-setup-codes nil)
  2493. (python-shell-interpreter
  2494. (executable-find python-tests-shell-interpreter))
  2495. (proc-name (python-shell-get-process-name nil))
  2496. (shell-buffer
  2497. (python-tests-with-temp-buffer
  2498. "" (python-shell-make-comint
  2499. (python-shell-calculate-command) proc-name)))
  2500. (process (get-buffer-process shell-buffer)))
  2501. (unwind-protect
  2502. (progn
  2503. (set-process-query-on-exit-flag process nil)
  2504. (should (process-live-p process))
  2505. (with-current-buffer shell-buffer
  2506. (should (eq major-mode 'inferior-python-mode))
  2507. (should (string= (buffer-name) (format "*%s*" proc-name)))))
  2508. (kill-buffer shell-buffer))))
  2509. (ert-deftest python-shell-make-comint-2 ()
  2510. "Check comint creation for internal shell buffer."
  2511. (skip-unless (executable-find python-tests-shell-interpreter))
  2512. (let* ((python-shell-setup-codes nil)
  2513. (python-shell-interpreter
  2514. (executable-find python-tests-shell-interpreter))
  2515. (proc-name (python-shell-internal-get-process-name))
  2516. (shell-buffer
  2517. (python-tests-with-temp-buffer
  2518. "" (python-shell-make-comint
  2519. (python-shell-calculate-command) proc-name nil t)))
  2520. (process (get-buffer-process shell-buffer)))
  2521. (unwind-protect
  2522. (progn
  2523. (set-process-query-on-exit-flag process nil)
  2524. (should (process-live-p process))
  2525. (with-current-buffer shell-buffer
  2526. (should (eq major-mode 'inferior-python-mode))
  2527. (should (string= (buffer-name) (format " *%s*" proc-name)))))
  2528. (kill-buffer shell-buffer))))
  2529. (ert-deftest python-shell-make-comint-3 ()
  2530. "Check comint creation with overridden python interpreter and args.
  2531. The command passed to `python-shell-make-comint' as argument must
  2532. locally override global values set in `python-shell-interpreter'
  2533. and `python-shell-interpreter-args' in the new shell buffer."
  2534. (skip-unless (executable-find python-tests-shell-interpreter))
  2535. (let* ((python-shell-setup-codes nil)
  2536. (python-shell-interpreter "interpreter")
  2537. (python-shell-interpreter-args "--some-args")
  2538. (proc-name (python-shell-get-process-name nil))
  2539. (interpreter-override
  2540. (concat (executable-find python-tests-shell-interpreter) " " "-i"))
  2541. (shell-buffer
  2542. (python-tests-with-temp-buffer
  2543. "" (python-shell-make-comint interpreter-override proc-name nil)))
  2544. (process (get-buffer-process shell-buffer)))
  2545. (unwind-protect
  2546. (progn
  2547. (set-process-query-on-exit-flag process nil)
  2548. (should (process-live-p process))
  2549. (with-current-buffer shell-buffer
  2550. (should (eq major-mode 'inferior-python-mode))
  2551. (should (file-equal-p
  2552. python-shell-interpreter
  2553. (executable-find python-tests-shell-interpreter)))
  2554. (should (string= python-shell-interpreter-args "-i"))))
  2555. (kill-buffer shell-buffer))))
  2556. (ert-deftest python-shell-make-comint-4 ()
  2557. "Check shell calculated prompts regexps are set."
  2558. (skip-unless (executable-find python-tests-shell-interpreter))
  2559. (let* ((process-environment process-environment)
  2560. (python-shell-setup-codes nil)
  2561. (python-shell-interpreter
  2562. (executable-find python-tests-shell-interpreter))
  2563. (python-shell-interpreter-args "-i")
  2564. (python-shell--prompt-calculated-input-regexp nil)
  2565. (python-shell--prompt-calculated-output-regexp nil)
  2566. (python-shell-prompt-detect-enabled t)
  2567. (python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
  2568. (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
  2569. (python-shell-prompt-regexp "in")
  2570. (python-shell-prompt-block-regexp "block")
  2571. (python-shell-prompt-pdb-regexp "pdf")
  2572. (python-shell-prompt-output-regexp "output")
  2573. (startup-code (concat "import sys\n"
  2574. "sys.ps1 = 'py> '\n"
  2575. "sys.ps2 = '..> '\n"
  2576. "sys.ps3 = 'out '\n"))
  2577. (startup-file (python-shell--save-temp-file startup-code))
  2578. (proc-name (python-shell-get-process-name nil))
  2579. (shell-buffer
  2580. (progn
  2581. (setenv "PYTHONSTARTUP" startup-file)
  2582. (python-tests-with-temp-buffer
  2583. "" (python-shell-make-comint
  2584. (python-shell-calculate-command) proc-name nil))))
  2585. (process (get-buffer-process shell-buffer)))
  2586. (unwind-protect
  2587. (progn
  2588. (set-process-query-on-exit-flag process nil)
  2589. (should (process-live-p process))
  2590. (with-current-buffer shell-buffer
  2591. (should (eq major-mode 'inferior-python-mode))
  2592. (should (string=
  2593. python-shell--prompt-calculated-input-regexp
  2594. (concat "^\\(extralargeinputprompt\\|\\.\\.> \\|"
  2595. "block\\|py> \\|pdf\\|sml\\|in\\)")))
  2596. (should (string=
  2597. python-shell--prompt-calculated-output-regexp
  2598. "^\\(extralargeoutputprompt\\|output\\|out \\|sml\\)"))))
  2599. (delete-file startup-file)
  2600. (kill-buffer shell-buffer))))
  2601. (ert-deftest python-shell-get-process-1 ()
  2602. "Check dedicated shell process preference over global."
  2603. (skip-unless (executable-find python-tests-shell-interpreter))
  2604. (python-tests-with-temp-file
  2605. ""
  2606. (let* ((python-shell-setup-codes nil)
  2607. (python-shell-interpreter
  2608. (executable-find python-tests-shell-interpreter))
  2609. (global-proc-name (python-shell-get-process-name nil))
  2610. (dedicated-proc-name (python-shell-get-process-name t))
  2611. (global-shell-buffer
  2612. (python-shell-make-comint
  2613. (python-shell-calculate-command) global-proc-name))
  2614. (dedicated-shell-buffer
  2615. (python-shell-make-comint
  2616. (python-shell-calculate-command) dedicated-proc-name))
  2617. (global-process (get-buffer-process global-shell-buffer))
  2618. (dedicated-process (get-buffer-process dedicated-shell-buffer)))
  2619. (unwind-protect
  2620. (progn
  2621. (set-process-query-on-exit-flag global-process nil)
  2622. (set-process-query-on-exit-flag dedicated-process nil)
  2623. ;; Prefer dedicated if global also exists.
  2624. (should (equal (python-shell-get-process) dedicated-process))
  2625. (kill-buffer dedicated-shell-buffer)
  2626. ;; If there's only global, use it.
  2627. (should (equal (python-shell-get-process) global-process))
  2628. (kill-buffer global-shell-buffer)
  2629. ;; No buffer available.
  2630. (should (not (python-shell-get-process))))
  2631. (ignore-errors (kill-buffer global-shell-buffer))
  2632. (ignore-errors (kill-buffer dedicated-shell-buffer))))))
  2633. (ert-deftest python-shell-internal-get-or-create-process-1 ()
  2634. "Check internal shell process creation fallback."
  2635. (skip-unless (executable-find python-tests-shell-interpreter))
  2636. (python-tests-with-temp-file
  2637. ""
  2638. (should (not (process-live-p (python-shell-internal-get-process-name))))
  2639. (let* ((python-shell-interpreter
  2640. (executable-find python-tests-shell-interpreter))
  2641. (internal-process-name (python-shell-internal-get-process-name))
  2642. (internal-process (python-shell-internal-get-or-create-process))
  2643. (internal-shell-buffer (process-buffer internal-process)))
  2644. (unwind-protect
  2645. (progn
  2646. (set-process-query-on-exit-flag internal-process nil)
  2647. (should (equal (process-name internal-process)
  2648. internal-process-name))
  2649. (should (equal internal-process
  2650. (python-shell-internal-get-or-create-process)))
  2651. ;; Assert the internal process is not a user process
  2652. (should (not (python-shell-get-process)))
  2653. (kill-buffer internal-shell-buffer))
  2654. (ignore-errors (kill-buffer internal-shell-buffer))))))
  2655. (ert-deftest python-shell-prompt-detect-1 ()
  2656. "Check prompt autodetection."
  2657. (skip-unless (executable-find python-tests-shell-interpreter))
  2658. (let ((process-environment process-environment))
  2659. ;; Ensure no startup file is enabled
  2660. (setenv "PYTHONSTARTUP" "")
  2661. (should python-shell-prompt-detect-enabled)
  2662. (should (equal (python-shell-prompt-detect) '(">>> " "... " "")))))
  2663. (ert-deftest python-shell-prompt-detect-2 ()
  2664. "Check prompt autodetection with startup file. Bug#17370."
  2665. (skip-unless (executable-find python-tests-shell-interpreter))
  2666. (let* ((process-environment process-environment)
  2667. (startup-code (concat "import sys\n"
  2668. "sys.ps1 = 'py> '\n"
  2669. "sys.ps2 = '..> '\n"
  2670. "sys.ps3 = 'out '\n"))
  2671. (startup-file (python-shell--save-temp-file startup-code)))
  2672. (unwind-protect
  2673. (progn
  2674. ;; Ensure startup file is enabled
  2675. (setenv "PYTHONSTARTUP" startup-file)
  2676. (should python-shell-prompt-detect-enabled)
  2677. (should (equal (python-shell-prompt-detect) '("py> " "..> " "out "))))
  2678. (ignore-errors (delete-file startup-file)))))
  2679. (ert-deftest python-shell-prompt-detect-3 ()
  2680. "Check prompts are not autodetected when feature is disabled."
  2681. (skip-unless (executable-find python-tests-shell-interpreter))
  2682. (let ((process-environment process-environment)
  2683. (python-shell-prompt-detect-enabled nil))
  2684. ;; Ensure no startup file is enabled
  2685. (should (not python-shell-prompt-detect-enabled))
  2686. (should (not (python-shell-prompt-detect)))))
  2687. (ert-deftest python-shell-prompt-detect-4 ()
  2688. "Check warning is shown when detection fails."
  2689. (skip-unless (executable-find python-tests-shell-interpreter))
  2690. (let* ((process-environment process-environment)
  2691. ;; Trigger failure by removing prompts in the startup file
  2692. (startup-code (concat "import sys\n"
  2693. "sys.ps1 = ''\n"
  2694. "sys.ps2 = ''\n"
  2695. "sys.ps3 = ''\n"))
  2696. (startup-file (python-shell--save-temp-file startup-code)))
  2697. (unwind-protect
  2698. (progn
  2699. (kill-buffer (get-buffer-create "*Warnings*"))
  2700. (should (not (get-buffer "*Warnings*")))
  2701. (setenv "PYTHONSTARTUP" startup-file)
  2702. (should python-shell-prompt-detect-failure-warning)
  2703. (should python-shell-prompt-detect-enabled)
  2704. (should (not (python-shell-prompt-detect)))
  2705. (should (get-buffer "*Warnings*")))
  2706. (ignore-errors (delete-file startup-file)))))
  2707. (ert-deftest python-shell-prompt-detect-5 ()
  2708. "Check disabled warnings are not shown when detection fails."
  2709. (skip-unless (executable-find python-tests-shell-interpreter))
  2710. (let* ((process-environment process-environment)
  2711. (startup-code (concat "import sys\n"
  2712. "sys.ps1 = ''\n"
  2713. "sys.ps2 = ''\n"
  2714. "sys.ps3 = ''\n"))
  2715. (startup-file (python-shell--save-temp-file startup-code))
  2716. (python-shell-prompt-detect-failure-warning nil))
  2717. (unwind-protect
  2718. (progn
  2719. (kill-buffer (get-buffer-create "*Warnings*"))
  2720. (should (not (get-buffer "*Warnings*")))
  2721. (setenv "PYTHONSTARTUP" startup-file)
  2722. (should (not python-shell-prompt-detect-failure-warning))
  2723. (should python-shell-prompt-detect-enabled)
  2724. (should (not (python-shell-prompt-detect)))
  2725. (should (not (get-buffer "*Warnings*"))))
  2726. (ignore-errors (delete-file startup-file)))))
  2727. (ert-deftest python-shell-prompt-detect-6 ()
  2728. "Warnings are not shown when detection is disabled."
  2729. (skip-unless (executable-find python-tests-shell-interpreter))
  2730. (let* ((process-environment process-environment)
  2731. (startup-code (concat "import sys\n"
  2732. "sys.ps1 = ''\n"
  2733. "sys.ps2 = ''\n"
  2734. "sys.ps3 = ''\n"))
  2735. (startup-file (python-shell--save-temp-file startup-code))
  2736. (python-shell-prompt-detect-failure-warning t)
  2737. (python-shell-prompt-detect-enabled nil))
  2738. (unwind-protect
  2739. (progn
  2740. (kill-buffer (get-buffer-create "*Warnings*"))
  2741. (should (not (get-buffer "*Warnings*")))
  2742. (setenv "PYTHONSTARTUP" startup-file)
  2743. (should python-shell-prompt-detect-failure-warning)
  2744. (should (not python-shell-prompt-detect-enabled))
  2745. (should (not (python-shell-prompt-detect)))
  2746. (should (not (get-buffer "*Warnings*"))))
  2747. (ignore-errors (delete-file startup-file)))))
  2748. (ert-deftest python-shell-prompt-validate-regexps-1 ()
  2749. "Check `python-shell-prompt-input-regexps' are validated."
  2750. (let* ((python-shell-prompt-input-regexps '("\\("))
  2751. (error-data (should-error (python-shell-prompt-validate-regexps)
  2752. :type 'user-error)))
  2753. (should
  2754. (string= (cadr error-data)
  2755. (format-message
  2756. "Invalid regexp \\( in `python-shell-prompt-input-regexps'")))))
  2757. (ert-deftest python-shell-prompt-validate-regexps-2 ()
  2758. "Check `python-shell-prompt-output-regexps' are validated."
  2759. (let* ((python-shell-prompt-output-regexps '("\\("))
  2760. (error-data (should-error (python-shell-prompt-validate-regexps)
  2761. :type 'user-error)))
  2762. (should
  2763. (string= (cadr error-data)
  2764. (format-message
  2765. "Invalid regexp \\( in `python-shell-prompt-output-regexps'")))))
  2766. (ert-deftest python-shell-prompt-validate-regexps-3 ()
  2767. "Check `python-shell-prompt-regexp' is validated."
  2768. (let* ((python-shell-prompt-regexp "\\(")
  2769. (error-data (should-error (python-shell-prompt-validate-regexps)
  2770. :type 'user-error)))
  2771. (should
  2772. (string= (cadr error-data)
  2773. (format-message
  2774. "Invalid regexp \\( in `python-shell-prompt-regexp'")))))
  2775. (ert-deftest python-shell-prompt-validate-regexps-4 ()
  2776. "Check `python-shell-prompt-block-regexp' is validated."
  2777. (let* ((python-shell-prompt-block-regexp "\\(")
  2778. (error-data (should-error (python-shell-prompt-validate-regexps)
  2779. :type 'user-error)))
  2780. (should
  2781. (string= (cadr error-data)
  2782. (format-message
  2783. "Invalid regexp \\( in `python-shell-prompt-block-regexp'")))))
  2784. (ert-deftest python-shell-prompt-validate-regexps-5 ()
  2785. "Check `python-shell-prompt-pdb-regexp' is validated."
  2786. (let* ((python-shell-prompt-pdb-regexp "\\(")
  2787. (error-data (should-error (python-shell-prompt-validate-regexps)
  2788. :type 'user-error)))
  2789. (should
  2790. (string= (cadr error-data)
  2791. (format-message
  2792. "Invalid regexp \\( in `python-shell-prompt-pdb-regexp'")))))
  2793. (ert-deftest python-shell-prompt-validate-regexps-6 ()
  2794. "Check `python-shell-prompt-output-regexp' is validated."
  2795. (let* ((python-shell-prompt-output-regexp "\\(")
  2796. (error-data (should-error (python-shell-prompt-validate-regexps)
  2797. :type 'user-error)))
  2798. (should
  2799. (string= (cadr error-data)
  2800. (format-message
  2801. "Invalid regexp \\( in `python-shell-prompt-output-regexp'")))))
  2802. (ert-deftest python-shell-prompt-validate-regexps-7 ()
  2803. "Check default regexps are valid."
  2804. ;; should not signal error
  2805. (python-shell-prompt-validate-regexps))
  2806. (ert-deftest python-shell-prompt-set-calculated-regexps-1 ()
  2807. "Check regexps are validated."
  2808. (let* ((python-shell-prompt-output-regexp '("\\("))
  2809. (python-shell--prompt-calculated-input-regexp nil)
  2810. (python-shell--prompt-calculated-output-regexp nil)
  2811. (python-shell-prompt-detect-enabled nil)
  2812. (error-data (should-error (python-shell-prompt-set-calculated-regexps)
  2813. :type 'user-error)))
  2814. (should
  2815. (string= (cadr error-data)
  2816. (format-message
  2817. "Invalid regexp \\( in `python-shell-prompt-output-regexp'")))))
  2818. (ert-deftest python-shell-prompt-set-calculated-regexps-2 ()
  2819. "Check `python-shell-prompt-input-regexps' are set."
  2820. (let* ((python-shell-prompt-input-regexps '("my" "prompt"))
  2821. (python-shell-prompt-output-regexps '(""))
  2822. (python-shell-prompt-regexp "")
  2823. (python-shell-prompt-block-regexp "")
  2824. (python-shell-prompt-pdb-regexp "")
  2825. (python-shell-prompt-output-regexp "")
  2826. (python-shell--prompt-calculated-input-regexp nil)
  2827. (python-shell--prompt-calculated-output-regexp nil)
  2828. (python-shell-prompt-detect-enabled nil))
  2829. (python-shell-prompt-set-calculated-regexps)
  2830. (should (string= python-shell--prompt-calculated-input-regexp
  2831. "^\\(prompt\\|my\\|\\)"))))
  2832. (ert-deftest python-shell-prompt-set-calculated-regexps-3 ()
  2833. "Check `python-shell-prompt-output-regexps' are set."
  2834. (let* ((python-shell-prompt-input-regexps '(""))
  2835. (python-shell-prompt-output-regexps '("my" "prompt"))
  2836. (python-shell-prompt-regexp "")
  2837. (python-shell-prompt-block-regexp "")
  2838. (python-shell-prompt-pdb-regexp "")
  2839. (python-shell-prompt-output-regexp "")
  2840. (python-shell--prompt-calculated-input-regexp nil)
  2841. (python-shell--prompt-calculated-output-regexp nil)
  2842. (python-shell-prompt-detect-enabled nil))
  2843. (python-shell-prompt-set-calculated-regexps)
  2844. (should (string= python-shell--prompt-calculated-output-regexp
  2845. "^\\(prompt\\|my\\|\\)"))))
  2846. (ert-deftest python-shell-prompt-set-calculated-regexps-4 ()
  2847. "Check user defined prompts are set."
  2848. (let* ((python-shell-prompt-input-regexps '(""))
  2849. (python-shell-prompt-output-regexps '(""))
  2850. (python-shell-prompt-regexp "prompt")
  2851. (python-shell-prompt-block-regexp "block")
  2852. (python-shell-prompt-pdb-regexp "pdb")
  2853. (python-shell-prompt-output-regexp "output")
  2854. (python-shell--prompt-calculated-input-regexp nil)
  2855. (python-shell--prompt-calculated-output-regexp nil)
  2856. (python-shell-prompt-detect-enabled nil))
  2857. (python-shell-prompt-set-calculated-regexps)
  2858. (should (string= python-shell--prompt-calculated-input-regexp
  2859. "^\\(prompt\\|block\\|pdb\\|\\)"))
  2860. (should (string= python-shell--prompt-calculated-output-regexp
  2861. "^\\(output\\|\\)"))))
  2862. (ert-deftest python-shell-prompt-set-calculated-regexps-5 ()
  2863. "Check order of regexps (larger first)."
  2864. (let* ((python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
  2865. (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
  2866. (python-shell-prompt-regexp "in")
  2867. (python-shell-prompt-block-regexp "block")
  2868. (python-shell-prompt-pdb-regexp "pdf")
  2869. (python-shell-prompt-output-regexp "output")
  2870. (python-shell--prompt-calculated-input-regexp nil)
  2871. (python-shell--prompt-calculated-output-regexp nil)
  2872. (python-shell-prompt-detect-enabled nil))
  2873. (python-shell-prompt-set-calculated-regexps)
  2874. (should (string= python-shell--prompt-calculated-input-regexp
  2875. "^\\(extralargeinputprompt\\|block\\|pdf\\|sml\\|in\\)"))
  2876. (should (string= python-shell--prompt-calculated-output-regexp
  2877. "^\\(extralargeoutputprompt\\|output\\|sml\\)"))))
  2878. (ert-deftest python-shell-prompt-set-calculated-regexps-6 ()
  2879. "Check detected prompts are included `regexp-quote'd."
  2880. (skip-unless (executable-find python-tests-shell-interpreter))
  2881. (let* ((python-shell-prompt-input-regexps '(""))
  2882. (python-shell-prompt-output-regexps '(""))
  2883. (python-shell-prompt-regexp "")
  2884. (python-shell-prompt-block-regexp "")
  2885. (python-shell-prompt-pdb-regexp "")
  2886. (python-shell-prompt-output-regexp "")
  2887. (python-shell--prompt-calculated-input-regexp nil)
  2888. (python-shell--prompt-calculated-output-regexp nil)
  2889. (python-shell-prompt-detect-enabled t)
  2890. (process-environment process-environment)
  2891. (startup-code (concat "import sys\n"
  2892. "sys.ps1 = 'p.> '\n"
  2893. "sys.ps2 = '..> '\n"
  2894. "sys.ps3 = 'o.t '\n"))
  2895. (startup-file (python-shell--save-temp-file startup-code)))
  2896. (unwind-protect
  2897. (progn
  2898. (setenv "PYTHONSTARTUP" startup-file)
  2899. (python-shell-prompt-set-calculated-regexps)
  2900. (should (string= python-shell--prompt-calculated-input-regexp
  2901. "^\\(\\.\\.> \\|p\\.> \\|\\)"))
  2902. (should (string= python-shell--prompt-calculated-output-regexp
  2903. "^\\(o\\.t \\|\\)")))
  2904. (ignore-errors (delete-file startup-file)))))
  2905. (ert-deftest python-shell-buffer-substring-1 ()
  2906. "Selecting a substring of the whole buffer must match its contents."
  2907. (python-tests-with-temp-buffer
  2908. "
  2909. class Foo(models.Model):
  2910. pass
  2911. class Bar(models.Model):
  2912. pass
  2913. "
  2914. (should (string= (buffer-string)
  2915. (python-shell-buffer-substring (point-min) (point-max))))))
  2916. (ert-deftest python-shell-buffer-substring-2 ()
  2917. "Main block should be removed if NOMAIN is non-nil."
  2918. (python-tests-with-temp-buffer
  2919. "
  2920. class Foo(models.Model):
  2921. pass
  2922. class Bar(models.Model):
  2923. pass
  2924. if __name__ == \"__main__\":
  2925. foo = Foo()
  2926. print (foo)
  2927. "
  2928. (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
  2929. "
  2930. class Foo(models.Model):
  2931. pass
  2932. class Bar(models.Model):
  2933. pass
  2934. "))))
  2935. (ert-deftest python-shell-buffer-substring-3 ()
  2936. "Main block should be removed if NOMAIN is non-nil."
  2937. (python-tests-with-temp-buffer
  2938. "
  2939. class Foo(models.Model):
  2940. pass
  2941. if __name__ == \"__main__\":
  2942. foo = Foo()
  2943. print (foo)
  2944. class Bar(models.Model):
  2945. pass
  2946. "
  2947. (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
  2948. "
  2949. class Foo(models.Model):
  2950. pass
  2951. class Bar(models.Model):
  2952. pass
  2953. "))))
  2954. (ert-deftest python-shell-buffer-substring-4 ()
  2955. "Coding cookie should be added for substrings."
  2956. (python-tests-with-temp-buffer
  2957. "# coding: latin-1
  2958. class Foo(models.Model):
  2959. pass
  2960. if __name__ == \"__main__\":
  2961. foo = Foo()
  2962. print (foo)
  2963. class Bar(models.Model):
  2964. pass
  2965. "
  2966. (should (string= (python-shell-buffer-substring
  2967. (python-tests-look-at "class Foo(models.Model):")
  2968. (progn (python-nav-forward-sexp) (point)))
  2969. "# -*- coding: latin-1 -*-
  2970. class Foo(models.Model):
  2971. pass"))))
  2972. (ert-deftest python-shell-buffer-substring-5 ()
  2973. "The proper amount of blank lines is added for a substring."
  2974. (python-tests-with-temp-buffer
  2975. "# coding: latin-1
  2976. class Foo(models.Model):
  2977. pass
  2978. if __name__ == \"__main__\":
  2979. foo = Foo()
  2980. print (foo)
  2981. class Bar(models.Model):
  2982. pass
  2983. "
  2984. (should (string= (python-shell-buffer-substring
  2985. (python-tests-look-at "class Bar(models.Model):")
  2986. (progn (python-nav-forward-sexp) (point)))
  2987. "# -*- coding: latin-1 -*-
  2988. class Bar(models.Model):
  2989. pass"))))
  2990. (ert-deftest python-shell-buffer-substring-6 ()
  2991. "Handle substring with coding cookie in the second line."
  2992. (python-tests-with-temp-buffer
  2993. "
  2994. # coding: latin-1
  2995. class Foo(models.Model):
  2996. pass
  2997. if __name__ == \"__main__\":
  2998. foo = Foo()
  2999. print (foo)
  3000. class Bar(models.Model):
  3001. pass
  3002. "
  3003. (should (string= (python-shell-buffer-substring
  3004. (python-tests-look-at "# coding: latin-1")
  3005. (python-tests-look-at "if __name__ == \"__main__\":"))
  3006. "# -*- coding: latin-1 -*-
  3007. class Foo(models.Model):
  3008. pass
  3009. "))))
  3010. (ert-deftest python-shell-buffer-substring-7 ()
  3011. "Ensure first coding cookie gets precedence."
  3012. (python-tests-with-temp-buffer
  3013. "# coding: utf-8
  3014. # coding: latin-1
  3015. class Foo(models.Model):
  3016. pass
  3017. if __name__ == \"__main__\":
  3018. foo = Foo()
  3019. print (foo)
  3020. class Bar(models.Model):
  3021. pass
  3022. "
  3023. (should (string= (python-shell-buffer-substring
  3024. (python-tests-look-at "# coding: latin-1")
  3025. (python-tests-look-at "if __name__ == \"__main__\":"))
  3026. "# -*- coding: utf-8 -*-
  3027. class Foo(models.Model):
  3028. pass
  3029. "))))
  3030. (ert-deftest python-shell-buffer-substring-8 ()
  3031. "Ensure first coding cookie gets precedence when sending whole buffer."
  3032. (python-tests-with-temp-buffer
  3033. "# coding: utf-8
  3034. # coding: latin-1
  3035. class Foo(models.Model):
  3036. pass
  3037. "
  3038. (should (string= (python-shell-buffer-substring (point-min) (point-max))
  3039. "# coding: utf-8
  3040. class Foo(models.Model):
  3041. pass
  3042. "))))
  3043. (ert-deftest python-shell-buffer-substring-9 ()
  3044. "Check substring starting from `point-min'."
  3045. (python-tests-with-temp-buffer
  3046. "# coding: utf-8
  3047. class Foo(models.Model):
  3048. pass
  3049. class Bar(models.Model):
  3050. pass
  3051. "
  3052. (should (string= (python-shell-buffer-substring
  3053. (point-min)
  3054. (python-tests-look-at "class Bar(models.Model):"))
  3055. "# coding: utf-8
  3056. class Foo(models.Model):
  3057. pass
  3058. "))))
  3059. (ert-deftest python-shell-buffer-substring-10 ()
  3060. "Check substring from partial block."
  3061. (python-tests-with-temp-buffer
  3062. "
  3063. def foo():
  3064. print ('a')
  3065. "
  3066. (should (string= (python-shell-buffer-substring
  3067. (python-tests-look-at "print ('a')")
  3068. (point-max))
  3069. "if True:
  3070. print ('a')
  3071. "))))
  3072. (ert-deftest python-shell-buffer-substring-11 ()
  3073. "Check substring from partial block and point within indentation."
  3074. (python-tests-with-temp-buffer
  3075. "
  3076. def foo():
  3077. print ('a')
  3078. "
  3079. (should (string= (python-shell-buffer-substring
  3080. (progn
  3081. (python-tests-look-at "print ('a')")
  3082. (backward-char 1)
  3083. (point))
  3084. (point-max))
  3085. "if True:
  3086. print ('a')
  3087. "))))
  3088. (ert-deftest python-shell-buffer-substring-12 ()
  3089. "Check substring from partial block and point in whitespace."
  3090. (python-tests-with-temp-buffer
  3091. "
  3092. def foo():
  3093. # Whitespace
  3094. print ('a')
  3095. "
  3096. (should (string= (python-shell-buffer-substring
  3097. (python-tests-look-at "# Whitespace")
  3098. (point-max))
  3099. "if True:
  3100. # Whitespace
  3101. print ('a')
  3102. "))))
  3103. ;;; Shell completion
  3104. (ert-deftest python-shell-completion-native-interpreter-disabled-p-1 ()
  3105. (let* ((python-shell-completion-native-disabled-interpreters (list "pypy"))
  3106. (python-shell-interpreter "/some/path/to/bin/pypy"))
  3107. (should (python-shell-completion-native-interpreter-disabled-p))))
  3108. ;;; PDB Track integration
  3109. ;;; Symbol completion
  3110. ;;; Fill paragraph
  3111. ;;; Skeletons
  3112. ;;; FFAP
  3113. ;;; Code check
  3114. ;;; Eldoc
  3115. (ert-deftest python-eldoc--get-symbol-at-point-1 ()
  3116. "Test paren handling."
  3117. (python-tests-with-temp-buffer
  3118. "
  3119. map(xx
  3120. map(codecs.open('somefile'
  3121. "
  3122. (python-tests-look-at "ap(xx")
  3123. (should (string= (python-eldoc--get-symbol-at-point) "map"))
  3124. (goto-char (line-end-position))
  3125. (should (string= (python-eldoc--get-symbol-at-point) "map"))
  3126. (python-tests-look-at "('somefile'")
  3127. (should (string= (python-eldoc--get-symbol-at-point) "map"))
  3128. (goto-char (line-end-position))
  3129. (should (string= (python-eldoc--get-symbol-at-point) "codecs.open"))))
  3130. (ert-deftest python-eldoc--get-symbol-at-point-2 ()
  3131. "Ensure self is replaced with the class name."
  3132. (python-tests-with-temp-buffer
  3133. "
  3134. class TheClass:
  3135. def some_method(self, n):
  3136. return n
  3137. def other(self):
  3138. return self.some_method(1234)
  3139. "
  3140. (python-tests-look-at "self.some_method")
  3141. (should (string= (python-eldoc--get-symbol-at-point)
  3142. "TheClass.some_method"))
  3143. (python-tests-look-at "1234)")
  3144. (should (string= (python-eldoc--get-symbol-at-point)
  3145. "TheClass.some_method"))))
  3146. (ert-deftest python-eldoc--get-symbol-at-point-3 ()
  3147. "Ensure symbol is found when point is at end of buffer."
  3148. (python-tests-with-temp-buffer
  3149. "
  3150. some_symbol
  3151. "
  3152. (goto-char (point-max))
  3153. (should (string= (python-eldoc--get-symbol-at-point)
  3154. "some_symbol"))))
  3155. (ert-deftest python-eldoc--get-symbol-at-point-4 ()
  3156. "Ensure symbol is found when point is at whitespace."
  3157. (python-tests-with-temp-buffer
  3158. "
  3159. some_symbol some_other_symbol
  3160. "
  3161. (python-tests-look-at " some_other_symbol")
  3162. (should (string= (python-eldoc--get-symbol-at-point)
  3163. "some_symbol"))))
  3164. ;;; Imenu
  3165. (ert-deftest python-imenu-create-index-1 ()
  3166. (python-tests-with-temp-buffer
  3167. "
  3168. class Foo(models.Model):
  3169. pass
  3170. class Bar(models.Model):
  3171. pass
  3172. def decorator(arg1, arg2, arg3):
  3173. '''print decorated function call data to stdout.
  3174. Usage:
  3175. @decorator('arg1', 'arg2')
  3176. def func(a, b, c=True):
  3177. pass
  3178. '''
  3179. def wrap(f):
  3180. print ('wrap')
  3181. def wrapped_f(*args):
  3182. print ('wrapped_f')
  3183. print ('Decorator arguments:', arg1, arg2, arg3)
  3184. f(*args)
  3185. print ('called f(*args)')
  3186. return wrapped_f
  3187. return wrap
  3188. class Baz(object):
  3189. def a(self):
  3190. pass
  3191. def b(self):
  3192. pass
  3193. class Frob(object):
  3194. def c(self):
  3195. pass
  3196. async def d(self):
  3197. pass
  3198. "
  3199. (goto-char (point-max))
  3200. (should (equal
  3201. (list
  3202. (cons "Foo (class)" (copy-marker 2))
  3203. (cons "Bar (class)" (copy-marker 38))
  3204. (list
  3205. "decorator (def)"
  3206. (cons "*function definition*" (copy-marker 74))
  3207. (list
  3208. "wrap (def)"
  3209. (cons "*function definition*" (copy-marker 254))
  3210. (cons "wrapped_f (def)" (copy-marker 294))))
  3211. (list
  3212. "Baz (class)"
  3213. (cons "*class definition*" (copy-marker 519))
  3214. (cons "a (def)" (copy-marker 539))
  3215. (cons "b (def)" (copy-marker 570))
  3216. (list
  3217. "Frob (class)"
  3218. (cons "*class definition*" (copy-marker 601))
  3219. (cons "c (def)" (copy-marker 626))
  3220. (cons "d (async def)" (copy-marker 665)))))
  3221. (python-imenu-create-index)))))
  3222. (ert-deftest python-imenu-create-index-2 ()
  3223. (python-tests-with-temp-buffer
  3224. "
  3225. class Foo(object):
  3226. def foo(self):
  3227. def foo1():
  3228. pass
  3229. def foobar(self):
  3230. pass
  3231. "
  3232. (goto-char (point-max))
  3233. (should (equal
  3234. (list
  3235. (list
  3236. "Foo (class)"
  3237. (cons "*class definition*" (copy-marker 2))
  3238. (list
  3239. "foo (def)"
  3240. (cons "*function definition*" (copy-marker 21))
  3241. (cons "foo1 (def)" (copy-marker 40)))
  3242. (cons "foobar (def)" (copy-marker 78))))
  3243. (python-imenu-create-index)))))
  3244. (ert-deftest python-imenu-create-index-3 ()
  3245. (python-tests-with-temp-buffer
  3246. "
  3247. class Foo(object):
  3248. def foo(self):
  3249. def foo1():
  3250. pass
  3251. def foo2():
  3252. pass
  3253. "
  3254. (goto-char (point-max))
  3255. (should (equal
  3256. (list
  3257. (list
  3258. "Foo (class)"
  3259. (cons "*class definition*" (copy-marker 2))
  3260. (list
  3261. "foo (def)"
  3262. (cons "*function definition*" (copy-marker 21))
  3263. (cons "foo1 (def)" (copy-marker 40))
  3264. (cons "foo2 (def)" (copy-marker 77)))))
  3265. (python-imenu-create-index)))))
  3266. (ert-deftest python-imenu-create-index-4 ()
  3267. (python-tests-with-temp-buffer
  3268. "
  3269. class Foo(object):
  3270. class Bar(object):
  3271. def __init__(self):
  3272. pass
  3273. def __str__(self):
  3274. pass
  3275. def __init__(self):
  3276. pass
  3277. "
  3278. (goto-char (point-max))
  3279. (should (equal
  3280. (list
  3281. (list
  3282. "Foo (class)"
  3283. (cons "*class definition*" (copy-marker 2))
  3284. (list
  3285. "Bar (class)"
  3286. (cons "*class definition*" (copy-marker 21))
  3287. (cons "__init__ (def)" (copy-marker 44))
  3288. (cons "__str__ (def)" (copy-marker 90)))
  3289. (cons "__init__ (def)" (copy-marker 135))))
  3290. (python-imenu-create-index)))))
  3291. (ert-deftest python-imenu-create-flat-index-1 ()
  3292. (python-tests-with-temp-buffer
  3293. "
  3294. class Foo(models.Model):
  3295. pass
  3296. class Bar(models.Model):
  3297. pass
  3298. def decorator(arg1, arg2, arg3):
  3299. '''print decorated function call data to stdout.
  3300. Usage:
  3301. @decorator('arg1', 'arg2')
  3302. def func(a, b, c=True):
  3303. pass
  3304. '''
  3305. def wrap(f):
  3306. print ('wrap')
  3307. def wrapped_f(*args):
  3308. print ('wrapped_f')
  3309. print ('Decorator arguments:', arg1, arg2, arg3)
  3310. f(*args)
  3311. print ('called f(*args)')
  3312. return wrapped_f
  3313. return wrap
  3314. class Baz(object):
  3315. def a(self):
  3316. pass
  3317. def b(self):
  3318. pass
  3319. class Frob(object):
  3320. def c(self):
  3321. pass
  3322. async def d(self):
  3323. pass
  3324. "
  3325. (goto-char (point-max))
  3326. (should (equal
  3327. (list (cons "Foo" (copy-marker 2))
  3328. (cons "Bar" (copy-marker 38))
  3329. (cons "decorator" (copy-marker 74))
  3330. (cons "decorator.wrap" (copy-marker 254))
  3331. (cons "decorator.wrap.wrapped_f" (copy-marker 294))
  3332. (cons "Baz" (copy-marker 519))
  3333. (cons "Baz.a" (copy-marker 539))
  3334. (cons "Baz.b" (copy-marker 570))
  3335. (cons "Baz.Frob" (copy-marker 601))
  3336. (cons "Baz.Frob.c" (copy-marker 626))
  3337. (cons "Baz.Frob.d" (copy-marker 665)))
  3338. (python-imenu-create-flat-index)))))
  3339. (ert-deftest python-imenu-create-flat-index-2 ()
  3340. (python-tests-with-temp-buffer
  3341. "
  3342. class Foo(object):
  3343. class Bar(object):
  3344. def __init__(self):
  3345. pass
  3346. def __str__(self):
  3347. pass
  3348. def __init__(self):
  3349. pass
  3350. "
  3351. (goto-char (point-max))
  3352. (should (equal
  3353. (list
  3354. (cons "Foo" (copy-marker 2))
  3355. (cons "Foo.Bar" (copy-marker 21))
  3356. (cons "Foo.Bar.__init__" (copy-marker 44))
  3357. (cons "Foo.Bar.__str__" (copy-marker 90))
  3358. (cons "Foo.__init__" (copy-marker 135)))
  3359. (python-imenu-create-flat-index)))))
  3360. ;;; Misc helpers
  3361. (ert-deftest python-info-current-defun-1 ()
  3362. (python-tests-with-temp-buffer
  3363. "
  3364. def foo(a, b):
  3365. "
  3366. (forward-line 1)
  3367. (should (string= "foo" (python-info-current-defun)))
  3368. (should (string= "def foo" (python-info-current-defun t)))
  3369. (forward-line 1)
  3370. (should (not (python-info-current-defun)))
  3371. (indent-for-tab-command)
  3372. (should (string= "foo" (python-info-current-defun)))
  3373. (should (string= "def foo" (python-info-current-defun t)))))
  3374. (ert-deftest python-info-current-defun-2 ()
  3375. (python-tests-with-temp-buffer
  3376. "
  3377. class C(object):
  3378. def m(self):
  3379. if True:
  3380. return [i for i in range(3)]
  3381. else:
  3382. return []
  3383. def b():
  3384. do_b()
  3385. def a():
  3386. do_a()
  3387. def c(self):
  3388. do_c()
  3389. "
  3390. (forward-line 1)
  3391. (should (string= "C" (python-info-current-defun)))
  3392. (should (string= "class C" (python-info-current-defun t)))
  3393. (python-tests-look-at "return [i for ")
  3394. (should (string= "C.m" (python-info-current-defun)))
  3395. (should (string= "def C.m" (python-info-current-defun t)))
  3396. (python-tests-look-at "def b():")
  3397. (should (string= "C.m.b" (python-info-current-defun)))
  3398. (should (string= "def C.m.b" (python-info-current-defun t)))
  3399. (forward-line 2)
  3400. (indent-for-tab-command)
  3401. (python-indent-dedent-line-backspace 1)
  3402. (should (string= "C.m" (python-info-current-defun)))
  3403. (should (string= "def C.m" (python-info-current-defun t)))
  3404. (python-tests-look-at "def c(self):")
  3405. (forward-line -1)
  3406. (indent-for-tab-command)
  3407. (should (string= "C.m.a" (python-info-current-defun)))
  3408. (should (string= "def C.m.a" (python-info-current-defun t)))
  3409. (python-indent-dedent-line-backspace 1)
  3410. (should (string= "C.m" (python-info-current-defun)))
  3411. (should (string= "def C.m" (python-info-current-defun t)))
  3412. (python-indent-dedent-line-backspace 1)
  3413. (should (string= "C" (python-info-current-defun)))
  3414. (should (string= "class C" (python-info-current-defun t)))
  3415. (python-tests-look-at "def c(self):")
  3416. (should (string= "C.c" (python-info-current-defun)))
  3417. (should (string= "def C.c" (python-info-current-defun t)))
  3418. (python-tests-look-at "do_c()")
  3419. (should (string= "C.c" (python-info-current-defun)))
  3420. (should (string= "def C.c" (python-info-current-defun t)))))
  3421. (ert-deftest python-info-current-defun-3 ()
  3422. (python-tests-with-temp-buffer
  3423. "
  3424. def decoratorFunctionWithArguments(arg1, arg2, arg3):
  3425. '''print decorated function call data to stdout.
  3426. Usage:
  3427. @decoratorFunctionWithArguments('arg1', 'arg2')
  3428. def func(a, b, c=True):
  3429. pass
  3430. '''
  3431. def wwrap(f):
  3432. print 'Inside wwrap()'
  3433. def wrapped_f(*args):
  3434. print 'Inside wrapped_f()'
  3435. print 'Decorator arguments:', arg1, arg2, arg3
  3436. f(*args)
  3437. print 'After f(*args)'
  3438. return wrapped_f
  3439. return wwrap
  3440. "
  3441. (python-tests-look-at "def wwrap(f):")
  3442. (forward-line -1)
  3443. (should (not (python-info-current-defun)))
  3444. (indent-for-tab-command 1)
  3445. (should (string= (python-info-current-defun)
  3446. "decoratorFunctionWithArguments"))
  3447. (should (string= (python-info-current-defun t)
  3448. "def decoratorFunctionWithArguments"))
  3449. (python-tests-look-at "def wrapped_f(*args):")
  3450. (should (string= (python-info-current-defun)
  3451. "decoratorFunctionWithArguments.wwrap.wrapped_f"))
  3452. (should (string= (python-info-current-defun t)
  3453. "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
  3454. (python-tests-look-at "return wrapped_f")
  3455. (should (string= (python-info-current-defun)
  3456. "decoratorFunctionWithArguments.wwrap"))
  3457. (should (string= (python-info-current-defun t)
  3458. "def decoratorFunctionWithArguments.wwrap"))
  3459. (end-of-line 1)
  3460. (python-tests-look-at "return wwrap")
  3461. (should (string= (python-info-current-defun)
  3462. "decoratorFunctionWithArguments"))
  3463. (should (string= (python-info-current-defun t)
  3464. "def decoratorFunctionWithArguments"))))
  3465. (ert-deftest python-info-current-symbol-1 ()
  3466. (python-tests-with-temp-buffer
  3467. "
  3468. class C(object):
  3469. def m(self):
  3470. self.c()
  3471. def c(self):
  3472. print ('a')
  3473. "
  3474. (python-tests-look-at "self.c()")
  3475. (should (string= "self.c" (python-info-current-symbol)))
  3476. (should (string= "C.c" (python-info-current-symbol t)))))
  3477. (ert-deftest python-info-current-symbol-2 ()
  3478. (python-tests-with-temp-buffer
  3479. "
  3480. class C(object):
  3481. class M(object):
  3482. def a(self):
  3483. self.c()
  3484. def c(self):
  3485. pass
  3486. "
  3487. (python-tests-look-at "self.c()")
  3488. (should (string= "self.c" (python-info-current-symbol)))
  3489. (should (string= "C.M.c" (python-info-current-symbol t)))))
  3490. (ert-deftest python-info-current-symbol-3 ()
  3491. "Keywords should not be considered symbols."
  3492. :expected-result :failed
  3493. (python-tests-with-temp-buffer
  3494. "
  3495. class C(object):
  3496. pass
  3497. "
  3498. ;; FIXME: keywords are not symbols.
  3499. (python-tests-look-at "class C")
  3500. (should (not (python-info-current-symbol)))
  3501. (should (not (python-info-current-symbol t)))
  3502. (python-tests-look-at "C(object)")
  3503. (should (string= "C" (python-info-current-symbol)))
  3504. (should (string= "class C" (python-info-current-symbol t)))))
  3505. (ert-deftest python-info-statement-starts-block-p-1 ()
  3506. (python-tests-with-temp-buffer
  3507. "
  3508. def long_function_name(
  3509. var_one, var_two, var_three,
  3510. var_four):
  3511. print (var_one)
  3512. "
  3513. (python-tests-look-at "def long_function_name")
  3514. (should (python-info-statement-starts-block-p))
  3515. (python-tests-look-at "print (var_one)")
  3516. (python-util-forward-comment -1)
  3517. (should (python-info-statement-starts-block-p))))
  3518. (ert-deftest python-info-statement-starts-block-p-2 ()
  3519. (python-tests-with-temp-buffer
  3520. "
  3521. if width == 0 and height == 0 and \\\\
  3522. color == 'red' and emphasis == 'strong' or \\\\
  3523. highlight > 100:
  3524. raise ValueError('sorry, you lose')
  3525. "
  3526. (python-tests-look-at "if width == 0 and")
  3527. (should (python-info-statement-starts-block-p))
  3528. (python-tests-look-at "raise ValueError(")
  3529. (python-util-forward-comment -1)
  3530. (should (python-info-statement-starts-block-p))))
  3531. (ert-deftest python-info-statement-ends-block-p-1 ()
  3532. (python-tests-with-temp-buffer
  3533. "
  3534. def long_function_name(
  3535. var_one, var_two, var_three,
  3536. var_four):
  3537. print (var_one)
  3538. "
  3539. (python-tests-look-at "print (var_one)")
  3540. (should (python-info-statement-ends-block-p))))
  3541. (ert-deftest python-info-statement-ends-block-p-2 ()
  3542. (python-tests-with-temp-buffer
  3543. "
  3544. if width == 0 and height == 0 and \\\\
  3545. color == 'red' and emphasis == 'strong' or \\\\
  3546. highlight > 100:
  3547. raise ValueError(
  3548. 'sorry, you lose'
  3549. )
  3550. "
  3551. (python-tests-look-at "raise ValueError(")
  3552. (should (python-info-statement-ends-block-p))))
  3553. (ert-deftest python-info-beginning-of-statement-p-1 ()
  3554. (python-tests-with-temp-buffer
  3555. "
  3556. def long_function_name(
  3557. var_one, var_two, var_three,
  3558. var_four):
  3559. print (var_one)
  3560. "
  3561. (python-tests-look-at "def long_function_name")
  3562. (should (python-info-beginning-of-statement-p))
  3563. (forward-char 10)
  3564. (should (not (python-info-beginning-of-statement-p)))
  3565. (python-tests-look-at "print (var_one)")
  3566. (should (python-info-beginning-of-statement-p))
  3567. (goto-char (line-beginning-position))
  3568. (should (not (python-info-beginning-of-statement-p)))))
  3569. (ert-deftest python-info-beginning-of-statement-p-2 ()
  3570. (python-tests-with-temp-buffer
  3571. "
  3572. if width == 0 and height == 0 and \\\\
  3573. color == 'red' and emphasis == 'strong' or \\\\
  3574. highlight > 100:
  3575. raise ValueError(
  3576. 'sorry, you lose'
  3577. )
  3578. "
  3579. (python-tests-look-at "if width == 0 and")
  3580. (should (python-info-beginning-of-statement-p))
  3581. (forward-char 10)
  3582. (should (not (python-info-beginning-of-statement-p)))
  3583. (python-tests-look-at "raise ValueError(")
  3584. (should (python-info-beginning-of-statement-p))
  3585. (goto-char (line-beginning-position))
  3586. (should (not (python-info-beginning-of-statement-p)))))
  3587. (ert-deftest python-info-end-of-statement-p-1 ()
  3588. (python-tests-with-temp-buffer
  3589. "
  3590. def long_function_name(
  3591. var_one, var_two, var_three,
  3592. var_four):
  3593. print (var_one)
  3594. "
  3595. (python-tests-look-at "def long_function_name")
  3596. (should (not (python-info-end-of-statement-p)))
  3597. (end-of-line)
  3598. (should (not (python-info-end-of-statement-p)))
  3599. (python-tests-look-at "print (var_one)")
  3600. (python-util-forward-comment -1)
  3601. (should (python-info-end-of-statement-p))
  3602. (python-tests-look-at "print (var_one)")
  3603. (should (not (python-info-end-of-statement-p)))
  3604. (end-of-line)
  3605. (should (python-info-end-of-statement-p))))
  3606. (ert-deftest python-info-end-of-statement-p-2 ()
  3607. (python-tests-with-temp-buffer
  3608. "
  3609. if width == 0 and height == 0 and \\\\
  3610. color == 'red' and emphasis == 'strong' or \\\\
  3611. highlight > 100:
  3612. raise ValueError(
  3613. 'sorry, you lose'
  3614. )
  3615. "
  3616. (python-tests-look-at "if width == 0 and")
  3617. (should (not (python-info-end-of-statement-p)))
  3618. (end-of-line)
  3619. (should (not (python-info-end-of-statement-p)))
  3620. (python-tests-look-at "raise ValueError(")
  3621. (python-util-forward-comment -1)
  3622. (should (python-info-end-of-statement-p))
  3623. (python-tests-look-at "raise ValueError(")
  3624. (should (not (python-info-end-of-statement-p)))
  3625. (end-of-line)
  3626. (should (not (python-info-end-of-statement-p)))
  3627. (goto-char (point-max))
  3628. (python-util-forward-comment -1)
  3629. (should (python-info-end-of-statement-p))))
  3630. (ert-deftest python-info-beginning-of-block-p-1 ()
  3631. (python-tests-with-temp-buffer
  3632. "
  3633. def long_function_name(
  3634. var_one, var_two, var_three,
  3635. var_four):
  3636. print (var_one)
  3637. "
  3638. (python-tests-look-at "def long_function_name")
  3639. (should (python-info-beginning-of-block-p))
  3640. (python-tests-look-at "var_one, var_two, var_three,")
  3641. (should (not (python-info-beginning-of-block-p)))
  3642. (python-tests-look-at "print (var_one)")
  3643. (should (not (python-info-beginning-of-block-p)))))
  3644. (ert-deftest python-info-beginning-of-block-p-2 ()
  3645. (python-tests-with-temp-buffer
  3646. "
  3647. if width == 0 and height == 0 and \\\\
  3648. color == 'red' and emphasis == 'strong' or \\\\
  3649. highlight > 100:
  3650. raise ValueError(
  3651. 'sorry, you lose'
  3652. )
  3653. "
  3654. (python-tests-look-at "if width == 0 and")
  3655. (should (python-info-beginning-of-block-p))
  3656. (python-tests-look-at "color == 'red' and emphasis")
  3657. (should (not (python-info-beginning-of-block-p)))
  3658. (python-tests-look-at "raise ValueError(")
  3659. (should (not (python-info-beginning-of-block-p)))))
  3660. (ert-deftest python-info-end-of-block-p-1 ()
  3661. (python-tests-with-temp-buffer
  3662. "
  3663. def long_function_name(
  3664. var_one, var_two, var_three,
  3665. var_four):
  3666. print (var_one)
  3667. "
  3668. (python-tests-look-at "def long_function_name")
  3669. (should (not (python-info-end-of-block-p)))
  3670. (python-tests-look-at "var_one, var_two, var_three,")
  3671. (should (not (python-info-end-of-block-p)))
  3672. (python-tests-look-at "var_four):")
  3673. (end-of-line)
  3674. (should (not (python-info-end-of-block-p)))
  3675. (python-tests-look-at "print (var_one)")
  3676. (should (not (python-info-end-of-block-p)))
  3677. (end-of-line 1)
  3678. (should (python-info-end-of-block-p))))
  3679. (ert-deftest python-info-end-of-block-p-2 ()
  3680. (python-tests-with-temp-buffer
  3681. "
  3682. if width == 0 and height == 0 and \\\\
  3683. color == 'red' and emphasis == 'strong' or \\\\
  3684. highlight > 100:
  3685. raise ValueError(
  3686. 'sorry, you lose'
  3687. )
  3688. "
  3689. (python-tests-look-at "if width == 0 and")
  3690. (should (not (python-info-end-of-block-p)))
  3691. (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
  3692. (should (not (python-info-end-of-block-p)))
  3693. (python-tests-look-at "highlight > 100:")
  3694. (end-of-line)
  3695. (should (not (python-info-end-of-block-p)))
  3696. (python-tests-look-at "raise ValueError(")
  3697. (should (not (python-info-end-of-block-p)))
  3698. (end-of-line 1)
  3699. (should (not (python-info-end-of-block-p)))
  3700. (goto-char (point-max))
  3701. (python-util-forward-comment -1)
  3702. (should (python-info-end-of-block-p))))
  3703. (ert-deftest python-info-dedenter-opening-block-position-1 ()
  3704. (python-tests-with-temp-buffer
  3705. "
  3706. if request.user.is_authenticated():
  3707. try:
  3708. profile = request.user.get_profile()
  3709. except Profile.DoesNotExist:
  3710. profile = Profile.objects.create(user=request.user)
  3711. else:
  3712. if profile.stats:
  3713. profile.recalculate_stats()
  3714. else:
  3715. profile.clear_stats()
  3716. finally:
  3717. profile.views += 1
  3718. profile.save()
  3719. "
  3720. (python-tests-look-at "try:")
  3721. (should (not (python-info-dedenter-opening-block-position)))
  3722. (python-tests-look-at "except Profile.DoesNotExist:")
  3723. (should (= (python-tests-look-at "try:" -1 t)
  3724. (python-info-dedenter-opening-block-position)))
  3725. (python-tests-look-at "else:")
  3726. (should (= (python-tests-look-at "except Profile.DoesNotExist:" -1 t)
  3727. (python-info-dedenter-opening-block-position)))
  3728. (python-tests-look-at "if profile.stats:")
  3729. (should (not (python-info-dedenter-opening-block-position)))
  3730. (python-tests-look-at "else:")
  3731. (should (= (python-tests-look-at "if profile.stats:" -1 t)
  3732. (python-info-dedenter-opening-block-position)))
  3733. (python-tests-look-at "finally:")
  3734. (should (= (python-tests-look-at "else:" -2 t)
  3735. (python-info-dedenter-opening-block-position)))))
  3736. (ert-deftest python-info-dedenter-opening-block-position-2 ()
  3737. (python-tests-with-temp-buffer
  3738. "
  3739. if request.user.is_authenticated():
  3740. profile = Profile.objects.get_or_create(user=request.user)
  3741. if profile.stats:
  3742. profile.recalculate_stats()
  3743. data = {
  3744. 'else': 'do it'
  3745. }
  3746. 'else'
  3747. "
  3748. (python-tests-look-at "'else': 'do it'")
  3749. (should (not (python-info-dedenter-opening-block-position)))
  3750. (python-tests-look-at "'else'")
  3751. (should (not (python-info-dedenter-opening-block-position)))))
  3752. (ert-deftest python-info-dedenter-opening-block-position-3 ()
  3753. (python-tests-with-temp-buffer
  3754. "
  3755. if save:
  3756. try:
  3757. write_to_disk(data)
  3758. except IOError:
  3759. msg = 'Error saving to disk'
  3760. message(msg)
  3761. logger.exception(msg)
  3762. except Exception:
  3763. if hide_details:
  3764. logger.exception('Unhandled exception')
  3765. else
  3766. finally:
  3767. data.free()
  3768. "
  3769. (python-tests-look-at "try:")
  3770. (should (not (python-info-dedenter-opening-block-position)))
  3771. (python-tests-look-at "except IOError:")
  3772. (should (= (python-tests-look-at "try:" -1 t)
  3773. (python-info-dedenter-opening-block-position)))
  3774. (python-tests-look-at "except Exception:")
  3775. (should (= (python-tests-look-at "except IOError:" -1 t)
  3776. (python-info-dedenter-opening-block-position)))
  3777. (python-tests-look-at "if hide_details:")
  3778. (should (not (python-info-dedenter-opening-block-position)))
  3779. ;; check indentation modifies the detected opening block
  3780. (python-tests-look-at "else")
  3781. (should (= (python-tests-look-at "if hide_details:" -1 t)
  3782. (python-info-dedenter-opening-block-position)))
  3783. (indent-line-to 8)
  3784. (should (= (python-tests-look-at "if hide_details:" -1 t)
  3785. (python-info-dedenter-opening-block-position)))
  3786. (indent-line-to 4)
  3787. (should (= (python-tests-look-at "except Exception:" -1 t)
  3788. (python-info-dedenter-opening-block-position)))
  3789. (indent-line-to 0)
  3790. (should (= (python-tests-look-at "if save:" -1 t)
  3791. (python-info-dedenter-opening-block-position)))))
  3792. (ert-deftest python-info-dedenter-opening-block-positions-1 ()
  3793. (python-tests-with-temp-buffer
  3794. "
  3795. if save:
  3796. try:
  3797. write_to_disk(data)
  3798. except IOError:
  3799. msg = 'Error saving to disk'
  3800. message(msg)
  3801. logger.exception(msg)
  3802. except Exception:
  3803. if hide_details:
  3804. logger.exception('Unhandled exception')
  3805. else
  3806. finally:
  3807. data.free()
  3808. "
  3809. (python-tests-look-at "try:")
  3810. (should (not (python-info-dedenter-opening-block-positions)))
  3811. (python-tests-look-at "except IOError:")
  3812. (should
  3813. (equal (list
  3814. (python-tests-look-at "try:" -1 t))
  3815. (python-info-dedenter-opening-block-positions)))
  3816. (python-tests-look-at "except Exception:")
  3817. (should
  3818. (equal (list
  3819. (python-tests-look-at "except IOError:" -1 t))
  3820. (python-info-dedenter-opening-block-positions)))
  3821. (python-tests-look-at "if hide_details:")
  3822. (should (not (python-info-dedenter-opening-block-positions)))
  3823. ;; check indentation does not modify the detected opening blocks
  3824. (python-tests-look-at "else")
  3825. (should
  3826. (equal (list
  3827. (python-tests-look-at "if hide_details:" -1 t)
  3828. (python-tests-look-at "except Exception:" -1 t)
  3829. (python-tests-look-at "if save:" -1 t))
  3830. (python-info-dedenter-opening-block-positions)))
  3831. (indent-line-to 8)
  3832. (should
  3833. (equal (list
  3834. (python-tests-look-at "if hide_details:" -1 t)
  3835. (python-tests-look-at "except Exception:" -1 t)
  3836. (python-tests-look-at "if save:" -1 t))
  3837. (python-info-dedenter-opening-block-positions)))
  3838. (indent-line-to 4)
  3839. (should
  3840. (equal (list
  3841. (python-tests-look-at "if hide_details:" -1 t)
  3842. (python-tests-look-at "except Exception:" -1 t)
  3843. (python-tests-look-at "if save:" -1 t))
  3844. (python-info-dedenter-opening-block-positions)))
  3845. (indent-line-to 0)
  3846. (should
  3847. (equal (list
  3848. (python-tests-look-at "if hide_details:" -1 t)
  3849. (python-tests-look-at "except Exception:" -1 t)
  3850. (python-tests-look-at "if save:" -1 t))
  3851. (python-info-dedenter-opening-block-positions)))))
  3852. (ert-deftest python-info-dedenter-opening-block-positions-2 ()
  3853. "Test detection of opening blocks for elif."
  3854. (python-tests-with-temp-buffer
  3855. "
  3856. if var:
  3857. if var2:
  3858. something()
  3859. elif var3:
  3860. something_else()
  3861. elif
  3862. "
  3863. (python-tests-look-at "elif var3:")
  3864. (should
  3865. (equal (list
  3866. (python-tests-look-at "if var2:" -1 t)
  3867. (python-tests-look-at "if var:" -1 t))
  3868. (python-info-dedenter-opening-block-positions)))
  3869. (python-tests-look-at "elif\n")
  3870. (should
  3871. (equal (list
  3872. (python-tests-look-at "elif var3:" -1 t)
  3873. (python-tests-look-at "if var:" -1 t))
  3874. (python-info-dedenter-opening-block-positions)))))
  3875. (ert-deftest python-info-dedenter-opening-block-positions-3 ()
  3876. "Test detection of opening blocks for else."
  3877. (python-tests-with-temp-buffer
  3878. "
  3879. try:
  3880. something()
  3881. except:
  3882. if var:
  3883. if var2:
  3884. something()
  3885. elif var3:
  3886. something_else()
  3887. else
  3888. if var4:
  3889. while var5:
  3890. var4.pop()
  3891. else
  3892. for value in var6:
  3893. if value > 0:
  3894. print value
  3895. else
  3896. "
  3897. (python-tests-look-at "else\n")
  3898. (should
  3899. (equal (list
  3900. (python-tests-look-at "elif var3:" -1 t)
  3901. (python-tests-look-at "if var:" -1 t)
  3902. (python-tests-look-at "except:" -1 t))
  3903. (python-info-dedenter-opening-block-positions)))
  3904. (python-tests-look-at "else\n")
  3905. (should
  3906. (equal (list
  3907. (python-tests-look-at "while var5:" -1 t)
  3908. (python-tests-look-at "if var4:" -1 t))
  3909. (python-info-dedenter-opening-block-positions)))
  3910. (python-tests-look-at "else\n")
  3911. (should
  3912. (equal (list
  3913. (python-tests-look-at "if value > 0:" -1 t)
  3914. (python-tests-look-at "for value in var6:" -1 t)
  3915. (python-tests-look-at "if var4:" -1 t))
  3916. (python-info-dedenter-opening-block-positions)))))
  3917. (ert-deftest python-info-dedenter-opening-block-positions-4 ()
  3918. "Test detection of opening blocks for except."
  3919. (python-tests-with-temp-buffer
  3920. "
  3921. try:
  3922. something()
  3923. except ValueError:
  3924. something_else()
  3925. except
  3926. "
  3927. (python-tests-look-at "except ValueError:")
  3928. (should
  3929. (equal (list (python-tests-look-at "try:" -1 t))
  3930. (python-info-dedenter-opening-block-positions)))
  3931. (python-tests-look-at "except\n")
  3932. (should
  3933. (equal (list (python-tests-look-at "except ValueError:" -1 t))
  3934. (python-info-dedenter-opening-block-positions)))))
  3935. (ert-deftest python-info-dedenter-opening-block-positions-5 ()
  3936. "Test detection of opening blocks for finally."
  3937. (python-tests-with-temp-buffer
  3938. "
  3939. try:
  3940. something()
  3941. finally
  3942. try:
  3943. something_else()
  3944. except:
  3945. logger.exception('something went wrong')
  3946. finally
  3947. try:
  3948. something_else_else()
  3949. except Exception:
  3950. logger.exception('something else went wrong')
  3951. else:
  3952. print ('all good')
  3953. finally
  3954. "
  3955. (python-tests-look-at "finally\n")
  3956. (should
  3957. (equal (list (python-tests-look-at "try:" -1 t))
  3958. (python-info-dedenter-opening-block-positions)))
  3959. (python-tests-look-at "finally\n")
  3960. (should
  3961. (equal (list (python-tests-look-at "except:" -1 t))
  3962. (python-info-dedenter-opening-block-positions)))
  3963. (python-tests-look-at "finally\n")
  3964. (should
  3965. (equal (list (python-tests-look-at "else:" -1 t))
  3966. (python-info-dedenter-opening-block-positions)))))
  3967. (ert-deftest python-info-dedenter-opening-block-message-1 ()
  3968. "Test dedenters inside strings are ignored."
  3969. (python-tests-with-temp-buffer
  3970. "'''
  3971. try:
  3972. something()
  3973. except:
  3974. logger.exception('something went wrong')
  3975. '''
  3976. "
  3977. (python-tests-look-at "except\n")
  3978. (should (not (python-info-dedenter-opening-block-message)))))
  3979. (ert-deftest python-info-dedenter-opening-block-message-2 ()
  3980. "Test except keyword."
  3981. (python-tests-with-temp-buffer
  3982. "
  3983. try:
  3984. something()
  3985. except:
  3986. logger.exception('something went wrong')
  3987. "
  3988. (python-tests-look-at "except:")
  3989. (should (string=
  3990. "Closes try:"
  3991. (substring-no-properties
  3992. (python-info-dedenter-opening-block-message))))
  3993. (end-of-line)
  3994. (should (string=
  3995. "Closes try:"
  3996. (substring-no-properties
  3997. (python-info-dedenter-opening-block-message))))))
  3998. (ert-deftest python-info-dedenter-opening-block-message-3 ()
  3999. "Test else keyword."
  4000. (python-tests-with-temp-buffer
  4001. "
  4002. try:
  4003. something()
  4004. except:
  4005. logger.exception('something went wrong')
  4006. else:
  4007. logger.debug('all good')
  4008. "
  4009. (python-tests-look-at "else:")
  4010. (should (string=
  4011. "Closes except:"
  4012. (substring-no-properties
  4013. (python-info-dedenter-opening-block-message))))
  4014. (end-of-line)
  4015. (should (string=
  4016. "Closes except:"
  4017. (substring-no-properties
  4018. (python-info-dedenter-opening-block-message))))))
  4019. (ert-deftest python-info-dedenter-opening-block-message-4 ()
  4020. "Test finally keyword."
  4021. (python-tests-with-temp-buffer
  4022. "
  4023. try:
  4024. something()
  4025. except:
  4026. logger.exception('something went wrong')
  4027. else:
  4028. logger.debug('all good')
  4029. finally:
  4030. clean()
  4031. "
  4032. (python-tests-look-at "finally:")
  4033. (should (string=
  4034. "Closes else:"
  4035. (substring-no-properties
  4036. (python-info-dedenter-opening-block-message))))
  4037. (end-of-line)
  4038. (should (string=
  4039. "Closes else:"
  4040. (substring-no-properties
  4041. (python-info-dedenter-opening-block-message))))))
  4042. (ert-deftest python-info-dedenter-opening-block-message-5 ()
  4043. "Test elif keyword."
  4044. (python-tests-with-temp-buffer
  4045. "
  4046. if a:
  4047. something()
  4048. elif b:
  4049. "
  4050. (python-tests-look-at "elif b:")
  4051. (should (string=
  4052. "Closes if a:"
  4053. (substring-no-properties
  4054. (python-info-dedenter-opening-block-message))))
  4055. (end-of-line)
  4056. (should (string=
  4057. "Closes if a:"
  4058. (substring-no-properties
  4059. (python-info-dedenter-opening-block-message))))))
  4060. (ert-deftest python-info-dedenter-statement-p-1 ()
  4061. "Test dedenters inside strings are ignored."
  4062. (python-tests-with-temp-buffer
  4063. "'''
  4064. try:
  4065. something()
  4066. except:
  4067. logger.exception('something went wrong')
  4068. '''
  4069. "
  4070. (python-tests-look-at "except\n")
  4071. (should (not (python-info-dedenter-statement-p)))))
  4072. (ert-deftest python-info-dedenter-statement-p-2 ()
  4073. "Test except keyword."
  4074. (python-tests-with-temp-buffer
  4075. "
  4076. try:
  4077. something()
  4078. except:
  4079. logger.exception('something went wrong')
  4080. "
  4081. (python-tests-look-at "except:")
  4082. (should (= (point) (python-info-dedenter-statement-p)))
  4083. (end-of-line)
  4084. (should (= (save-excursion
  4085. (back-to-indentation)
  4086. (point))
  4087. (python-info-dedenter-statement-p)))))
  4088. (ert-deftest python-info-dedenter-statement-p-3 ()
  4089. "Test else keyword."
  4090. (python-tests-with-temp-buffer
  4091. "
  4092. try:
  4093. something()
  4094. except:
  4095. logger.exception('something went wrong')
  4096. else:
  4097. logger.debug('all good')
  4098. "
  4099. (python-tests-look-at "else:")
  4100. (should (= (point) (python-info-dedenter-statement-p)))
  4101. (end-of-line)
  4102. (should (= (save-excursion
  4103. (back-to-indentation)
  4104. (point))
  4105. (python-info-dedenter-statement-p)))))
  4106. (ert-deftest python-info-dedenter-statement-p-4 ()
  4107. "Test finally keyword."
  4108. (python-tests-with-temp-buffer
  4109. "
  4110. try:
  4111. something()
  4112. except:
  4113. logger.exception('something went wrong')
  4114. else:
  4115. logger.debug('all good')
  4116. finally:
  4117. clean()
  4118. "
  4119. (python-tests-look-at "finally:")
  4120. (should (= (point) (python-info-dedenter-statement-p)))
  4121. (end-of-line)
  4122. (should (= (save-excursion
  4123. (back-to-indentation)
  4124. (point))
  4125. (python-info-dedenter-statement-p)))))
  4126. (ert-deftest python-info-dedenter-statement-p-5 ()
  4127. "Test elif keyword."
  4128. (python-tests-with-temp-buffer
  4129. "
  4130. if a:
  4131. something()
  4132. elif b:
  4133. "
  4134. (python-tests-look-at "elif b:")
  4135. (should (= (point) (python-info-dedenter-statement-p)))
  4136. (end-of-line)
  4137. (should (= (save-excursion
  4138. (back-to-indentation)
  4139. (point))
  4140. (python-info-dedenter-statement-p)))))
  4141. (ert-deftest python-info-line-ends-backslash-p-1 ()
  4142. (python-tests-with-temp-buffer
  4143. "
  4144. objects = Thing.objects.all() \\\\
  4145. .filter(
  4146. type='toy',
  4147. status='bought'
  4148. ) \\\\
  4149. .aggregate(
  4150. Sum('amount')
  4151. ) \\\\
  4152. .values_list()
  4153. "
  4154. (should (python-info-line-ends-backslash-p 2)) ; .filter(...
  4155. (should (python-info-line-ends-backslash-p 3))
  4156. (should (python-info-line-ends-backslash-p 4))
  4157. (should (python-info-line-ends-backslash-p 5))
  4158. (should (python-info-line-ends-backslash-p 6)) ; ) \...
  4159. (should (python-info-line-ends-backslash-p 7))
  4160. (should (python-info-line-ends-backslash-p 8))
  4161. (should (python-info-line-ends-backslash-p 9))
  4162. (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
  4163. (ert-deftest python-info-beginning-of-backslash-1 ()
  4164. (python-tests-with-temp-buffer
  4165. "
  4166. objects = Thing.objects.all() \\\\
  4167. .filter(
  4168. type='toy',
  4169. status='bought'
  4170. ) \\\\
  4171. .aggregate(
  4172. Sum('amount')
  4173. ) \\\\
  4174. .values_list()
  4175. "
  4176. (let ((first 2)
  4177. (second (python-tests-look-at ".filter("))
  4178. (third (python-tests-look-at ".aggregate(")))
  4179. (should (= first (python-info-beginning-of-backslash 2)))
  4180. (should (= second (python-info-beginning-of-backslash 3)))
  4181. (should (= second (python-info-beginning-of-backslash 4)))
  4182. (should (= second (python-info-beginning-of-backslash 5)))
  4183. (should (= second (python-info-beginning-of-backslash 6)))
  4184. (should (= third (python-info-beginning-of-backslash 7)))
  4185. (should (= third (python-info-beginning-of-backslash 8)))
  4186. (should (= third (python-info-beginning-of-backslash 9)))
  4187. (should (not (python-info-beginning-of-backslash 10))))))
  4188. (ert-deftest python-info-continuation-line-p-1 ()
  4189. (python-tests-with-temp-buffer
  4190. "
  4191. if width == 0 and height == 0 and \\\\
  4192. color == 'red' and emphasis == 'strong' or \\\\
  4193. highlight > 100:
  4194. raise ValueError(
  4195. 'sorry, you lose'
  4196. )
  4197. "
  4198. (python-tests-look-at "if width == 0 and height == 0 and")
  4199. (should (not (python-info-continuation-line-p)))
  4200. (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
  4201. (should (python-info-continuation-line-p))
  4202. (python-tests-look-at "highlight > 100:")
  4203. (should (python-info-continuation-line-p))
  4204. (python-tests-look-at "raise ValueError(")
  4205. (should (not (python-info-continuation-line-p)))
  4206. (python-tests-look-at "'sorry, you lose'")
  4207. (should (python-info-continuation-line-p))
  4208. (forward-line 1)
  4209. (should (python-info-continuation-line-p))
  4210. (python-tests-look-at ")")
  4211. (should (python-info-continuation-line-p))
  4212. (forward-line 1)
  4213. (should (not (python-info-continuation-line-p)))))
  4214. (ert-deftest python-info-block-continuation-line-p-1 ()
  4215. (python-tests-with-temp-buffer
  4216. "
  4217. if width == 0 and height == 0 and \\\\
  4218. color == 'red' and emphasis == 'strong' or \\\\
  4219. highlight > 100:
  4220. raise ValueError(
  4221. 'sorry, you lose'
  4222. )
  4223. "
  4224. (python-tests-look-at "if width == 0 and")
  4225. (should (not (python-info-block-continuation-line-p)))
  4226. (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
  4227. (should (= (python-info-block-continuation-line-p)
  4228. (python-tests-look-at "if width == 0 and" -1 t)))
  4229. (python-tests-look-at "highlight > 100:")
  4230. (should (not (python-info-block-continuation-line-p)))))
  4231. (ert-deftest python-info-block-continuation-line-p-2 ()
  4232. (python-tests-with-temp-buffer
  4233. "
  4234. def foo(a,
  4235. b,
  4236. c):
  4237. pass
  4238. "
  4239. (python-tests-look-at "def foo(a,")
  4240. (should (not (python-info-block-continuation-line-p)))
  4241. (python-tests-look-at "b,")
  4242. (should (= (python-info-block-continuation-line-p)
  4243. (python-tests-look-at "def foo(a," -1 t)))
  4244. (python-tests-look-at "c):")
  4245. (should (not (python-info-block-continuation-line-p)))))
  4246. (ert-deftest python-info-assignment-statement-p-1 ()
  4247. (python-tests-with-temp-buffer
  4248. "
  4249. data = foo(), bar() \\\\
  4250. baz(), 4 \\\\
  4251. 5, 6
  4252. "
  4253. (python-tests-look-at "data = foo(), bar()")
  4254. (should (python-info-assignment-statement-p))
  4255. (should (python-info-assignment-statement-p t))
  4256. (python-tests-look-at "baz(), 4")
  4257. (should (python-info-assignment-statement-p))
  4258. (should (not (python-info-assignment-statement-p t)))
  4259. (python-tests-look-at "5, 6")
  4260. (should (python-info-assignment-statement-p))
  4261. (should (not (python-info-assignment-statement-p t)))))
  4262. (ert-deftest python-info-assignment-statement-p-2 ()
  4263. (python-tests-with-temp-buffer
  4264. "
  4265. data = (foo(), bar()
  4266. baz(), 4
  4267. 5, 6)
  4268. "
  4269. (python-tests-look-at "data = (foo(), bar()")
  4270. (should (python-info-assignment-statement-p))
  4271. (should (python-info-assignment-statement-p t))
  4272. (python-tests-look-at "baz(), 4")
  4273. (should (python-info-assignment-statement-p))
  4274. (should (not (python-info-assignment-statement-p t)))
  4275. (python-tests-look-at "5, 6)")
  4276. (should (python-info-assignment-statement-p))
  4277. (should (not (python-info-assignment-statement-p t)))))
  4278. (ert-deftest python-info-assignment-statement-p-3 ()
  4279. (python-tests-with-temp-buffer
  4280. "
  4281. data '=' 42
  4282. "
  4283. (python-tests-look-at "data '=' 42")
  4284. (should (not (python-info-assignment-statement-p)))
  4285. (should (not (python-info-assignment-statement-p t)))))
  4286. (ert-deftest python-info-assignment-continuation-line-p-1 ()
  4287. (python-tests-with-temp-buffer
  4288. "
  4289. data = foo(), bar() \\\\
  4290. baz(), 4 \\\\
  4291. 5, 6
  4292. "
  4293. (python-tests-look-at "data = foo(), bar()")
  4294. (should (not (python-info-assignment-continuation-line-p)))
  4295. (python-tests-look-at "baz(), 4")
  4296. (should (= (python-info-assignment-continuation-line-p)
  4297. (python-tests-look-at "foo()," -1 t)))
  4298. (python-tests-look-at "5, 6")
  4299. (should (not (python-info-assignment-continuation-line-p)))))
  4300. (ert-deftest python-info-assignment-continuation-line-p-2 ()
  4301. (python-tests-with-temp-buffer
  4302. "
  4303. data = (foo(), bar()
  4304. baz(), 4
  4305. 5, 6)
  4306. "
  4307. (python-tests-look-at "data = (foo(), bar()")
  4308. (should (not (python-info-assignment-continuation-line-p)))
  4309. (python-tests-look-at "baz(), 4")
  4310. (should (= (python-info-assignment-continuation-line-p)
  4311. (python-tests-look-at "(foo()," -1 t)))
  4312. (python-tests-look-at "5, 6)")
  4313. (should (not (python-info-assignment-continuation-line-p)))))
  4314. (ert-deftest python-info-looking-at-beginning-of-defun-1 ()
  4315. (python-tests-with-temp-buffer
  4316. "
  4317. def decorat0r(deff):
  4318. '''decorates stuff.
  4319. @decorat0r
  4320. def foo(arg):
  4321. ...
  4322. '''
  4323. def wrap():
  4324. deff()
  4325. return wwrap
  4326. "
  4327. (python-tests-look-at "def decorat0r(deff):")
  4328. (should (python-info-looking-at-beginning-of-defun))
  4329. (python-tests-look-at "def foo(arg):")
  4330. (should (not (python-info-looking-at-beginning-of-defun)))
  4331. (python-tests-look-at "def wrap():")
  4332. (should (python-info-looking-at-beginning-of-defun))
  4333. (python-tests-look-at "deff()")
  4334. (should (not (python-info-looking-at-beginning-of-defun)))))
  4335. (ert-deftest python-info-current-line-comment-p-1 ()
  4336. (python-tests-with-temp-buffer
  4337. "
  4338. # this is a comment
  4339. foo = True # another comment
  4340. '#this is a string'
  4341. if foo:
  4342. # more comments
  4343. print ('bar') # print bar
  4344. "
  4345. (python-tests-look-at "# this is a comment")
  4346. (should (python-info-current-line-comment-p))
  4347. (python-tests-look-at "foo = True # another comment")
  4348. (should (not (python-info-current-line-comment-p)))
  4349. (python-tests-look-at "'#this is a string'")
  4350. (should (not (python-info-current-line-comment-p)))
  4351. (python-tests-look-at "# more comments")
  4352. (should (python-info-current-line-comment-p))
  4353. (python-tests-look-at "print ('bar') # print bar")
  4354. (should (not (python-info-current-line-comment-p)))))
  4355. (ert-deftest python-info-current-line-empty-p ()
  4356. (python-tests-with-temp-buffer
  4357. "
  4358. # this is a comment
  4359. foo = True # another comment
  4360. "
  4361. (should (python-info-current-line-empty-p))
  4362. (python-tests-look-at "# this is a comment")
  4363. (should (not (python-info-current-line-empty-p)))
  4364. (forward-line 1)
  4365. (should (python-info-current-line-empty-p))))
  4366. (ert-deftest python-info-docstring-p-1 ()
  4367. "Test module docstring detection."
  4368. (python-tests-with-temp-buffer
  4369. "# -*- coding: utf-8 -*-
  4370. #!/usr/bin/python
  4371. '''
  4372. Module Docstring Django style.
  4373. '''
  4374. u'''Additional module docstring.'''
  4375. '''Not a module docstring.'''
  4376. "
  4377. (python-tests-look-at "Module Docstring Django style.")
  4378. (should (python-info-docstring-p))
  4379. (python-tests-look-at "u'''Additional module docstring.'''")
  4380. (should (python-info-docstring-p))
  4381. (python-tests-look-at "'''Not a module docstring.'''")
  4382. (should (not (python-info-docstring-p)))))
  4383. (ert-deftest python-info-docstring-p-2 ()
  4384. "Test variable docstring detection."
  4385. (python-tests-with-temp-buffer
  4386. "
  4387. variable = 42
  4388. U'''Variable docstring.'''
  4389. '''Additional variable docstring.'''
  4390. '''Not a variable docstring.'''
  4391. "
  4392. (python-tests-look-at "Variable docstring.")
  4393. (should (python-info-docstring-p))
  4394. (python-tests-look-at "u'''Additional variable docstring.'''")
  4395. (should (python-info-docstring-p))
  4396. (python-tests-look-at "'''Not a variable docstring.'''")
  4397. (should (not (python-info-docstring-p)))))
  4398. (ert-deftest python-info-docstring-p-3 ()
  4399. "Test function docstring detection."
  4400. (python-tests-with-temp-buffer
  4401. "
  4402. def func(a, b):
  4403. r'''
  4404. Function docstring.
  4405. onetwo style.
  4406. '''
  4407. R'''Additional function docstring.'''
  4408. '''Not a function docstring.'''
  4409. return a + b
  4410. "
  4411. (python-tests-look-at "Function docstring.")
  4412. (should (python-info-docstring-p))
  4413. (python-tests-look-at "R'''Additional function docstring.'''")
  4414. (should (python-info-docstring-p))
  4415. (python-tests-look-at "'''Not a function docstring.'''")
  4416. (should (not (python-info-docstring-p)))))
  4417. (ert-deftest python-info-docstring-p-4 ()
  4418. "Test class docstring detection."
  4419. (python-tests-with-temp-buffer
  4420. "
  4421. class Class:
  4422. ur'''
  4423. Class docstring.
  4424. symmetric style.
  4425. '''
  4426. uR'''
  4427. Additional class docstring.
  4428. '''
  4429. '''Not a class docstring.'''
  4430. pass
  4431. "
  4432. (python-tests-look-at "Class docstring.")
  4433. (should (python-info-docstring-p))
  4434. (python-tests-look-at "uR'''") ;; Additional class docstring
  4435. (should (python-info-docstring-p))
  4436. (python-tests-look-at "'''Not a class docstring.'''")
  4437. (should (not (python-info-docstring-p)))))
  4438. (ert-deftest python-info-docstring-p-5 ()
  4439. "Test class attribute docstring detection."
  4440. (python-tests-with-temp-buffer
  4441. "
  4442. class Class:
  4443. attribute = 42
  4444. Ur'''
  4445. Class attribute docstring.
  4446. pep-257 style.
  4447. '''
  4448. UR'''
  4449. Additional class attribute docstring.
  4450. '''
  4451. '''Not a class attribute docstring.'''
  4452. pass
  4453. "
  4454. (python-tests-look-at "Class attribute docstring.")
  4455. (should (python-info-docstring-p))
  4456. (python-tests-look-at "UR'''") ;; Additional class attr docstring
  4457. (should (python-info-docstring-p))
  4458. (python-tests-look-at "'''Not a class attribute docstring.'''")
  4459. (should (not (python-info-docstring-p)))))
  4460. (ert-deftest python-info-docstring-p-6 ()
  4461. "Test class method docstring detection."
  4462. (python-tests-with-temp-buffer
  4463. "
  4464. class Class:
  4465. def __init__(self, a, b):
  4466. self.a = a
  4467. self.b = b
  4468. def __call__(self):
  4469. '''Method docstring.
  4470. pep-257-nn style.
  4471. '''
  4472. '''Additional method docstring.'''
  4473. '''Not a method docstring.'''
  4474. return self.a + self.b
  4475. "
  4476. (python-tests-look-at "Method docstring.")
  4477. (should (python-info-docstring-p))
  4478. (python-tests-look-at "'''Additional method docstring.'''")
  4479. (should (python-info-docstring-p))
  4480. (python-tests-look-at "'''Not a method docstring.'''")
  4481. (should (not (python-info-docstring-p)))))
  4482. (ert-deftest python-info-encoding-from-cookie-1 ()
  4483. "Should detect it on first line."
  4484. (python-tests-with-temp-buffer
  4485. "# coding=latin-1
  4486. foo = True # another comment
  4487. "
  4488. (should (eq (python-info-encoding-from-cookie) 'latin-1))))
  4489. (ert-deftest python-info-encoding-from-cookie-2 ()
  4490. "Should detect it on second line."
  4491. (python-tests-with-temp-buffer
  4492. "
  4493. # coding=latin-1
  4494. foo = True # another comment
  4495. "
  4496. (should (eq (python-info-encoding-from-cookie) 'latin-1))))
  4497. (ert-deftest python-info-encoding-from-cookie-3 ()
  4498. "Should not be detected on third line (and following ones)."
  4499. (python-tests-with-temp-buffer
  4500. "
  4501. # coding=latin-1
  4502. foo = True # another comment
  4503. "
  4504. (should (not (python-info-encoding-from-cookie)))))
  4505. (ert-deftest python-info-encoding-from-cookie-4 ()
  4506. "Should detect Emacs style."
  4507. (python-tests-with-temp-buffer
  4508. "# -*- coding: latin-1 -*-
  4509. foo = True # another comment"
  4510. (should (eq (python-info-encoding-from-cookie) 'latin-1))))
  4511. (ert-deftest python-info-encoding-from-cookie-5 ()
  4512. "Should detect Vim style."
  4513. (python-tests-with-temp-buffer
  4514. "# vim: set fileencoding=latin-1 :
  4515. foo = True # another comment"
  4516. (should (eq (python-info-encoding-from-cookie) 'latin-1))))
  4517. (ert-deftest python-info-encoding-from-cookie-6 ()
  4518. "First cookie wins."
  4519. (python-tests-with-temp-buffer
  4520. "# -*- coding: iso-8859-1 -*-
  4521. # vim: set fileencoding=latin-1 :
  4522. foo = True # another comment"
  4523. (should (eq (python-info-encoding-from-cookie) 'iso-8859-1))))
  4524. (ert-deftest python-info-encoding-from-cookie-7 ()
  4525. "First cookie wins."
  4526. (python-tests-with-temp-buffer
  4527. "# vim: set fileencoding=latin-1 :
  4528. # -*- coding: iso-8859-1 -*-
  4529. foo = True # another comment"
  4530. (should (eq (python-info-encoding-from-cookie) 'latin-1))))
  4531. (ert-deftest python-info-encoding-1 ()
  4532. "Should return the detected encoding from cookie."
  4533. (python-tests-with-temp-buffer
  4534. "# vim: set fileencoding=latin-1 :
  4535. foo = True # another comment"
  4536. (should (eq (python-info-encoding) 'latin-1))))
  4537. (ert-deftest python-info-encoding-2 ()
  4538. "Should default to utf-8."
  4539. (python-tests-with-temp-buffer
  4540. "# No encoding for you
  4541. foo = True # another comment"
  4542. (should (eq (python-info-encoding) 'utf-8))))
  4543. ;;; Utility functions
  4544. (ert-deftest python-util-goto-line-1 ()
  4545. (python-tests-with-temp-buffer
  4546. (concat
  4547. "# a comment
  4548. # another comment
  4549. def foo(a, b, c):
  4550. pass" (make-string 20 ?\n))
  4551. (python-util-goto-line 10)
  4552. (should (= (line-number-at-pos) 10))
  4553. (python-util-goto-line 20)
  4554. (should (= (line-number-at-pos) 20))))
  4555. (ert-deftest python-util-clone-local-variables-1 ()
  4556. (let ((buffer (generate-new-buffer
  4557. "python-util-clone-local-variables-1"))
  4558. (varcons
  4559. '((python-fill-docstring-style . django)
  4560. (python-shell-interpreter . "python")
  4561. (python-shell-interpreter-args . "manage.py shell")
  4562. (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
  4563. (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
  4564. (python-shell-extra-pythonpaths "/home/user/pylib/")
  4565. (python-shell-completion-setup-code
  4566. . "from IPython.core.completerlib import module_completion")
  4567. (python-shell-completion-string-code
  4568. . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
  4569. (python-shell-virtualenv-root
  4570. . "/home/user/.virtualenvs/project"))))
  4571. (with-current-buffer buffer
  4572. (kill-all-local-variables)
  4573. (dolist (ccons varcons)
  4574. (set (make-local-variable (car ccons)) (cdr ccons))))
  4575. (python-tests-with-temp-buffer
  4576. ""
  4577. (python-util-clone-local-variables buffer)
  4578. (dolist (ccons varcons)
  4579. (should
  4580. (equal (symbol-value (car ccons)) (cdr ccons)))))
  4581. (kill-buffer buffer)))
  4582. (ert-deftest python-util-strip-string-1 ()
  4583. (should (string= (python-util-strip-string "\t\r\n str") "str"))
  4584. (should (string= (python-util-strip-string "str \n\r") "str"))
  4585. (should (string= (python-util-strip-string "\t\r\n str \n\r ") "str"))
  4586. (should
  4587. (string= (python-util-strip-string "\n str \nin \tg \n\r") "str \nin \tg"))
  4588. (should (string= (python-util-strip-string "\n \t \n\r ") ""))
  4589. (should (string= (python-util-strip-string "") "")))
  4590. (ert-deftest python-util-forward-comment-1 ()
  4591. (python-tests-with-temp-buffer
  4592. (concat
  4593. "# a comment
  4594. # another comment
  4595. # bad indented comment
  4596. # more comments" (make-string 9999 ?\n))
  4597. (python-util-forward-comment 1)
  4598. (should (= (point) (point-max)))
  4599. (python-util-forward-comment -1)
  4600. (should (= (point) (point-min)))))
  4601. (ert-deftest python-util-valid-regexp-p-1 ()
  4602. (should (python-util-valid-regexp-p ""))
  4603. (should (python-util-valid-regexp-p python-shell-prompt-regexp))
  4604. (should (not (python-util-valid-regexp-p "\\("))))
  4605. ;;; Electricity
  4606. (ert-deftest python-parens-electric-indent-1 ()
  4607. (let ((eim electric-indent-mode))
  4608. (unwind-protect
  4609. (progn
  4610. (python-tests-with-temp-buffer
  4611. "
  4612. from django.conf.urls import patterns, include, url
  4613. from django.contrib import admin
  4614. from myapp import views
  4615. urlpatterns = patterns('',
  4616. url(r'^$', views.index
  4617. )
  4618. "
  4619. (electric-indent-mode 1)
  4620. (python-tests-look-at "views.index")
  4621. (end-of-line)
  4622. ;; Inserting commas within the same line should leave
  4623. ;; indentation unchanged.
  4624. (python-tests-self-insert ",")
  4625. (should (= (current-indentation) 4))
  4626. ;; As well as any other input happening within the same
  4627. ;; set of parens.
  4628. (python-tests-self-insert " name='index')")
  4629. (should (= (current-indentation) 4))
  4630. ;; But a comma outside it, should trigger indentation.
  4631. (python-tests-self-insert ",")
  4632. (should (= (current-indentation) 23))
  4633. ;; Newline indents to the first argument column
  4634. (python-tests-self-insert "\n")
  4635. (should (= (current-indentation) 23))
  4636. ;; All this input must not change indentation
  4637. (indent-line-to 4)
  4638. (python-tests-self-insert "url(r'^/login$', views.login)")
  4639. (should (= (current-indentation) 4))
  4640. ;; But this comma does
  4641. (python-tests-self-insert ",")
  4642. (should (= (current-indentation) 23))))
  4643. (or eim (electric-indent-mode -1)))))
  4644. (ert-deftest python-triple-quote-pairing ()
  4645. (let ((epm electric-pair-mode))
  4646. (unwind-protect
  4647. (progn
  4648. (python-tests-with-temp-buffer
  4649. "\"\"\n"
  4650. (or epm (electric-pair-mode 1))
  4651. (goto-char (1- (point-max)))
  4652. (python-tests-self-insert ?\")
  4653. (should (string= (buffer-string)
  4654. "\"\"\"\"\"\"\n"))
  4655. (should (= (point) 4)))
  4656. (python-tests-with-temp-buffer
  4657. "\n"
  4658. (python-tests-self-insert (list ?\" ?\" ?\"))
  4659. (should (string= (buffer-string)
  4660. "\"\"\"\"\"\"\n"))
  4661. (should (= (point) 4)))
  4662. (python-tests-with-temp-buffer
  4663. "\"\n\"\"\n"
  4664. (goto-char (1- (point-max)))
  4665. (python-tests-self-insert ?\")
  4666. (should (= (point) (1- (point-max))))
  4667. (should (string= (buffer-string)
  4668. "\"\n\"\"\"\n"))))
  4669. (or epm (electric-pair-mode -1)))))
  4670. ;;; Hideshow support
  4671. (ert-deftest python-hideshow-hide-levels-1 ()
  4672. "Should hide all methods when called after class start."
  4673. (let ((enabled hs-minor-mode))
  4674. (unwind-protect
  4675. (progn
  4676. (python-tests-with-temp-buffer
  4677. "
  4678. class SomeClass:
  4679. def __init__(self, arg, kwarg=1):
  4680. self.arg = arg
  4681. self.kwarg = kwarg
  4682. def filter(self, nums):
  4683. def fn(item):
  4684. return item in [self.arg, self.kwarg]
  4685. return filter(fn, nums)
  4686. def __str__(self):
  4687. return '%s-%s' % (self.arg, self.kwarg)
  4688. "
  4689. (hs-minor-mode 1)
  4690. (python-tests-look-at "class SomeClass:")
  4691. (forward-line)
  4692. (hs-hide-level 1)
  4693. (should
  4694. (string=
  4695. (python-tests-visible-string)
  4696. "
  4697. class SomeClass:
  4698. def __init__(self, arg, kwarg=1):
  4699. def filter(self, nums):
  4700. def __str__(self):"))))
  4701. (or enabled (hs-minor-mode -1)))))
  4702. (ert-deftest python-hideshow-hide-levels-2 ()
  4703. "Should hide nested methods and parens at end of defun."
  4704. (let ((enabled hs-minor-mode))
  4705. (unwind-protect
  4706. (progn
  4707. (python-tests-with-temp-buffer
  4708. "
  4709. class SomeClass:
  4710. def __init__(self, arg, kwarg=1):
  4711. self.arg = arg
  4712. self.kwarg = kwarg
  4713. def filter(self, nums):
  4714. def fn(item):
  4715. return item in [self.arg, self.kwarg]
  4716. return filter(fn, nums)
  4717. def __str__(self):
  4718. return '%s-%s' % (self.arg, self.kwarg)
  4719. "
  4720. (hs-minor-mode 1)
  4721. (python-tests-look-at "def fn(item):")
  4722. (hs-hide-block)
  4723. (should
  4724. (string=
  4725. (python-tests-visible-string)
  4726. "
  4727. class SomeClass:
  4728. def __init__(self, arg, kwarg=1):
  4729. self.arg = arg
  4730. self.kwarg = kwarg
  4731. def filter(self, nums):
  4732. def fn(item):
  4733. return filter(fn, nums)
  4734. def __str__(self):
  4735. return '%s-%s' % (self.arg, self.kwarg)
  4736. "))))
  4737. (or enabled (hs-minor-mode -1)))))
  4738. (provide 'python-tests)
  4739. ;; Local Variables:
  4740. ;; indent-tabs-mode: nil
  4741. ;; End:
  4742. ;;; python-tests.el ends here