python-tests.el 169 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289
  1. ;;; python-tests.el --- Test suite for python.el
  2. ;; Copyright (C) 2013-2016 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. ;;; Shell integration
  2255. (defvar python-tests-shell-interpreter "python")
  2256. (ert-deftest python-shell-get-process-name-1 ()
  2257. "Check process name calculation sans `buffer-file-name'."
  2258. (python-tests-with-temp-buffer
  2259. ""
  2260. (should (string= (python-shell-get-process-name nil)
  2261. python-shell-buffer-name))
  2262. (should (string= (python-shell-get-process-name t)
  2263. (format "%s[%s]" python-shell-buffer-name (buffer-name))))))
  2264. (ert-deftest python-shell-get-process-name-2 ()
  2265. "Check process name calculation with `buffer-file-name'."
  2266. (python-tests-with-temp-file
  2267. ""
  2268. ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
  2269. ;; should be respected.
  2270. (should (string= (python-shell-get-process-name nil)
  2271. python-shell-buffer-name))
  2272. (should (string=
  2273. (python-shell-get-process-name t)
  2274. (format "%s[%s]" python-shell-buffer-name (buffer-name))))))
  2275. (ert-deftest python-shell-internal-get-process-name-1 ()
  2276. "Check the internal process name is buffer-unique sans `buffer-file-name'."
  2277. (python-tests-with-temp-buffer
  2278. ""
  2279. (should (string= (python-shell-internal-get-process-name)
  2280. (format "%s[%s]" python-shell-internal-buffer-name (buffer-name))))))
  2281. (ert-deftest python-shell-internal-get-process-name-2 ()
  2282. "Check the internal process name is buffer-unique with `buffer-file-name'."
  2283. (python-tests-with-temp-file
  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-calculate-command-1 ()
  2288. "Check the command to execute is calculated correctly.
  2289. Using `python-shell-interpreter' and
  2290. `python-shell-interpreter-args'."
  2291. (skip-unless (executable-find python-tests-shell-interpreter))
  2292. (let ((python-shell-interpreter (executable-find
  2293. python-tests-shell-interpreter))
  2294. (python-shell-interpreter-args "-B"))
  2295. (should (string=
  2296. (format "%s %s"
  2297. (shell-quote-argument python-shell-interpreter)
  2298. python-shell-interpreter-args)
  2299. (python-shell-calculate-command)))))
  2300. (ert-deftest python-shell-calculate-pythonpath-1 ()
  2301. "Test PYTHONPATH calculation."
  2302. (let ((process-environment '("PYTHONPATH=/path0"))
  2303. (python-shell-extra-pythonpaths '("/path1" "/path2")))
  2304. (should (string= (python-shell-calculate-pythonpath)
  2305. (concat "/path1" path-separator
  2306. "/path2" path-separator "/path0")))))
  2307. (ert-deftest python-shell-calculate-pythonpath-2 ()
  2308. "Test existing paths are moved to front."
  2309. (let ((process-environment
  2310. (list (concat "PYTHONPATH=/path0" path-separator "/path1")))
  2311. (python-shell-extra-pythonpaths '("/path1" "/path2")))
  2312. (should (string= (python-shell-calculate-pythonpath)
  2313. (concat "/path1" path-separator
  2314. "/path2" path-separator "/path0")))))
  2315. (ert-deftest python-shell-calculate-process-environment-1 ()
  2316. "Test `python-shell-process-environment' modification."
  2317. (let* ((python-shell-process-environment
  2318. '("TESTVAR1=value1" "TESTVAR2=value2"))
  2319. (process-environment (python-shell-calculate-process-environment)))
  2320. (should (equal (getenv "TESTVAR1") "value1"))
  2321. (should (equal (getenv "TESTVAR2") "value2"))))
  2322. (ert-deftest python-shell-calculate-process-environment-2 ()
  2323. "Test `python-shell-extra-pythonpaths' modification."
  2324. (let* ((process-environment process-environment)
  2325. (original-pythonpath (setenv "PYTHONPATH" "/path0"))
  2326. (python-shell-extra-pythonpaths '("/path1" "/path2"))
  2327. (process-environment (python-shell-calculate-process-environment)))
  2328. (should (equal (getenv "PYTHONPATH")
  2329. (concat "/path1" path-separator
  2330. "/path2" path-separator "/path0")))))
  2331. (ert-deftest python-shell-calculate-process-environment-3 ()
  2332. "Test `python-shell-virtualenv-root' modification."
  2333. (let* ((python-shell-virtualenv-root "/env")
  2334. (process-environment
  2335. (let (process-environment process-environment)
  2336. (setenv "PYTHONHOME" "/home")
  2337. (setenv "VIRTUAL_ENV")
  2338. (python-shell-calculate-process-environment))))
  2339. (should (not (getenv "PYTHONHOME")))
  2340. (should (string= (getenv "VIRTUAL_ENV") "/env"))))
  2341. (ert-deftest python-shell-calculate-process-environment-4 ()
  2342. "Test PYTHONUNBUFFERED when `python-shell-unbuffered' is non-nil."
  2343. (let* ((python-shell-unbuffered t)
  2344. (process-environment
  2345. (let ((process-environment process-environment))
  2346. (setenv "PYTHONUNBUFFERED")
  2347. (python-shell-calculate-process-environment))))
  2348. (should (string= (getenv "PYTHONUNBUFFERED") "1"))))
  2349. (ert-deftest python-shell-calculate-process-environment-5 ()
  2350. "Test PYTHONUNBUFFERED when `python-shell-unbuffered' is nil."
  2351. (let* ((python-shell-unbuffered nil)
  2352. (process-environment
  2353. (let ((process-environment process-environment))
  2354. (setenv "PYTHONUNBUFFERED")
  2355. (python-shell-calculate-process-environment))))
  2356. (should (not (getenv "PYTHONUNBUFFERED")))))
  2357. (ert-deftest python-shell-calculate-process-environment-6 ()
  2358. "Test PYTHONUNBUFFERED=1 when `python-shell-unbuffered' is nil."
  2359. (let* ((python-shell-unbuffered nil)
  2360. (process-environment
  2361. (let ((process-environment process-environment))
  2362. (setenv "PYTHONUNBUFFERED" "1")
  2363. (python-shell-calculate-process-environment))))
  2364. ;; User default settings must remain untouched:
  2365. (should (string= (getenv "PYTHONUNBUFFERED") "1"))))
  2366. (ert-deftest python-shell-calculate-process-environment-7 ()
  2367. "Test no side-effects on `process-environment'."
  2368. (let* ((python-shell-process-environment
  2369. '("TESTVAR1=value1" "TESTVAR2=value2"))
  2370. (python-shell-virtualenv-root "/env")
  2371. (python-shell-unbuffered t)
  2372. (python-shell-extra-pythonpaths'("/path1" "/path2"))
  2373. (original-process-environment (copy-sequence process-environment)))
  2374. (python-shell-calculate-process-environment)
  2375. (should (equal process-environment original-process-environment))))
  2376. (ert-deftest python-shell-calculate-process-environment-8 ()
  2377. "Test no side-effects on `tramp-remote-process-environment'."
  2378. (let* ((default-directory "/ssh::/example/dir/")
  2379. (python-shell-process-environment
  2380. '("TESTVAR1=value1" "TESTVAR2=value2"))
  2381. (python-shell-virtualenv-root "/env")
  2382. (python-shell-unbuffered t)
  2383. (python-shell-extra-pythonpaths'("/path1" "/path2"))
  2384. (original-process-environment
  2385. (copy-sequence tramp-remote-process-environment)))
  2386. (python-shell-calculate-process-environment)
  2387. (should (equal tramp-remote-process-environment original-process-environment))))
  2388. (ert-deftest python-shell-calculate-exec-path-1 ()
  2389. "Test `python-shell-exec-path' modification."
  2390. (let* ((exec-path '("/path0"))
  2391. (python-shell-exec-path '("/path1" "/path2"))
  2392. (new-exec-path (python-shell-calculate-exec-path)))
  2393. (should (equal new-exec-path '("/path1" "/path2" "/path0")))))
  2394. (ert-deftest python-shell-calculate-exec-path-2 ()
  2395. "Test `python-shell-virtualenv-root' modification."
  2396. (let* ((exec-path '("/path0"))
  2397. (python-shell-virtualenv-root "/env")
  2398. (new-exec-path (python-shell-calculate-exec-path)))
  2399. (should (equal new-exec-path
  2400. (list (expand-file-name "/env/bin") "/path0")))))
  2401. (ert-deftest python-shell-calculate-exec-path-3 ()
  2402. "Test complete `python-shell-virtualenv-root' modification."
  2403. (let* ((exec-path '("/path0"))
  2404. (python-shell-exec-path '("/path1" "/path2"))
  2405. (python-shell-virtualenv-root "/env")
  2406. (new-exec-path (python-shell-calculate-exec-path)))
  2407. (should (equal new-exec-path
  2408. (list (expand-file-name "/env/bin")
  2409. "/path1" "/path2" "/path0")))))
  2410. (ert-deftest python-shell-calculate-exec-path-4 ()
  2411. "Test complete `python-shell-virtualenv-root' with remote."
  2412. (let* ((default-directory "/ssh::/example/dir/")
  2413. (python-shell-remote-exec-path '("/path0"))
  2414. (python-shell-exec-path '("/path1" "/path2"))
  2415. (python-shell-virtualenv-root "/env")
  2416. (new-exec-path (python-shell-calculate-exec-path)))
  2417. (should (equal new-exec-path
  2418. (list (expand-file-name "/env/bin")
  2419. "/path1" "/path2" "/path0")))))
  2420. (ert-deftest python-shell-calculate-exec-path-5 ()
  2421. "Test no side-effects on `exec-path'."
  2422. (let* ((exec-path '("/path0"))
  2423. (python-shell-exec-path '("/path1" "/path2"))
  2424. (python-shell-virtualenv-root "/env")
  2425. (original-exec-path (copy-sequence exec-path)))
  2426. (python-shell-calculate-exec-path)
  2427. (should (equal exec-path original-exec-path))))
  2428. (ert-deftest python-shell-calculate-exec-path-6 ()
  2429. "Test no side-effects on `python-shell-remote-exec-path'."
  2430. (let* ((default-directory "/ssh::/example/dir/")
  2431. (python-shell-remote-exec-path '("/path0"))
  2432. (python-shell-exec-path '("/path1" "/path2"))
  2433. (python-shell-virtualenv-root "/env")
  2434. (original-exec-path (copy-sequence python-shell-remote-exec-path)))
  2435. (python-shell-calculate-exec-path)
  2436. (should (equal python-shell-remote-exec-path original-exec-path))))
  2437. (ert-deftest python-shell-with-environment-1 ()
  2438. "Test environment with local `default-directory'."
  2439. (let* ((exec-path '("/path0"))
  2440. (python-shell-exec-path '("/path1" "/path2"))
  2441. (original-exec-path exec-path)
  2442. (python-shell-virtualenv-root "/env"))
  2443. (python-shell-with-environment
  2444. (should (equal exec-path
  2445. (list (expand-file-name "/env/bin")
  2446. "/path1" "/path2" "/path0")))
  2447. (should (not (getenv "PYTHONHOME")))
  2448. (should (string= (getenv "VIRTUAL_ENV") "/env")))
  2449. (should (equal exec-path original-exec-path))))
  2450. (ert-deftest python-shell-with-environment-2 ()
  2451. "Test environment with remote `default-directory'."
  2452. (let* ((default-directory "/ssh::/example/dir/")
  2453. (python-shell-remote-exec-path '("/remote1" "/remote2"))
  2454. (python-shell-exec-path '("/path1" "/path2"))
  2455. (tramp-remote-process-environment '("EMACS=t"))
  2456. (original-process-environment (copy-sequence tramp-remote-process-environment))
  2457. (python-shell-virtualenv-root "/env"))
  2458. (python-shell-with-environment
  2459. (should (equal (python-shell-calculate-exec-path)
  2460. (list (expand-file-name "/env/bin")
  2461. "/path1" "/path2" "/remote1" "/remote2")))
  2462. (let ((process-environment (python-shell-calculate-process-environment)))
  2463. (should (not (getenv "PYTHONHOME")))
  2464. (should (string= (getenv "VIRTUAL_ENV") "/env"))
  2465. (should (equal tramp-remote-process-environment process-environment))))
  2466. (should (equal tramp-remote-process-environment original-process-environment))))
  2467. (ert-deftest python-shell-with-environment-3 ()
  2468. "Test `python-shell-with-environment' is idempotent."
  2469. (let* ((python-shell-extra-pythonpaths '("/example/dir/"))
  2470. (python-shell-exec-path '("path1" "path2"))
  2471. (python-shell-virtualenv-root "/home/user/env")
  2472. (single-call
  2473. (python-shell-with-environment
  2474. (list exec-path process-environment)))
  2475. (nested-call
  2476. (python-shell-with-environment
  2477. (python-shell-with-environment
  2478. (list exec-path process-environment)))))
  2479. (should (equal single-call nested-call))))
  2480. (ert-deftest python-shell-make-comint-1 ()
  2481. "Check comint creation for global shell buffer."
  2482. (skip-unless (executable-find python-tests-shell-interpreter))
  2483. ;; The interpreter can get killed too quickly to allow it to clean
  2484. ;; up the tempfiles that the default python-shell-setup-codes create,
  2485. ;; so it leaves tempfiles behind, which is a minor irritation.
  2486. (let* ((python-shell-setup-codes nil)
  2487. (python-shell-interpreter
  2488. (executable-find python-tests-shell-interpreter))
  2489. (proc-name (python-shell-get-process-name nil))
  2490. (shell-buffer
  2491. (python-tests-with-temp-buffer
  2492. "" (python-shell-make-comint
  2493. (python-shell-calculate-command) proc-name)))
  2494. (process (get-buffer-process shell-buffer)))
  2495. (unwind-protect
  2496. (progn
  2497. (set-process-query-on-exit-flag process nil)
  2498. (should (process-live-p process))
  2499. (with-current-buffer shell-buffer
  2500. (should (eq major-mode 'inferior-python-mode))
  2501. (should (string= (buffer-name) (format "*%s*" proc-name)))))
  2502. (kill-buffer shell-buffer))))
  2503. (ert-deftest python-shell-make-comint-2 ()
  2504. "Check comint creation for internal shell buffer."
  2505. (skip-unless (executable-find python-tests-shell-interpreter))
  2506. (let* ((python-shell-setup-codes nil)
  2507. (python-shell-interpreter
  2508. (executable-find python-tests-shell-interpreter))
  2509. (proc-name (python-shell-internal-get-process-name))
  2510. (shell-buffer
  2511. (python-tests-with-temp-buffer
  2512. "" (python-shell-make-comint
  2513. (python-shell-calculate-command) proc-name nil t)))
  2514. (process (get-buffer-process shell-buffer)))
  2515. (unwind-protect
  2516. (progn
  2517. (set-process-query-on-exit-flag process nil)
  2518. (should (process-live-p process))
  2519. (with-current-buffer shell-buffer
  2520. (should (eq major-mode 'inferior-python-mode))
  2521. (should (string= (buffer-name) (format " *%s*" proc-name)))))
  2522. (kill-buffer shell-buffer))))
  2523. (ert-deftest python-shell-make-comint-3 ()
  2524. "Check comint creation with overridden python interpreter and args.
  2525. The command passed to `python-shell-make-comint' as argument must
  2526. locally override global values set in `python-shell-interpreter'
  2527. and `python-shell-interpreter-args' in the new shell buffer."
  2528. (skip-unless (executable-find python-tests-shell-interpreter))
  2529. (let* ((python-shell-setup-codes nil)
  2530. (python-shell-interpreter "interpreter")
  2531. (python-shell-interpreter-args "--some-args")
  2532. (proc-name (python-shell-get-process-name nil))
  2533. (interpreter-override
  2534. (concat (executable-find python-tests-shell-interpreter) " " "-i"))
  2535. (shell-buffer
  2536. (python-tests-with-temp-buffer
  2537. "" (python-shell-make-comint interpreter-override proc-name nil)))
  2538. (process (get-buffer-process shell-buffer)))
  2539. (unwind-protect
  2540. (progn
  2541. (set-process-query-on-exit-flag process nil)
  2542. (should (process-live-p process))
  2543. (with-current-buffer shell-buffer
  2544. (should (eq major-mode 'inferior-python-mode))
  2545. (should (file-equal-p
  2546. python-shell-interpreter
  2547. (executable-find python-tests-shell-interpreter)))
  2548. (should (string= python-shell-interpreter-args "-i"))))
  2549. (kill-buffer shell-buffer))))
  2550. (ert-deftest python-shell-make-comint-4 ()
  2551. "Check shell calculated prompts regexps are set."
  2552. (skip-unless (executable-find python-tests-shell-interpreter))
  2553. (let* ((process-environment process-environment)
  2554. (python-shell-setup-codes nil)
  2555. (python-shell-interpreter
  2556. (executable-find python-tests-shell-interpreter))
  2557. (python-shell-interpreter-args "-i")
  2558. (python-shell--prompt-calculated-input-regexp nil)
  2559. (python-shell--prompt-calculated-output-regexp nil)
  2560. (python-shell-prompt-detect-enabled t)
  2561. (python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
  2562. (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
  2563. (python-shell-prompt-regexp "in")
  2564. (python-shell-prompt-block-regexp "block")
  2565. (python-shell-prompt-pdb-regexp "pdf")
  2566. (python-shell-prompt-output-regexp "output")
  2567. (startup-code (concat "import sys\n"
  2568. "sys.ps1 = 'py> '\n"
  2569. "sys.ps2 = '..> '\n"
  2570. "sys.ps3 = 'out '\n"))
  2571. (startup-file (python-shell--save-temp-file startup-code))
  2572. (proc-name (python-shell-get-process-name nil))
  2573. (shell-buffer
  2574. (progn
  2575. (setenv "PYTHONSTARTUP" startup-file)
  2576. (python-tests-with-temp-buffer
  2577. "" (python-shell-make-comint
  2578. (python-shell-calculate-command) proc-name nil))))
  2579. (process (get-buffer-process shell-buffer)))
  2580. (unwind-protect
  2581. (progn
  2582. (set-process-query-on-exit-flag process nil)
  2583. (should (process-live-p process))
  2584. (with-current-buffer shell-buffer
  2585. (should (eq major-mode 'inferior-python-mode))
  2586. (should (string=
  2587. python-shell--prompt-calculated-input-regexp
  2588. (concat "^\\(extralargeinputprompt\\|\\.\\.> \\|"
  2589. "block\\|py> \\|pdf\\|sml\\|in\\)")))
  2590. (should (string=
  2591. python-shell--prompt-calculated-output-regexp
  2592. "^\\(extralargeoutputprompt\\|output\\|out \\|sml\\)"))))
  2593. (delete-file startup-file)
  2594. (kill-buffer shell-buffer))))
  2595. (ert-deftest python-shell-get-process-1 ()
  2596. "Check dedicated shell process preference over global."
  2597. (skip-unless (executable-find python-tests-shell-interpreter))
  2598. (python-tests-with-temp-file
  2599. ""
  2600. (let* ((python-shell-setup-codes nil)
  2601. (python-shell-interpreter
  2602. (executable-find python-tests-shell-interpreter))
  2603. (global-proc-name (python-shell-get-process-name nil))
  2604. (dedicated-proc-name (python-shell-get-process-name t))
  2605. (global-shell-buffer
  2606. (python-shell-make-comint
  2607. (python-shell-calculate-command) global-proc-name))
  2608. (dedicated-shell-buffer
  2609. (python-shell-make-comint
  2610. (python-shell-calculate-command) dedicated-proc-name))
  2611. (global-process (get-buffer-process global-shell-buffer))
  2612. (dedicated-process (get-buffer-process dedicated-shell-buffer)))
  2613. (unwind-protect
  2614. (progn
  2615. (set-process-query-on-exit-flag global-process nil)
  2616. (set-process-query-on-exit-flag dedicated-process nil)
  2617. ;; Prefer dedicated if global also exists.
  2618. (should (equal (python-shell-get-process) dedicated-process))
  2619. (kill-buffer dedicated-shell-buffer)
  2620. ;; If there's only global, use it.
  2621. (should (equal (python-shell-get-process) global-process))
  2622. (kill-buffer global-shell-buffer)
  2623. ;; No buffer available.
  2624. (should (not (python-shell-get-process))))
  2625. (ignore-errors (kill-buffer global-shell-buffer))
  2626. (ignore-errors (kill-buffer dedicated-shell-buffer))))))
  2627. (ert-deftest python-shell-internal-get-or-create-process-1 ()
  2628. "Check internal shell process creation fallback."
  2629. (skip-unless (executable-find python-tests-shell-interpreter))
  2630. (python-tests-with-temp-file
  2631. ""
  2632. (should (not (process-live-p (python-shell-internal-get-process-name))))
  2633. (let* ((python-shell-interpreter
  2634. (executable-find python-tests-shell-interpreter))
  2635. (internal-process-name (python-shell-internal-get-process-name))
  2636. (internal-process (python-shell-internal-get-or-create-process))
  2637. (internal-shell-buffer (process-buffer internal-process)))
  2638. (unwind-protect
  2639. (progn
  2640. (set-process-query-on-exit-flag internal-process nil)
  2641. (should (equal (process-name internal-process)
  2642. internal-process-name))
  2643. (should (equal internal-process
  2644. (python-shell-internal-get-or-create-process)))
  2645. ;; Assert the internal process is not a user process
  2646. (should (not (python-shell-get-process)))
  2647. (kill-buffer internal-shell-buffer))
  2648. (ignore-errors (kill-buffer internal-shell-buffer))))))
  2649. (ert-deftest python-shell-prompt-detect-1 ()
  2650. "Check prompt autodetection."
  2651. (skip-unless (executable-find python-tests-shell-interpreter))
  2652. (let ((process-environment process-environment))
  2653. ;; Ensure no startup file is enabled
  2654. (setenv "PYTHONSTARTUP" "")
  2655. (should python-shell-prompt-detect-enabled)
  2656. (should (equal (python-shell-prompt-detect) '(">>> " "... " "")))))
  2657. (ert-deftest python-shell-prompt-detect-2 ()
  2658. "Check prompt autodetection with startup file. Bug#17370."
  2659. (skip-unless (executable-find python-tests-shell-interpreter))
  2660. (let* ((process-environment process-environment)
  2661. (startup-code (concat "import sys\n"
  2662. "sys.ps1 = 'py> '\n"
  2663. "sys.ps2 = '..> '\n"
  2664. "sys.ps3 = 'out '\n"))
  2665. (startup-file (python-shell--save-temp-file startup-code)))
  2666. (unwind-protect
  2667. (progn
  2668. ;; Ensure startup file is enabled
  2669. (setenv "PYTHONSTARTUP" startup-file)
  2670. (should python-shell-prompt-detect-enabled)
  2671. (should (equal (python-shell-prompt-detect) '("py> " "..> " "out "))))
  2672. (ignore-errors (delete-file startup-file)))))
  2673. (ert-deftest python-shell-prompt-detect-3 ()
  2674. "Check prompts are not autodetected when feature is disabled."
  2675. (skip-unless (executable-find python-tests-shell-interpreter))
  2676. (let ((process-environment process-environment)
  2677. (python-shell-prompt-detect-enabled nil))
  2678. ;; Ensure no startup file is enabled
  2679. (should (not python-shell-prompt-detect-enabled))
  2680. (should (not (python-shell-prompt-detect)))))
  2681. (ert-deftest python-shell-prompt-detect-4 ()
  2682. "Check warning is shown when detection fails."
  2683. (skip-unless (executable-find python-tests-shell-interpreter))
  2684. (let* ((process-environment process-environment)
  2685. ;; Trigger failure by removing prompts in the startup file
  2686. (startup-code (concat "import sys\n"
  2687. "sys.ps1 = ''\n"
  2688. "sys.ps2 = ''\n"
  2689. "sys.ps3 = ''\n"))
  2690. (startup-file (python-shell--save-temp-file startup-code)))
  2691. (unwind-protect
  2692. (progn
  2693. (kill-buffer (get-buffer-create "*Warnings*"))
  2694. (should (not (get-buffer "*Warnings*")))
  2695. (setenv "PYTHONSTARTUP" startup-file)
  2696. (should python-shell-prompt-detect-failure-warning)
  2697. (should python-shell-prompt-detect-enabled)
  2698. (should (not (python-shell-prompt-detect)))
  2699. (should (get-buffer "*Warnings*")))
  2700. (ignore-errors (delete-file startup-file)))))
  2701. (ert-deftest python-shell-prompt-detect-5 ()
  2702. "Check disabled warnings are not shown when detection fails."
  2703. (skip-unless (executable-find python-tests-shell-interpreter))
  2704. (let* ((process-environment process-environment)
  2705. (startup-code (concat "import sys\n"
  2706. "sys.ps1 = ''\n"
  2707. "sys.ps2 = ''\n"
  2708. "sys.ps3 = ''\n"))
  2709. (startup-file (python-shell--save-temp-file startup-code))
  2710. (python-shell-prompt-detect-failure-warning nil))
  2711. (unwind-protect
  2712. (progn
  2713. (kill-buffer (get-buffer-create "*Warnings*"))
  2714. (should (not (get-buffer "*Warnings*")))
  2715. (setenv "PYTHONSTARTUP" startup-file)
  2716. (should (not python-shell-prompt-detect-failure-warning))
  2717. (should python-shell-prompt-detect-enabled)
  2718. (should (not (python-shell-prompt-detect)))
  2719. (should (not (get-buffer "*Warnings*"))))
  2720. (ignore-errors (delete-file startup-file)))))
  2721. (ert-deftest python-shell-prompt-detect-6 ()
  2722. "Warnings are not shown when detection is disabled."
  2723. (skip-unless (executable-find python-tests-shell-interpreter))
  2724. (let* ((process-environment process-environment)
  2725. (startup-code (concat "import sys\n"
  2726. "sys.ps1 = ''\n"
  2727. "sys.ps2 = ''\n"
  2728. "sys.ps3 = ''\n"))
  2729. (startup-file (python-shell--save-temp-file startup-code))
  2730. (python-shell-prompt-detect-failure-warning t)
  2731. (python-shell-prompt-detect-enabled nil))
  2732. (unwind-protect
  2733. (progn
  2734. (kill-buffer (get-buffer-create "*Warnings*"))
  2735. (should (not (get-buffer "*Warnings*")))
  2736. (setenv "PYTHONSTARTUP" startup-file)
  2737. (should python-shell-prompt-detect-failure-warning)
  2738. (should (not python-shell-prompt-detect-enabled))
  2739. (should (not (python-shell-prompt-detect)))
  2740. (should (not (get-buffer "*Warnings*"))))
  2741. (ignore-errors (delete-file startup-file)))))
  2742. (ert-deftest python-shell-prompt-validate-regexps-1 ()
  2743. "Check `python-shell-prompt-input-regexps' are validated."
  2744. (let* ((python-shell-prompt-input-regexps '("\\("))
  2745. (error-data (should-error (python-shell-prompt-validate-regexps)
  2746. :type 'user-error)))
  2747. (should
  2748. (string= (cadr error-data)
  2749. (format-message
  2750. "Invalid regexp \\( in `python-shell-prompt-input-regexps'")))))
  2751. (ert-deftest python-shell-prompt-validate-regexps-2 ()
  2752. "Check `python-shell-prompt-output-regexps' are validated."
  2753. (let* ((python-shell-prompt-output-regexps '("\\("))
  2754. (error-data (should-error (python-shell-prompt-validate-regexps)
  2755. :type 'user-error)))
  2756. (should
  2757. (string= (cadr error-data)
  2758. (format-message
  2759. "Invalid regexp \\( in `python-shell-prompt-output-regexps'")))))
  2760. (ert-deftest python-shell-prompt-validate-regexps-3 ()
  2761. "Check `python-shell-prompt-regexp' is validated."
  2762. (let* ((python-shell-prompt-regexp "\\(")
  2763. (error-data (should-error (python-shell-prompt-validate-regexps)
  2764. :type 'user-error)))
  2765. (should
  2766. (string= (cadr error-data)
  2767. (format-message
  2768. "Invalid regexp \\( in `python-shell-prompt-regexp'")))))
  2769. (ert-deftest python-shell-prompt-validate-regexps-4 ()
  2770. "Check `python-shell-prompt-block-regexp' is validated."
  2771. (let* ((python-shell-prompt-block-regexp "\\(")
  2772. (error-data (should-error (python-shell-prompt-validate-regexps)
  2773. :type 'user-error)))
  2774. (should
  2775. (string= (cadr error-data)
  2776. (format-message
  2777. "Invalid regexp \\( in `python-shell-prompt-block-regexp'")))))
  2778. (ert-deftest python-shell-prompt-validate-regexps-5 ()
  2779. "Check `python-shell-prompt-pdb-regexp' is validated."
  2780. (let* ((python-shell-prompt-pdb-regexp "\\(")
  2781. (error-data (should-error (python-shell-prompt-validate-regexps)
  2782. :type 'user-error)))
  2783. (should
  2784. (string= (cadr error-data)
  2785. (format-message
  2786. "Invalid regexp \\( in `python-shell-prompt-pdb-regexp'")))))
  2787. (ert-deftest python-shell-prompt-validate-regexps-6 ()
  2788. "Check `python-shell-prompt-output-regexp' is validated."
  2789. (let* ((python-shell-prompt-output-regexp "\\(")
  2790. (error-data (should-error (python-shell-prompt-validate-regexps)
  2791. :type 'user-error)))
  2792. (should
  2793. (string= (cadr error-data)
  2794. (format-message
  2795. "Invalid regexp \\( in `python-shell-prompt-output-regexp'")))))
  2796. (ert-deftest python-shell-prompt-validate-regexps-7 ()
  2797. "Check default regexps are valid."
  2798. ;; should not signal error
  2799. (python-shell-prompt-validate-regexps))
  2800. (ert-deftest python-shell-prompt-set-calculated-regexps-1 ()
  2801. "Check regexps are validated."
  2802. (let* ((python-shell-prompt-output-regexp '("\\("))
  2803. (python-shell--prompt-calculated-input-regexp nil)
  2804. (python-shell--prompt-calculated-output-regexp nil)
  2805. (python-shell-prompt-detect-enabled nil)
  2806. (error-data (should-error (python-shell-prompt-set-calculated-regexps)
  2807. :type 'user-error)))
  2808. (should
  2809. (string= (cadr error-data)
  2810. (format-message
  2811. "Invalid regexp \\( in `python-shell-prompt-output-regexp'")))))
  2812. (ert-deftest python-shell-prompt-set-calculated-regexps-2 ()
  2813. "Check `python-shell-prompt-input-regexps' are set."
  2814. (let* ((python-shell-prompt-input-regexps '("my" "prompt"))
  2815. (python-shell-prompt-output-regexps '(""))
  2816. (python-shell-prompt-regexp "")
  2817. (python-shell-prompt-block-regexp "")
  2818. (python-shell-prompt-pdb-regexp "")
  2819. (python-shell-prompt-output-regexp "")
  2820. (python-shell--prompt-calculated-input-regexp nil)
  2821. (python-shell--prompt-calculated-output-regexp nil)
  2822. (python-shell-prompt-detect-enabled nil))
  2823. (python-shell-prompt-set-calculated-regexps)
  2824. (should (string= python-shell--prompt-calculated-input-regexp
  2825. "^\\(prompt\\|my\\|\\)"))))
  2826. (ert-deftest python-shell-prompt-set-calculated-regexps-3 ()
  2827. "Check `python-shell-prompt-output-regexps' are set."
  2828. (let* ((python-shell-prompt-input-regexps '(""))
  2829. (python-shell-prompt-output-regexps '("my" "prompt"))
  2830. (python-shell-prompt-regexp "")
  2831. (python-shell-prompt-block-regexp "")
  2832. (python-shell-prompt-pdb-regexp "")
  2833. (python-shell-prompt-output-regexp "")
  2834. (python-shell--prompt-calculated-input-regexp nil)
  2835. (python-shell--prompt-calculated-output-regexp nil)
  2836. (python-shell-prompt-detect-enabled nil))
  2837. (python-shell-prompt-set-calculated-regexps)
  2838. (should (string= python-shell--prompt-calculated-output-regexp
  2839. "^\\(prompt\\|my\\|\\)"))))
  2840. (ert-deftest python-shell-prompt-set-calculated-regexps-4 ()
  2841. "Check user defined prompts are set."
  2842. (let* ((python-shell-prompt-input-regexps '(""))
  2843. (python-shell-prompt-output-regexps '(""))
  2844. (python-shell-prompt-regexp "prompt")
  2845. (python-shell-prompt-block-regexp "block")
  2846. (python-shell-prompt-pdb-regexp "pdb")
  2847. (python-shell-prompt-output-regexp "output")
  2848. (python-shell--prompt-calculated-input-regexp nil)
  2849. (python-shell--prompt-calculated-output-regexp nil)
  2850. (python-shell-prompt-detect-enabled nil))
  2851. (python-shell-prompt-set-calculated-regexps)
  2852. (should (string= python-shell--prompt-calculated-input-regexp
  2853. "^\\(prompt\\|block\\|pdb\\|\\)"))
  2854. (should (string= python-shell--prompt-calculated-output-regexp
  2855. "^\\(output\\|\\)"))))
  2856. (ert-deftest python-shell-prompt-set-calculated-regexps-5 ()
  2857. "Check order of regexps (larger first)."
  2858. (let* ((python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
  2859. (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
  2860. (python-shell-prompt-regexp "in")
  2861. (python-shell-prompt-block-regexp "block")
  2862. (python-shell-prompt-pdb-regexp "pdf")
  2863. (python-shell-prompt-output-regexp "output")
  2864. (python-shell--prompt-calculated-input-regexp nil)
  2865. (python-shell--prompt-calculated-output-regexp nil)
  2866. (python-shell-prompt-detect-enabled nil))
  2867. (python-shell-prompt-set-calculated-regexps)
  2868. (should (string= python-shell--prompt-calculated-input-regexp
  2869. "^\\(extralargeinputprompt\\|block\\|pdf\\|sml\\|in\\)"))
  2870. (should (string= python-shell--prompt-calculated-output-regexp
  2871. "^\\(extralargeoutputprompt\\|output\\|sml\\)"))))
  2872. (ert-deftest python-shell-prompt-set-calculated-regexps-6 ()
  2873. "Check detected prompts are included `regexp-quote'd."
  2874. (skip-unless (executable-find python-tests-shell-interpreter))
  2875. (let* ((python-shell-prompt-input-regexps '(""))
  2876. (python-shell-prompt-output-regexps '(""))
  2877. (python-shell-prompt-regexp "")
  2878. (python-shell-prompt-block-regexp "")
  2879. (python-shell-prompt-pdb-regexp "")
  2880. (python-shell-prompt-output-regexp "")
  2881. (python-shell--prompt-calculated-input-regexp nil)
  2882. (python-shell--prompt-calculated-output-regexp nil)
  2883. (python-shell-prompt-detect-enabled t)
  2884. (process-environment process-environment)
  2885. (startup-code (concat "import sys\n"
  2886. "sys.ps1 = 'p.> '\n"
  2887. "sys.ps2 = '..> '\n"
  2888. "sys.ps3 = 'o.t '\n"))
  2889. (startup-file (python-shell--save-temp-file startup-code)))
  2890. (unwind-protect
  2891. (progn
  2892. (setenv "PYTHONSTARTUP" startup-file)
  2893. (python-shell-prompt-set-calculated-regexps)
  2894. (should (string= python-shell--prompt-calculated-input-regexp
  2895. "^\\(\\.\\.> \\|p\\.> \\|\\)"))
  2896. (should (string= python-shell--prompt-calculated-output-regexp
  2897. "^\\(o\\.t \\|\\)")))
  2898. (ignore-errors (delete-file startup-file)))))
  2899. (ert-deftest python-shell-buffer-substring-1 ()
  2900. "Selecting a substring of the whole buffer must match its contents."
  2901. (python-tests-with-temp-buffer
  2902. "
  2903. class Foo(models.Model):
  2904. pass
  2905. class Bar(models.Model):
  2906. pass
  2907. "
  2908. (should (string= (buffer-string)
  2909. (python-shell-buffer-substring (point-min) (point-max))))))
  2910. (ert-deftest python-shell-buffer-substring-2 ()
  2911. "Main block should be removed if NOMAIN is non-nil."
  2912. (python-tests-with-temp-buffer
  2913. "
  2914. class Foo(models.Model):
  2915. pass
  2916. class Bar(models.Model):
  2917. pass
  2918. if __name__ == \"__main__\":
  2919. foo = Foo()
  2920. print (foo)
  2921. "
  2922. (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
  2923. "
  2924. class Foo(models.Model):
  2925. pass
  2926. class Bar(models.Model):
  2927. pass
  2928. "))))
  2929. (ert-deftest python-shell-buffer-substring-3 ()
  2930. "Main block should be removed if NOMAIN is non-nil."
  2931. (python-tests-with-temp-buffer
  2932. "
  2933. class Foo(models.Model):
  2934. pass
  2935. if __name__ == \"__main__\":
  2936. foo = Foo()
  2937. print (foo)
  2938. class Bar(models.Model):
  2939. pass
  2940. "
  2941. (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
  2942. "
  2943. class Foo(models.Model):
  2944. pass
  2945. class Bar(models.Model):
  2946. pass
  2947. "))))
  2948. (ert-deftest python-shell-buffer-substring-4 ()
  2949. "Coding cookie should be added for substrings."
  2950. (python-tests-with-temp-buffer
  2951. "# coding: latin-1
  2952. class Foo(models.Model):
  2953. pass
  2954. if __name__ == \"__main__\":
  2955. foo = Foo()
  2956. print (foo)
  2957. class Bar(models.Model):
  2958. pass
  2959. "
  2960. (should (string= (python-shell-buffer-substring
  2961. (python-tests-look-at "class Foo(models.Model):")
  2962. (progn (python-nav-forward-sexp) (point)))
  2963. "# -*- coding: latin-1 -*-
  2964. class Foo(models.Model):
  2965. pass"))))
  2966. (ert-deftest python-shell-buffer-substring-5 ()
  2967. "The proper amount of blank lines is added for a substring."
  2968. (python-tests-with-temp-buffer
  2969. "# coding: latin-1
  2970. class Foo(models.Model):
  2971. pass
  2972. if __name__ == \"__main__\":
  2973. foo = Foo()
  2974. print (foo)
  2975. class Bar(models.Model):
  2976. pass
  2977. "
  2978. (should (string= (python-shell-buffer-substring
  2979. (python-tests-look-at "class Bar(models.Model):")
  2980. (progn (python-nav-forward-sexp) (point)))
  2981. "# -*- coding: latin-1 -*-
  2982. class Bar(models.Model):
  2983. pass"))))
  2984. (ert-deftest python-shell-buffer-substring-6 ()
  2985. "Handle substring with coding cookie in the second line."
  2986. (python-tests-with-temp-buffer
  2987. "
  2988. # coding: latin-1
  2989. class Foo(models.Model):
  2990. pass
  2991. if __name__ == \"__main__\":
  2992. foo = Foo()
  2993. print (foo)
  2994. class Bar(models.Model):
  2995. pass
  2996. "
  2997. (should (string= (python-shell-buffer-substring
  2998. (python-tests-look-at "# coding: latin-1")
  2999. (python-tests-look-at "if __name__ == \"__main__\":"))
  3000. "# -*- coding: latin-1 -*-
  3001. class Foo(models.Model):
  3002. pass
  3003. "))))
  3004. (ert-deftest python-shell-buffer-substring-7 ()
  3005. "Ensure first coding cookie gets precedence."
  3006. (python-tests-with-temp-buffer
  3007. "# coding: utf-8
  3008. # coding: latin-1
  3009. class Foo(models.Model):
  3010. pass
  3011. if __name__ == \"__main__\":
  3012. foo = Foo()
  3013. print (foo)
  3014. class Bar(models.Model):
  3015. pass
  3016. "
  3017. (should (string= (python-shell-buffer-substring
  3018. (python-tests-look-at "# coding: latin-1")
  3019. (python-tests-look-at "if __name__ == \"__main__\":"))
  3020. "# -*- coding: utf-8 -*-
  3021. class Foo(models.Model):
  3022. pass
  3023. "))))
  3024. (ert-deftest python-shell-buffer-substring-8 ()
  3025. "Ensure first coding cookie gets precedence when sending whole buffer."
  3026. (python-tests-with-temp-buffer
  3027. "# coding: utf-8
  3028. # coding: latin-1
  3029. class Foo(models.Model):
  3030. pass
  3031. "
  3032. (should (string= (python-shell-buffer-substring (point-min) (point-max))
  3033. "# coding: utf-8
  3034. class Foo(models.Model):
  3035. pass
  3036. "))))
  3037. (ert-deftest python-shell-buffer-substring-9 ()
  3038. "Check substring starting from `point-min'."
  3039. (python-tests-with-temp-buffer
  3040. "# coding: utf-8
  3041. class Foo(models.Model):
  3042. pass
  3043. class Bar(models.Model):
  3044. pass
  3045. "
  3046. (should (string= (python-shell-buffer-substring
  3047. (point-min)
  3048. (python-tests-look-at "class Bar(models.Model):"))
  3049. "# coding: utf-8
  3050. class Foo(models.Model):
  3051. pass
  3052. "))))
  3053. (ert-deftest python-shell-buffer-substring-10 ()
  3054. "Check substring from partial block."
  3055. (python-tests-with-temp-buffer
  3056. "
  3057. def foo():
  3058. print ('a')
  3059. "
  3060. (should (string= (python-shell-buffer-substring
  3061. (python-tests-look-at "print ('a')")
  3062. (point-max))
  3063. "if True:
  3064. print ('a')
  3065. "))))
  3066. (ert-deftest python-shell-buffer-substring-11 ()
  3067. "Check substring from partial block and point within indentation."
  3068. (python-tests-with-temp-buffer
  3069. "
  3070. def foo():
  3071. print ('a')
  3072. "
  3073. (should (string= (python-shell-buffer-substring
  3074. (progn
  3075. (python-tests-look-at "print ('a')")
  3076. (backward-char 1)
  3077. (point))
  3078. (point-max))
  3079. "if True:
  3080. print ('a')
  3081. "))))
  3082. (ert-deftest python-shell-buffer-substring-12 ()
  3083. "Check substring from partial block and point in whitespace."
  3084. (python-tests-with-temp-buffer
  3085. "
  3086. def foo():
  3087. # Whitespace
  3088. print ('a')
  3089. "
  3090. (should (string= (python-shell-buffer-substring
  3091. (python-tests-look-at "# Whitespace")
  3092. (point-max))
  3093. "if True:
  3094. # Whitespace
  3095. print ('a')
  3096. "))))
  3097. ;;; Shell completion
  3098. (ert-deftest python-shell-completion-native-interpreter-disabled-p-1 ()
  3099. (let* ((python-shell-completion-native-disabled-interpreters (list "pypy"))
  3100. (python-shell-interpreter "/some/path/to/bin/pypy"))
  3101. (should (python-shell-completion-native-interpreter-disabled-p))))
  3102. ;;; PDB Track integration
  3103. ;;; Symbol completion
  3104. ;;; Fill paragraph
  3105. ;;; Skeletons
  3106. ;;; FFAP
  3107. ;;; Code check
  3108. ;;; Eldoc
  3109. (ert-deftest python-eldoc--get-symbol-at-point-1 ()
  3110. "Test paren handling."
  3111. (python-tests-with-temp-buffer
  3112. "
  3113. map(xx
  3114. map(codecs.open('somefile'
  3115. "
  3116. (python-tests-look-at "ap(xx")
  3117. (should (string= (python-eldoc--get-symbol-at-point) "map"))
  3118. (goto-char (line-end-position))
  3119. (should (string= (python-eldoc--get-symbol-at-point) "map"))
  3120. (python-tests-look-at "('somefile'")
  3121. (should (string= (python-eldoc--get-symbol-at-point) "map"))
  3122. (goto-char (line-end-position))
  3123. (should (string= (python-eldoc--get-symbol-at-point) "codecs.open"))))
  3124. (ert-deftest python-eldoc--get-symbol-at-point-2 ()
  3125. "Ensure self is replaced with the class name."
  3126. (python-tests-with-temp-buffer
  3127. "
  3128. class TheClass:
  3129. def some_method(self, n):
  3130. return n
  3131. def other(self):
  3132. return self.some_method(1234)
  3133. "
  3134. (python-tests-look-at "self.some_method")
  3135. (should (string= (python-eldoc--get-symbol-at-point)
  3136. "TheClass.some_method"))
  3137. (python-tests-look-at "1234)")
  3138. (should (string= (python-eldoc--get-symbol-at-point)
  3139. "TheClass.some_method"))))
  3140. (ert-deftest python-eldoc--get-symbol-at-point-3 ()
  3141. "Ensure symbol is found when point is at end of buffer."
  3142. (python-tests-with-temp-buffer
  3143. "
  3144. some_symbol
  3145. "
  3146. (goto-char (point-max))
  3147. (should (string= (python-eldoc--get-symbol-at-point)
  3148. "some_symbol"))))
  3149. (ert-deftest python-eldoc--get-symbol-at-point-4 ()
  3150. "Ensure symbol is found when point is at whitespace."
  3151. (python-tests-with-temp-buffer
  3152. "
  3153. some_symbol some_other_symbol
  3154. "
  3155. (python-tests-look-at " some_other_symbol")
  3156. (should (string= (python-eldoc--get-symbol-at-point)
  3157. "some_symbol"))))
  3158. ;;; Imenu
  3159. (ert-deftest python-imenu-create-index-1 ()
  3160. (python-tests-with-temp-buffer
  3161. "
  3162. class Foo(models.Model):
  3163. pass
  3164. class Bar(models.Model):
  3165. pass
  3166. def decorator(arg1, arg2, arg3):
  3167. '''print decorated function call data to stdout.
  3168. Usage:
  3169. @decorator('arg1', 'arg2')
  3170. def func(a, b, c=True):
  3171. pass
  3172. '''
  3173. def wrap(f):
  3174. print ('wrap')
  3175. def wrapped_f(*args):
  3176. print ('wrapped_f')
  3177. print ('Decorator arguments:', arg1, arg2, arg3)
  3178. f(*args)
  3179. print ('called f(*args)')
  3180. return wrapped_f
  3181. return wrap
  3182. class Baz(object):
  3183. def a(self):
  3184. pass
  3185. def b(self):
  3186. pass
  3187. class Frob(object):
  3188. def c(self):
  3189. pass
  3190. "
  3191. (goto-char (point-max))
  3192. (should (equal
  3193. (list
  3194. (cons "Foo (class)" (copy-marker 2))
  3195. (cons "Bar (class)" (copy-marker 38))
  3196. (list
  3197. "decorator (def)"
  3198. (cons "*function definition*" (copy-marker 74))
  3199. (list
  3200. "wrap (def)"
  3201. (cons "*function definition*" (copy-marker 254))
  3202. (cons "wrapped_f (def)" (copy-marker 294))))
  3203. (list
  3204. "Baz (class)"
  3205. (cons "*class definition*" (copy-marker 519))
  3206. (cons "a (def)" (copy-marker 539))
  3207. (cons "b (def)" (copy-marker 570))
  3208. (list
  3209. "Frob (class)"
  3210. (cons "*class definition*" (copy-marker 601))
  3211. (cons "c (def)" (copy-marker 626)))))
  3212. (python-imenu-create-index)))))
  3213. (ert-deftest python-imenu-create-index-2 ()
  3214. (python-tests-with-temp-buffer
  3215. "
  3216. class Foo(object):
  3217. def foo(self):
  3218. def foo1():
  3219. pass
  3220. def foobar(self):
  3221. pass
  3222. "
  3223. (goto-char (point-max))
  3224. (should (equal
  3225. (list
  3226. (list
  3227. "Foo (class)"
  3228. (cons "*class definition*" (copy-marker 2))
  3229. (list
  3230. "foo (def)"
  3231. (cons "*function definition*" (copy-marker 21))
  3232. (cons "foo1 (def)" (copy-marker 40)))
  3233. (cons "foobar (def)" (copy-marker 78))))
  3234. (python-imenu-create-index)))))
  3235. (ert-deftest python-imenu-create-index-3 ()
  3236. (python-tests-with-temp-buffer
  3237. "
  3238. class Foo(object):
  3239. def foo(self):
  3240. def foo1():
  3241. pass
  3242. def foo2():
  3243. pass
  3244. "
  3245. (goto-char (point-max))
  3246. (should (equal
  3247. (list
  3248. (list
  3249. "Foo (class)"
  3250. (cons "*class definition*" (copy-marker 2))
  3251. (list
  3252. "foo (def)"
  3253. (cons "*function definition*" (copy-marker 21))
  3254. (cons "foo1 (def)" (copy-marker 40))
  3255. (cons "foo2 (def)" (copy-marker 77)))))
  3256. (python-imenu-create-index)))))
  3257. (ert-deftest python-imenu-create-index-4 ()
  3258. (python-tests-with-temp-buffer
  3259. "
  3260. class Foo(object):
  3261. class Bar(object):
  3262. def __init__(self):
  3263. pass
  3264. def __str__(self):
  3265. pass
  3266. def __init__(self):
  3267. pass
  3268. "
  3269. (goto-char (point-max))
  3270. (should (equal
  3271. (list
  3272. (list
  3273. "Foo (class)"
  3274. (cons "*class definition*" (copy-marker 2))
  3275. (list
  3276. "Bar (class)"
  3277. (cons "*class definition*" (copy-marker 21))
  3278. (cons "__init__ (def)" (copy-marker 44))
  3279. (cons "__str__ (def)" (copy-marker 90)))
  3280. (cons "__init__ (def)" (copy-marker 135))))
  3281. (python-imenu-create-index)))))
  3282. (ert-deftest python-imenu-create-flat-index-1 ()
  3283. (python-tests-with-temp-buffer
  3284. "
  3285. class Foo(models.Model):
  3286. pass
  3287. class Bar(models.Model):
  3288. pass
  3289. def decorator(arg1, arg2, arg3):
  3290. '''print decorated function call data to stdout.
  3291. Usage:
  3292. @decorator('arg1', 'arg2')
  3293. def func(a, b, c=True):
  3294. pass
  3295. '''
  3296. def wrap(f):
  3297. print ('wrap')
  3298. def wrapped_f(*args):
  3299. print ('wrapped_f')
  3300. print ('Decorator arguments:', arg1, arg2, arg3)
  3301. f(*args)
  3302. print ('called f(*args)')
  3303. return wrapped_f
  3304. return wrap
  3305. class Baz(object):
  3306. def a(self):
  3307. pass
  3308. def b(self):
  3309. pass
  3310. class Frob(object):
  3311. def c(self):
  3312. pass
  3313. "
  3314. (goto-char (point-max))
  3315. (should (equal
  3316. (list (cons "Foo" (copy-marker 2))
  3317. (cons "Bar" (copy-marker 38))
  3318. (cons "decorator" (copy-marker 74))
  3319. (cons "decorator.wrap" (copy-marker 254))
  3320. (cons "decorator.wrap.wrapped_f" (copy-marker 294))
  3321. (cons "Baz" (copy-marker 519))
  3322. (cons "Baz.a" (copy-marker 539))
  3323. (cons "Baz.b" (copy-marker 570))
  3324. (cons "Baz.Frob" (copy-marker 601))
  3325. (cons "Baz.Frob.c" (copy-marker 626)))
  3326. (python-imenu-create-flat-index)))))
  3327. (ert-deftest python-imenu-create-flat-index-2 ()
  3328. (python-tests-with-temp-buffer
  3329. "
  3330. class Foo(object):
  3331. class Bar(object):
  3332. def __init__(self):
  3333. pass
  3334. def __str__(self):
  3335. pass
  3336. def __init__(self):
  3337. pass
  3338. "
  3339. (goto-char (point-max))
  3340. (should (equal
  3341. (list
  3342. (cons "Foo" (copy-marker 2))
  3343. (cons "Foo.Bar" (copy-marker 21))
  3344. (cons "Foo.Bar.__init__" (copy-marker 44))
  3345. (cons "Foo.Bar.__str__" (copy-marker 90))
  3346. (cons "Foo.__init__" (copy-marker 135)))
  3347. (python-imenu-create-flat-index)))))
  3348. ;;; Misc helpers
  3349. (ert-deftest python-info-current-defun-1 ()
  3350. (python-tests-with-temp-buffer
  3351. "
  3352. def foo(a, b):
  3353. "
  3354. (forward-line 1)
  3355. (should (string= "foo" (python-info-current-defun)))
  3356. (should (string= "def foo" (python-info-current-defun t)))
  3357. (forward-line 1)
  3358. (should (not (python-info-current-defun)))
  3359. (indent-for-tab-command)
  3360. (should (string= "foo" (python-info-current-defun)))
  3361. (should (string= "def foo" (python-info-current-defun t)))))
  3362. (ert-deftest python-info-current-defun-2 ()
  3363. (python-tests-with-temp-buffer
  3364. "
  3365. class C(object):
  3366. def m(self):
  3367. if True:
  3368. return [i for i in range(3)]
  3369. else:
  3370. return []
  3371. def b():
  3372. do_b()
  3373. def a():
  3374. do_a()
  3375. def c(self):
  3376. do_c()
  3377. "
  3378. (forward-line 1)
  3379. (should (string= "C" (python-info-current-defun)))
  3380. (should (string= "class C" (python-info-current-defun t)))
  3381. (python-tests-look-at "return [i for ")
  3382. (should (string= "C.m" (python-info-current-defun)))
  3383. (should (string= "def C.m" (python-info-current-defun t)))
  3384. (python-tests-look-at "def b():")
  3385. (should (string= "C.m.b" (python-info-current-defun)))
  3386. (should (string= "def C.m.b" (python-info-current-defun t)))
  3387. (forward-line 2)
  3388. (indent-for-tab-command)
  3389. (python-indent-dedent-line-backspace 1)
  3390. (should (string= "C.m" (python-info-current-defun)))
  3391. (should (string= "def C.m" (python-info-current-defun t)))
  3392. (python-tests-look-at "def c(self):")
  3393. (forward-line -1)
  3394. (indent-for-tab-command)
  3395. (should (string= "C.m.a" (python-info-current-defun)))
  3396. (should (string= "def C.m.a" (python-info-current-defun t)))
  3397. (python-indent-dedent-line-backspace 1)
  3398. (should (string= "C.m" (python-info-current-defun)))
  3399. (should (string= "def C.m" (python-info-current-defun t)))
  3400. (python-indent-dedent-line-backspace 1)
  3401. (should (string= "C" (python-info-current-defun)))
  3402. (should (string= "class C" (python-info-current-defun t)))
  3403. (python-tests-look-at "def c(self):")
  3404. (should (string= "C.c" (python-info-current-defun)))
  3405. (should (string= "def C.c" (python-info-current-defun t)))
  3406. (python-tests-look-at "do_c()")
  3407. (should (string= "C.c" (python-info-current-defun)))
  3408. (should (string= "def C.c" (python-info-current-defun t)))))
  3409. (ert-deftest python-info-current-defun-3 ()
  3410. (python-tests-with-temp-buffer
  3411. "
  3412. def decoratorFunctionWithArguments(arg1, arg2, arg3):
  3413. '''print decorated function call data to stdout.
  3414. Usage:
  3415. @decoratorFunctionWithArguments('arg1', 'arg2')
  3416. def func(a, b, c=True):
  3417. pass
  3418. '''
  3419. def wwrap(f):
  3420. print 'Inside wwrap()'
  3421. def wrapped_f(*args):
  3422. print 'Inside wrapped_f()'
  3423. print 'Decorator arguments:', arg1, arg2, arg3
  3424. f(*args)
  3425. print 'After f(*args)'
  3426. return wrapped_f
  3427. return wwrap
  3428. "
  3429. (python-tests-look-at "def wwrap(f):")
  3430. (forward-line -1)
  3431. (should (not (python-info-current-defun)))
  3432. (indent-for-tab-command 1)
  3433. (should (string= (python-info-current-defun)
  3434. "decoratorFunctionWithArguments"))
  3435. (should (string= (python-info-current-defun t)
  3436. "def decoratorFunctionWithArguments"))
  3437. (python-tests-look-at "def wrapped_f(*args):")
  3438. (should (string= (python-info-current-defun)
  3439. "decoratorFunctionWithArguments.wwrap.wrapped_f"))
  3440. (should (string= (python-info-current-defun t)
  3441. "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
  3442. (python-tests-look-at "return wrapped_f")
  3443. (should (string= (python-info-current-defun)
  3444. "decoratorFunctionWithArguments.wwrap"))
  3445. (should (string= (python-info-current-defun t)
  3446. "def decoratorFunctionWithArguments.wwrap"))
  3447. (end-of-line 1)
  3448. (python-tests-look-at "return wwrap")
  3449. (should (string= (python-info-current-defun)
  3450. "decoratorFunctionWithArguments"))
  3451. (should (string= (python-info-current-defun t)
  3452. "def decoratorFunctionWithArguments"))))
  3453. (ert-deftest python-info-current-symbol-1 ()
  3454. (python-tests-with-temp-buffer
  3455. "
  3456. class C(object):
  3457. def m(self):
  3458. self.c()
  3459. def c(self):
  3460. print ('a')
  3461. "
  3462. (python-tests-look-at "self.c()")
  3463. (should (string= "self.c" (python-info-current-symbol)))
  3464. (should (string= "C.c" (python-info-current-symbol t)))))
  3465. (ert-deftest python-info-current-symbol-2 ()
  3466. (python-tests-with-temp-buffer
  3467. "
  3468. class C(object):
  3469. class M(object):
  3470. def a(self):
  3471. self.c()
  3472. def c(self):
  3473. pass
  3474. "
  3475. (python-tests-look-at "self.c()")
  3476. (should (string= "self.c" (python-info-current-symbol)))
  3477. (should (string= "C.M.c" (python-info-current-symbol t)))))
  3478. (ert-deftest python-info-current-symbol-3 ()
  3479. "Keywords should not be considered symbols."
  3480. :expected-result :failed
  3481. (python-tests-with-temp-buffer
  3482. "
  3483. class C(object):
  3484. pass
  3485. "
  3486. ;; FIXME: keywords are not symbols.
  3487. (python-tests-look-at "class C")
  3488. (should (not (python-info-current-symbol)))
  3489. (should (not (python-info-current-symbol t)))
  3490. (python-tests-look-at "C(object)")
  3491. (should (string= "C" (python-info-current-symbol)))
  3492. (should (string= "class C" (python-info-current-symbol t)))))
  3493. (ert-deftest python-info-statement-starts-block-p-1 ()
  3494. (python-tests-with-temp-buffer
  3495. "
  3496. def long_function_name(
  3497. var_one, var_two, var_three,
  3498. var_four):
  3499. print (var_one)
  3500. "
  3501. (python-tests-look-at "def long_function_name")
  3502. (should (python-info-statement-starts-block-p))
  3503. (python-tests-look-at "print (var_one)")
  3504. (python-util-forward-comment -1)
  3505. (should (python-info-statement-starts-block-p))))
  3506. (ert-deftest python-info-statement-starts-block-p-2 ()
  3507. (python-tests-with-temp-buffer
  3508. "
  3509. if width == 0 and height == 0 and \\\\
  3510. color == 'red' and emphasis == 'strong' or \\\\
  3511. highlight > 100:
  3512. raise ValueError('sorry, you lose')
  3513. "
  3514. (python-tests-look-at "if width == 0 and")
  3515. (should (python-info-statement-starts-block-p))
  3516. (python-tests-look-at "raise ValueError(")
  3517. (python-util-forward-comment -1)
  3518. (should (python-info-statement-starts-block-p))))
  3519. (ert-deftest python-info-statement-ends-block-p-1 ()
  3520. (python-tests-with-temp-buffer
  3521. "
  3522. def long_function_name(
  3523. var_one, var_two, var_three,
  3524. var_four):
  3525. print (var_one)
  3526. "
  3527. (python-tests-look-at "print (var_one)")
  3528. (should (python-info-statement-ends-block-p))))
  3529. (ert-deftest python-info-statement-ends-block-p-2 ()
  3530. (python-tests-with-temp-buffer
  3531. "
  3532. if width == 0 and height == 0 and \\\\
  3533. color == 'red' and emphasis == 'strong' or \\\\
  3534. highlight > 100:
  3535. raise ValueError(
  3536. 'sorry, you lose'
  3537. )
  3538. "
  3539. (python-tests-look-at "raise ValueError(")
  3540. (should (python-info-statement-ends-block-p))))
  3541. (ert-deftest python-info-beginning-of-statement-p-1 ()
  3542. (python-tests-with-temp-buffer
  3543. "
  3544. def long_function_name(
  3545. var_one, var_two, var_three,
  3546. var_four):
  3547. print (var_one)
  3548. "
  3549. (python-tests-look-at "def long_function_name")
  3550. (should (python-info-beginning-of-statement-p))
  3551. (forward-char 10)
  3552. (should (not (python-info-beginning-of-statement-p)))
  3553. (python-tests-look-at "print (var_one)")
  3554. (should (python-info-beginning-of-statement-p))
  3555. (goto-char (line-beginning-position))
  3556. (should (not (python-info-beginning-of-statement-p)))))
  3557. (ert-deftest python-info-beginning-of-statement-p-2 ()
  3558. (python-tests-with-temp-buffer
  3559. "
  3560. if width == 0 and height == 0 and \\\\
  3561. color == 'red' and emphasis == 'strong' or \\\\
  3562. highlight > 100:
  3563. raise ValueError(
  3564. 'sorry, you lose'
  3565. )
  3566. "
  3567. (python-tests-look-at "if width == 0 and")
  3568. (should (python-info-beginning-of-statement-p))
  3569. (forward-char 10)
  3570. (should (not (python-info-beginning-of-statement-p)))
  3571. (python-tests-look-at "raise ValueError(")
  3572. (should (python-info-beginning-of-statement-p))
  3573. (goto-char (line-beginning-position))
  3574. (should (not (python-info-beginning-of-statement-p)))))
  3575. (ert-deftest python-info-end-of-statement-p-1 ()
  3576. (python-tests-with-temp-buffer
  3577. "
  3578. def long_function_name(
  3579. var_one, var_two, var_three,
  3580. var_four):
  3581. print (var_one)
  3582. "
  3583. (python-tests-look-at "def long_function_name")
  3584. (should (not (python-info-end-of-statement-p)))
  3585. (end-of-line)
  3586. (should (not (python-info-end-of-statement-p)))
  3587. (python-tests-look-at "print (var_one)")
  3588. (python-util-forward-comment -1)
  3589. (should (python-info-end-of-statement-p))
  3590. (python-tests-look-at "print (var_one)")
  3591. (should (not (python-info-end-of-statement-p)))
  3592. (end-of-line)
  3593. (should (python-info-end-of-statement-p))))
  3594. (ert-deftest python-info-end-of-statement-p-2 ()
  3595. (python-tests-with-temp-buffer
  3596. "
  3597. if width == 0 and height == 0 and \\\\
  3598. color == 'red' and emphasis == 'strong' or \\\\
  3599. highlight > 100:
  3600. raise ValueError(
  3601. 'sorry, you lose'
  3602. )
  3603. "
  3604. (python-tests-look-at "if width == 0 and")
  3605. (should (not (python-info-end-of-statement-p)))
  3606. (end-of-line)
  3607. (should (not (python-info-end-of-statement-p)))
  3608. (python-tests-look-at "raise ValueError(")
  3609. (python-util-forward-comment -1)
  3610. (should (python-info-end-of-statement-p))
  3611. (python-tests-look-at "raise ValueError(")
  3612. (should (not (python-info-end-of-statement-p)))
  3613. (end-of-line)
  3614. (should (not (python-info-end-of-statement-p)))
  3615. (goto-char (point-max))
  3616. (python-util-forward-comment -1)
  3617. (should (python-info-end-of-statement-p))))
  3618. (ert-deftest python-info-beginning-of-block-p-1 ()
  3619. (python-tests-with-temp-buffer
  3620. "
  3621. def long_function_name(
  3622. var_one, var_two, var_three,
  3623. var_four):
  3624. print (var_one)
  3625. "
  3626. (python-tests-look-at "def long_function_name")
  3627. (should (python-info-beginning-of-block-p))
  3628. (python-tests-look-at "var_one, var_two, var_three,")
  3629. (should (not (python-info-beginning-of-block-p)))
  3630. (python-tests-look-at "print (var_one)")
  3631. (should (not (python-info-beginning-of-block-p)))))
  3632. (ert-deftest python-info-beginning-of-block-p-2 ()
  3633. (python-tests-with-temp-buffer
  3634. "
  3635. if width == 0 and height == 0 and \\\\
  3636. color == 'red' and emphasis == 'strong' or \\\\
  3637. highlight > 100:
  3638. raise ValueError(
  3639. 'sorry, you lose'
  3640. )
  3641. "
  3642. (python-tests-look-at "if width == 0 and")
  3643. (should (python-info-beginning-of-block-p))
  3644. (python-tests-look-at "color == 'red' and emphasis")
  3645. (should (not (python-info-beginning-of-block-p)))
  3646. (python-tests-look-at "raise ValueError(")
  3647. (should (not (python-info-beginning-of-block-p)))))
  3648. (ert-deftest python-info-end-of-block-p-1 ()
  3649. (python-tests-with-temp-buffer
  3650. "
  3651. def long_function_name(
  3652. var_one, var_two, var_three,
  3653. var_four):
  3654. print (var_one)
  3655. "
  3656. (python-tests-look-at "def long_function_name")
  3657. (should (not (python-info-end-of-block-p)))
  3658. (python-tests-look-at "var_one, var_two, var_three,")
  3659. (should (not (python-info-end-of-block-p)))
  3660. (python-tests-look-at "var_four):")
  3661. (end-of-line)
  3662. (should (not (python-info-end-of-block-p)))
  3663. (python-tests-look-at "print (var_one)")
  3664. (should (not (python-info-end-of-block-p)))
  3665. (end-of-line 1)
  3666. (should (python-info-end-of-block-p))))
  3667. (ert-deftest python-info-end-of-block-p-2 ()
  3668. (python-tests-with-temp-buffer
  3669. "
  3670. if width == 0 and height == 0 and \\\\
  3671. color == 'red' and emphasis == 'strong' or \\\\
  3672. highlight > 100:
  3673. raise ValueError(
  3674. 'sorry, you lose'
  3675. )
  3676. "
  3677. (python-tests-look-at "if width == 0 and")
  3678. (should (not (python-info-end-of-block-p)))
  3679. (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
  3680. (should (not (python-info-end-of-block-p)))
  3681. (python-tests-look-at "highlight > 100:")
  3682. (end-of-line)
  3683. (should (not (python-info-end-of-block-p)))
  3684. (python-tests-look-at "raise ValueError(")
  3685. (should (not (python-info-end-of-block-p)))
  3686. (end-of-line 1)
  3687. (should (not (python-info-end-of-block-p)))
  3688. (goto-char (point-max))
  3689. (python-util-forward-comment -1)
  3690. (should (python-info-end-of-block-p))))
  3691. (ert-deftest python-info-dedenter-opening-block-position-1 ()
  3692. (python-tests-with-temp-buffer
  3693. "
  3694. if request.user.is_authenticated():
  3695. try:
  3696. profile = request.user.get_profile()
  3697. except Profile.DoesNotExist:
  3698. profile = Profile.objects.create(user=request.user)
  3699. else:
  3700. if profile.stats:
  3701. profile.recalculate_stats()
  3702. else:
  3703. profile.clear_stats()
  3704. finally:
  3705. profile.views += 1
  3706. profile.save()
  3707. "
  3708. (python-tests-look-at "try:")
  3709. (should (not (python-info-dedenter-opening-block-position)))
  3710. (python-tests-look-at "except Profile.DoesNotExist:")
  3711. (should (= (python-tests-look-at "try:" -1 t)
  3712. (python-info-dedenter-opening-block-position)))
  3713. (python-tests-look-at "else:")
  3714. (should (= (python-tests-look-at "except Profile.DoesNotExist:" -1 t)
  3715. (python-info-dedenter-opening-block-position)))
  3716. (python-tests-look-at "if profile.stats:")
  3717. (should (not (python-info-dedenter-opening-block-position)))
  3718. (python-tests-look-at "else:")
  3719. (should (= (python-tests-look-at "if profile.stats:" -1 t)
  3720. (python-info-dedenter-opening-block-position)))
  3721. (python-tests-look-at "finally:")
  3722. (should (= (python-tests-look-at "else:" -2 t)
  3723. (python-info-dedenter-opening-block-position)))))
  3724. (ert-deftest python-info-dedenter-opening-block-position-2 ()
  3725. (python-tests-with-temp-buffer
  3726. "
  3727. if request.user.is_authenticated():
  3728. profile = Profile.objects.get_or_create(user=request.user)
  3729. if profile.stats:
  3730. profile.recalculate_stats()
  3731. data = {
  3732. 'else': 'do it'
  3733. }
  3734. 'else'
  3735. "
  3736. (python-tests-look-at "'else': 'do it'")
  3737. (should (not (python-info-dedenter-opening-block-position)))
  3738. (python-tests-look-at "'else'")
  3739. (should (not (python-info-dedenter-opening-block-position)))))
  3740. (ert-deftest python-info-dedenter-opening-block-position-3 ()
  3741. (python-tests-with-temp-buffer
  3742. "
  3743. if save:
  3744. try:
  3745. write_to_disk(data)
  3746. except IOError:
  3747. msg = 'Error saving to disk'
  3748. message(msg)
  3749. logger.exception(msg)
  3750. except Exception:
  3751. if hide_details:
  3752. logger.exception('Unhandled exception')
  3753. else
  3754. finally:
  3755. data.free()
  3756. "
  3757. (python-tests-look-at "try:")
  3758. (should (not (python-info-dedenter-opening-block-position)))
  3759. (python-tests-look-at "except IOError:")
  3760. (should (= (python-tests-look-at "try:" -1 t)
  3761. (python-info-dedenter-opening-block-position)))
  3762. (python-tests-look-at "except Exception:")
  3763. (should (= (python-tests-look-at "except IOError:" -1 t)
  3764. (python-info-dedenter-opening-block-position)))
  3765. (python-tests-look-at "if hide_details:")
  3766. (should (not (python-info-dedenter-opening-block-position)))
  3767. ;; check indentation modifies the detected opening block
  3768. (python-tests-look-at "else")
  3769. (should (= (python-tests-look-at "if hide_details:" -1 t)
  3770. (python-info-dedenter-opening-block-position)))
  3771. (indent-line-to 8)
  3772. (should (= (python-tests-look-at "if hide_details:" -1 t)
  3773. (python-info-dedenter-opening-block-position)))
  3774. (indent-line-to 4)
  3775. (should (= (python-tests-look-at "except Exception:" -1 t)
  3776. (python-info-dedenter-opening-block-position)))
  3777. (indent-line-to 0)
  3778. (should (= (python-tests-look-at "if save:" -1 t)
  3779. (python-info-dedenter-opening-block-position)))))
  3780. (ert-deftest python-info-dedenter-opening-block-positions-1 ()
  3781. (python-tests-with-temp-buffer
  3782. "
  3783. if save:
  3784. try:
  3785. write_to_disk(data)
  3786. except IOError:
  3787. msg = 'Error saving to disk'
  3788. message(msg)
  3789. logger.exception(msg)
  3790. except Exception:
  3791. if hide_details:
  3792. logger.exception('Unhandled exception')
  3793. else
  3794. finally:
  3795. data.free()
  3796. "
  3797. (python-tests-look-at "try:")
  3798. (should (not (python-info-dedenter-opening-block-positions)))
  3799. (python-tests-look-at "except IOError:")
  3800. (should
  3801. (equal (list
  3802. (python-tests-look-at "try:" -1 t))
  3803. (python-info-dedenter-opening-block-positions)))
  3804. (python-tests-look-at "except Exception:")
  3805. (should
  3806. (equal (list
  3807. (python-tests-look-at "except IOError:" -1 t))
  3808. (python-info-dedenter-opening-block-positions)))
  3809. (python-tests-look-at "if hide_details:")
  3810. (should (not (python-info-dedenter-opening-block-positions)))
  3811. ;; check indentation does not modify the detected opening blocks
  3812. (python-tests-look-at "else")
  3813. (should
  3814. (equal (list
  3815. (python-tests-look-at "if hide_details:" -1 t)
  3816. (python-tests-look-at "except Exception:" -1 t)
  3817. (python-tests-look-at "if save:" -1 t))
  3818. (python-info-dedenter-opening-block-positions)))
  3819. (indent-line-to 8)
  3820. (should
  3821. (equal (list
  3822. (python-tests-look-at "if hide_details:" -1 t)
  3823. (python-tests-look-at "except Exception:" -1 t)
  3824. (python-tests-look-at "if save:" -1 t))
  3825. (python-info-dedenter-opening-block-positions)))
  3826. (indent-line-to 4)
  3827. (should
  3828. (equal (list
  3829. (python-tests-look-at "if hide_details:" -1 t)
  3830. (python-tests-look-at "except Exception:" -1 t)
  3831. (python-tests-look-at "if save:" -1 t))
  3832. (python-info-dedenter-opening-block-positions)))
  3833. (indent-line-to 0)
  3834. (should
  3835. (equal (list
  3836. (python-tests-look-at "if hide_details:" -1 t)
  3837. (python-tests-look-at "except Exception:" -1 t)
  3838. (python-tests-look-at "if save:" -1 t))
  3839. (python-info-dedenter-opening-block-positions)))))
  3840. (ert-deftest python-info-dedenter-opening-block-positions-2 ()
  3841. "Test detection of opening blocks for elif."
  3842. (python-tests-with-temp-buffer
  3843. "
  3844. if var:
  3845. if var2:
  3846. something()
  3847. elif var3:
  3848. something_else()
  3849. elif
  3850. "
  3851. (python-tests-look-at "elif var3:")
  3852. (should
  3853. (equal (list
  3854. (python-tests-look-at "if var2:" -1 t)
  3855. (python-tests-look-at "if var:" -1 t))
  3856. (python-info-dedenter-opening-block-positions)))
  3857. (python-tests-look-at "elif\n")
  3858. (should
  3859. (equal (list
  3860. (python-tests-look-at "elif var3:" -1 t)
  3861. (python-tests-look-at "if var:" -1 t))
  3862. (python-info-dedenter-opening-block-positions)))))
  3863. (ert-deftest python-info-dedenter-opening-block-positions-3 ()
  3864. "Test detection of opening blocks for else."
  3865. (python-tests-with-temp-buffer
  3866. "
  3867. try:
  3868. something()
  3869. except:
  3870. if var:
  3871. if var2:
  3872. something()
  3873. elif var3:
  3874. something_else()
  3875. else
  3876. if var4:
  3877. while var5:
  3878. var4.pop()
  3879. else
  3880. for value in var6:
  3881. if value > 0:
  3882. print value
  3883. else
  3884. "
  3885. (python-tests-look-at "else\n")
  3886. (should
  3887. (equal (list
  3888. (python-tests-look-at "elif var3:" -1 t)
  3889. (python-tests-look-at "if var:" -1 t)
  3890. (python-tests-look-at "except:" -1 t))
  3891. (python-info-dedenter-opening-block-positions)))
  3892. (python-tests-look-at "else\n")
  3893. (should
  3894. (equal (list
  3895. (python-tests-look-at "while var5:" -1 t)
  3896. (python-tests-look-at "if var4:" -1 t))
  3897. (python-info-dedenter-opening-block-positions)))
  3898. (python-tests-look-at "else\n")
  3899. (should
  3900. (equal (list
  3901. (python-tests-look-at "if value > 0:" -1 t)
  3902. (python-tests-look-at "for value in var6:" -1 t)
  3903. (python-tests-look-at "if var4:" -1 t))
  3904. (python-info-dedenter-opening-block-positions)))))
  3905. (ert-deftest python-info-dedenter-opening-block-positions-4 ()
  3906. "Test detection of opening blocks for except."
  3907. (python-tests-with-temp-buffer
  3908. "
  3909. try:
  3910. something()
  3911. except ValueError:
  3912. something_else()
  3913. except
  3914. "
  3915. (python-tests-look-at "except ValueError:")
  3916. (should
  3917. (equal (list (python-tests-look-at "try:" -1 t))
  3918. (python-info-dedenter-opening-block-positions)))
  3919. (python-tests-look-at "except\n")
  3920. (should
  3921. (equal (list (python-tests-look-at "except ValueError:" -1 t))
  3922. (python-info-dedenter-opening-block-positions)))))
  3923. (ert-deftest python-info-dedenter-opening-block-positions-5 ()
  3924. "Test detection of opening blocks for finally."
  3925. (python-tests-with-temp-buffer
  3926. "
  3927. try:
  3928. something()
  3929. finally
  3930. try:
  3931. something_else()
  3932. except:
  3933. logger.exception('something went wrong')
  3934. finally
  3935. try:
  3936. something_else_else()
  3937. except Exception:
  3938. logger.exception('something else went wrong')
  3939. else:
  3940. print ('all good')
  3941. finally
  3942. "
  3943. (python-tests-look-at "finally\n")
  3944. (should
  3945. (equal (list (python-tests-look-at "try:" -1 t))
  3946. (python-info-dedenter-opening-block-positions)))
  3947. (python-tests-look-at "finally\n")
  3948. (should
  3949. (equal (list (python-tests-look-at "except:" -1 t))
  3950. (python-info-dedenter-opening-block-positions)))
  3951. (python-tests-look-at "finally\n")
  3952. (should
  3953. (equal (list (python-tests-look-at "else:" -1 t))
  3954. (python-info-dedenter-opening-block-positions)))))
  3955. (ert-deftest python-info-dedenter-opening-block-message-1 ()
  3956. "Test dedenters inside strings are ignored."
  3957. (python-tests-with-temp-buffer
  3958. "'''
  3959. try:
  3960. something()
  3961. except:
  3962. logger.exception('something went wrong')
  3963. '''
  3964. "
  3965. (python-tests-look-at "except\n")
  3966. (should (not (python-info-dedenter-opening-block-message)))))
  3967. (ert-deftest python-info-dedenter-opening-block-message-2 ()
  3968. "Test except keyword."
  3969. (python-tests-with-temp-buffer
  3970. "
  3971. try:
  3972. something()
  3973. except:
  3974. logger.exception('something went wrong')
  3975. "
  3976. (python-tests-look-at "except:")
  3977. (should (string=
  3978. "Closes try:"
  3979. (substring-no-properties
  3980. (python-info-dedenter-opening-block-message))))
  3981. (end-of-line)
  3982. (should (string=
  3983. "Closes try:"
  3984. (substring-no-properties
  3985. (python-info-dedenter-opening-block-message))))))
  3986. (ert-deftest python-info-dedenter-opening-block-message-3 ()
  3987. "Test else keyword."
  3988. (python-tests-with-temp-buffer
  3989. "
  3990. try:
  3991. something()
  3992. except:
  3993. logger.exception('something went wrong')
  3994. else:
  3995. logger.debug('all good')
  3996. "
  3997. (python-tests-look-at "else:")
  3998. (should (string=
  3999. "Closes except:"
  4000. (substring-no-properties
  4001. (python-info-dedenter-opening-block-message))))
  4002. (end-of-line)
  4003. (should (string=
  4004. "Closes except:"
  4005. (substring-no-properties
  4006. (python-info-dedenter-opening-block-message))))))
  4007. (ert-deftest python-info-dedenter-opening-block-message-4 ()
  4008. "Test finally keyword."
  4009. (python-tests-with-temp-buffer
  4010. "
  4011. try:
  4012. something()
  4013. except:
  4014. logger.exception('something went wrong')
  4015. else:
  4016. logger.debug('all good')
  4017. finally:
  4018. clean()
  4019. "
  4020. (python-tests-look-at "finally:")
  4021. (should (string=
  4022. "Closes else:"
  4023. (substring-no-properties
  4024. (python-info-dedenter-opening-block-message))))
  4025. (end-of-line)
  4026. (should (string=
  4027. "Closes else:"
  4028. (substring-no-properties
  4029. (python-info-dedenter-opening-block-message))))))
  4030. (ert-deftest python-info-dedenter-opening-block-message-5 ()
  4031. "Test elif keyword."
  4032. (python-tests-with-temp-buffer
  4033. "
  4034. if a:
  4035. something()
  4036. elif b:
  4037. "
  4038. (python-tests-look-at "elif b:")
  4039. (should (string=
  4040. "Closes if a:"
  4041. (substring-no-properties
  4042. (python-info-dedenter-opening-block-message))))
  4043. (end-of-line)
  4044. (should (string=
  4045. "Closes if a:"
  4046. (substring-no-properties
  4047. (python-info-dedenter-opening-block-message))))))
  4048. (ert-deftest python-info-dedenter-statement-p-1 ()
  4049. "Test dedenters inside strings are ignored."
  4050. (python-tests-with-temp-buffer
  4051. "'''
  4052. try:
  4053. something()
  4054. except:
  4055. logger.exception('something went wrong')
  4056. '''
  4057. "
  4058. (python-tests-look-at "except\n")
  4059. (should (not (python-info-dedenter-statement-p)))))
  4060. (ert-deftest python-info-dedenter-statement-p-2 ()
  4061. "Test except keyword."
  4062. (python-tests-with-temp-buffer
  4063. "
  4064. try:
  4065. something()
  4066. except:
  4067. logger.exception('something went wrong')
  4068. "
  4069. (python-tests-look-at "except:")
  4070. (should (= (point) (python-info-dedenter-statement-p)))
  4071. (end-of-line)
  4072. (should (= (save-excursion
  4073. (back-to-indentation)
  4074. (point))
  4075. (python-info-dedenter-statement-p)))))
  4076. (ert-deftest python-info-dedenter-statement-p-3 ()
  4077. "Test else keyword."
  4078. (python-tests-with-temp-buffer
  4079. "
  4080. try:
  4081. something()
  4082. except:
  4083. logger.exception('something went wrong')
  4084. else:
  4085. logger.debug('all good')
  4086. "
  4087. (python-tests-look-at "else:")
  4088. (should (= (point) (python-info-dedenter-statement-p)))
  4089. (end-of-line)
  4090. (should (= (save-excursion
  4091. (back-to-indentation)
  4092. (point))
  4093. (python-info-dedenter-statement-p)))))
  4094. (ert-deftest python-info-dedenter-statement-p-4 ()
  4095. "Test finally keyword."
  4096. (python-tests-with-temp-buffer
  4097. "
  4098. try:
  4099. something()
  4100. except:
  4101. logger.exception('something went wrong')
  4102. else:
  4103. logger.debug('all good')
  4104. finally:
  4105. clean()
  4106. "
  4107. (python-tests-look-at "finally:")
  4108. (should (= (point) (python-info-dedenter-statement-p)))
  4109. (end-of-line)
  4110. (should (= (save-excursion
  4111. (back-to-indentation)
  4112. (point))
  4113. (python-info-dedenter-statement-p)))))
  4114. (ert-deftest python-info-dedenter-statement-p-5 ()
  4115. "Test elif keyword."
  4116. (python-tests-with-temp-buffer
  4117. "
  4118. if a:
  4119. something()
  4120. elif b:
  4121. "
  4122. (python-tests-look-at "elif b:")
  4123. (should (= (point) (python-info-dedenter-statement-p)))
  4124. (end-of-line)
  4125. (should (= (save-excursion
  4126. (back-to-indentation)
  4127. (point))
  4128. (python-info-dedenter-statement-p)))))
  4129. (ert-deftest python-info-line-ends-backslash-p-1 ()
  4130. (python-tests-with-temp-buffer
  4131. "
  4132. objects = Thing.objects.all() \\\\
  4133. .filter(
  4134. type='toy',
  4135. status='bought'
  4136. ) \\\\
  4137. .aggregate(
  4138. Sum('amount')
  4139. ) \\\\
  4140. .values_list()
  4141. "
  4142. (should (python-info-line-ends-backslash-p 2)) ; .filter(...
  4143. (should (python-info-line-ends-backslash-p 3))
  4144. (should (python-info-line-ends-backslash-p 4))
  4145. (should (python-info-line-ends-backslash-p 5))
  4146. (should (python-info-line-ends-backslash-p 6)) ; ) \...
  4147. (should (python-info-line-ends-backslash-p 7))
  4148. (should (python-info-line-ends-backslash-p 8))
  4149. (should (python-info-line-ends-backslash-p 9))
  4150. (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
  4151. (ert-deftest python-info-beginning-of-backslash-1 ()
  4152. (python-tests-with-temp-buffer
  4153. "
  4154. objects = Thing.objects.all() \\\\
  4155. .filter(
  4156. type='toy',
  4157. status='bought'
  4158. ) \\\\
  4159. .aggregate(
  4160. Sum('amount')
  4161. ) \\\\
  4162. .values_list()
  4163. "
  4164. (let ((first 2)
  4165. (second (python-tests-look-at ".filter("))
  4166. (third (python-tests-look-at ".aggregate(")))
  4167. (should (= first (python-info-beginning-of-backslash 2)))
  4168. (should (= second (python-info-beginning-of-backslash 3)))
  4169. (should (= second (python-info-beginning-of-backslash 4)))
  4170. (should (= second (python-info-beginning-of-backslash 5)))
  4171. (should (= second (python-info-beginning-of-backslash 6)))
  4172. (should (= third (python-info-beginning-of-backslash 7)))
  4173. (should (= third (python-info-beginning-of-backslash 8)))
  4174. (should (= third (python-info-beginning-of-backslash 9)))
  4175. (should (not (python-info-beginning-of-backslash 10))))))
  4176. (ert-deftest python-info-continuation-line-p-1 ()
  4177. (python-tests-with-temp-buffer
  4178. "
  4179. if width == 0 and height == 0 and \\\\
  4180. color == 'red' and emphasis == 'strong' or \\\\
  4181. highlight > 100:
  4182. raise ValueError(
  4183. 'sorry, you lose'
  4184. )
  4185. "
  4186. (python-tests-look-at "if width == 0 and height == 0 and")
  4187. (should (not (python-info-continuation-line-p)))
  4188. (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
  4189. (should (python-info-continuation-line-p))
  4190. (python-tests-look-at "highlight > 100:")
  4191. (should (python-info-continuation-line-p))
  4192. (python-tests-look-at "raise ValueError(")
  4193. (should (not (python-info-continuation-line-p)))
  4194. (python-tests-look-at "'sorry, you lose'")
  4195. (should (python-info-continuation-line-p))
  4196. (forward-line 1)
  4197. (should (python-info-continuation-line-p))
  4198. (python-tests-look-at ")")
  4199. (should (python-info-continuation-line-p))
  4200. (forward-line 1)
  4201. (should (not (python-info-continuation-line-p)))))
  4202. (ert-deftest python-info-block-continuation-line-p-1 ()
  4203. (python-tests-with-temp-buffer
  4204. "
  4205. if width == 0 and height == 0 and \\\\
  4206. color == 'red' and emphasis == 'strong' or \\\\
  4207. highlight > 100:
  4208. raise ValueError(
  4209. 'sorry, you lose'
  4210. )
  4211. "
  4212. (python-tests-look-at "if width == 0 and")
  4213. (should (not (python-info-block-continuation-line-p)))
  4214. (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
  4215. (should (= (python-info-block-continuation-line-p)
  4216. (python-tests-look-at "if width == 0 and" -1 t)))
  4217. (python-tests-look-at "highlight > 100:")
  4218. (should (not (python-info-block-continuation-line-p)))))
  4219. (ert-deftest python-info-block-continuation-line-p-2 ()
  4220. (python-tests-with-temp-buffer
  4221. "
  4222. def foo(a,
  4223. b,
  4224. c):
  4225. pass
  4226. "
  4227. (python-tests-look-at "def foo(a,")
  4228. (should (not (python-info-block-continuation-line-p)))
  4229. (python-tests-look-at "b,")
  4230. (should (= (python-info-block-continuation-line-p)
  4231. (python-tests-look-at "def foo(a," -1 t)))
  4232. (python-tests-look-at "c):")
  4233. (should (not (python-info-block-continuation-line-p)))))
  4234. (ert-deftest python-info-assignment-statement-p-1 ()
  4235. (python-tests-with-temp-buffer
  4236. "
  4237. data = foo(), bar() \\\\
  4238. baz(), 4 \\\\
  4239. 5, 6
  4240. "
  4241. (python-tests-look-at "data = foo(), bar()")
  4242. (should (python-info-assignment-statement-p))
  4243. (should (python-info-assignment-statement-p t))
  4244. (python-tests-look-at "baz(), 4")
  4245. (should (python-info-assignment-statement-p))
  4246. (should (not (python-info-assignment-statement-p t)))
  4247. (python-tests-look-at "5, 6")
  4248. (should (python-info-assignment-statement-p))
  4249. (should (not (python-info-assignment-statement-p t)))))
  4250. (ert-deftest python-info-assignment-statement-p-2 ()
  4251. (python-tests-with-temp-buffer
  4252. "
  4253. data = (foo(), bar()
  4254. baz(), 4
  4255. 5, 6)
  4256. "
  4257. (python-tests-look-at "data = (foo(), bar()")
  4258. (should (python-info-assignment-statement-p))
  4259. (should (python-info-assignment-statement-p t))
  4260. (python-tests-look-at "baz(), 4")
  4261. (should (python-info-assignment-statement-p))
  4262. (should (not (python-info-assignment-statement-p t)))
  4263. (python-tests-look-at "5, 6)")
  4264. (should (python-info-assignment-statement-p))
  4265. (should (not (python-info-assignment-statement-p t)))))
  4266. (ert-deftest python-info-assignment-statement-p-3 ()
  4267. (python-tests-with-temp-buffer
  4268. "
  4269. data '=' 42
  4270. "
  4271. (python-tests-look-at "data '=' 42")
  4272. (should (not (python-info-assignment-statement-p)))
  4273. (should (not (python-info-assignment-statement-p t)))))
  4274. (ert-deftest python-info-assignment-continuation-line-p-1 ()
  4275. (python-tests-with-temp-buffer
  4276. "
  4277. data = foo(), bar() \\\\
  4278. baz(), 4 \\\\
  4279. 5, 6
  4280. "
  4281. (python-tests-look-at "data = foo(), bar()")
  4282. (should (not (python-info-assignment-continuation-line-p)))
  4283. (python-tests-look-at "baz(), 4")
  4284. (should (= (python-info-assignment-continuation-line-p)
  4285. (python-tests-look-at "foo()," -1 t)))
  4286. (python-tests-look-at "5, 6")
  4287. (should (not (python-info-assignment-continuation-line-p)))))
  4288. (ert-deftest python-info-assignment-continuation-line-p-2 ()
  4289. (python-tests-with-temp-buffer
  4290. "
  4291. data = (foo(), bar()
  4292. baz(), 4
  4293. 5, 6)
  4294. "
  4295. (python-tests-look-at "data = (foo(), bar()")
  4296. (should (not (python-info-assignment-continuation-line-p)))
  4297. (python-tests-look-at "baz(), 4")
  4298. (should (= (python-info-assignment-continuation-line-p)
  4299. (python-tests-look-at "(foo()," -1 t)))
  4300. (python-tests-look-at "5, 6)")
  4301. (should (not (python-info-assignment-continuation-line-p)))))
  4302. (ert-deftest python-info-looking-at-beginning-of-defun-1 ()
  4303. (python-tests-with-temp-buffer
  4304. "
  4305. def decorat0r(deff):
  4306. '''decorates stuff.
  4307. @decorat0r
  4308. def foo(arg):
  4309. ...
  4310. '''
  4311. def wrap():
  4312. deff()
  4313. return wwrap
  4314. "
  4315. (python-tests-look-at "def decorat0r(deff):")
  4316. (should (python-info-looking-at-beginning-of-defun))
  4317. (python-tests-look-at "def foo(arg):")
  4318. (should (not (python-info-looking-at-beginning-of-defun)))
  4319. (python-tests-look-at "def wrap():")
  4320. (should (python-info-looking-at-beginning-of-defun))
  4321. (python-tests-look-at "deff()")
  4322. (should (not (python-info-looking-at-beginning-of-defun)))))
  4323. (ert-deftest python-info-current-line-comment-p-1 ()
  4324. (python-tests-with-temp-buffer
  4325. "
  4326. # this is a comment
  4327. foo = True # another comment
  4328. '#this is a string'
  4329. if foo:
  4330. # more comments
  4331. print ('bar') # print bar
  4332. "
  4333. (python-tests-look-at "# this is a comment")
  4334. (should (python-info-current-line-comment-p))
  4335. (python-tests-look-at "foo = True # another comment")
  4336. (should (not (python-info-current-line-comment-p)))
  4337. (python-tests-look-at "'#this is a string'")
  4338. (should (not (python-info-current-line-comment-p)))
  4339. (python-tests-look-at "# more comments")
  4340. (should (python-info-current-line-comment-p))
  4341. (python-tests-look-at "print ('bar') # print bar")
  4342. (should (not (python-info-current-line-comment-p)))))
  4343. (ert-deftest python-info-current-line-empty-p ()
  4344. (python-tests-with-temp-buffer
  4345. "
  4346. # this is a comment
  4347. foo = True # another comment
  4348. "
  4349. (should (python-info-current-line-empty-p))
  4350. (python-tests-look-at "# this is a comment")
  4351. (should (not (python-info-current-line-empty-p)))
  4352. (forward-line 1)
  4353. (should (python-info-current-line-empty-p))))
  4354. (ert-deftest python-info-docstring-p-1 ()
  4355. "Test module docstring detection."
  4356. (python-tests-with-temp-buffer
  4357. "# -*- coding: utf-8 -*-
  4358. #!/usr/bin/python
  4359. '''
  4360. Module Docstring Django style.
  4361. '''
  4362. u'''Additional module docstring.'''
  4363. '''Not a module docstring.'''
  4364. "
  4365. (python-tests-look-at "Module Docstring Django style.")
  4366. (should (python-info-docstring-p))
  4367. (python-tests-look-at "u'''Additional module docstring.'''")
  4368. (should (python-info-docstring-p))
  4369. (python-tests-look-at "'''Not a module docstring.'''")
  4370. (should (not (python-info-docstring-p)))))
  4371. (ert-deftest python-info-docstring-p-2 ()
  4372. "Test variable docstring detection."
  4373. (python-tests-with-temp-buffer
  4374. "
  4375. variable = 42
  4376. U'''Variable docstring.'''
  4377. '''Additional variable docstring.'''
  4378. '''Not a variable docstring.'''
  4379. "
  4380. (python-tests-look-at "Variable docstring.")
  4381. (should (python-info-docstring-p))
  4382. (python-tests-look-at "u'''Additional variable docstring.'''")
  4383. (should (python-info-docstring-p))
  4384. (python-tests-look-at "'''Not a variable docstring.'''")
  4385. (should (not (python-info-docstring-p)))))
  4386. (ert-deftest python-info-docstring-p-3 ()
  4387. "Test function docstring detection."
  4388. (python-tests-with-temp-buffer
  4389. "
  4390. def func(a, b):
  4391. r'''
  4392. Function docstring.
  4393. onetwo style.
  4394. '''
  4395. R'''Additional function docstring.'''
  4396. '''Not a function docstring.'''
  4397. return a + b
  4398. "
  4399. (python-tests-look-at "Function docstring.")
  4400. (should (python-info-docstring-p))
  4401. (python-tests-look-at "R'''Additional function docstring.'''")
  4402. (should (python-info-docstring-p))
  4403. (python-tests-look-at "'''Not a function docstring.'''")
  4404. (should (not (python-info-docstring-p)))))
  4405. (ert-deftest python-info-docstring-p-4 ()
  4406. "Test class docstring detection."
  4407. (python-tests-with-temp-buffer
  4408. "
  4409. class Class:
  4410. ur'''
  4411. Class docstring.
  4412. symmetric style.
  4413. '''
  4414. uR'''
  4415. Additional class docstring.
  4416. '''
  4417. '''Not a class docstring.'''
  4418. pass
  4419. "
  4420. (python-tests-look-at "Class docstring.")
  4421. (should (python-info-docstring-p))
  4422. (python-tests-look-at "uR'''") ;; Additional class docstring
  4423. (should (python-info-docstring-p))
  4424. (python-tests-look-at "'''Not a class docstring.'''")
  4425. (should (not (python-info-docstring-p)))))
  4426. (ert-deftest python-info-docstring-p-5 ()
  4427. "Test class attribute docstring detection."
  4428. (python-tests-with-temp-buffer
  4429. "
  4430. class Class:
  4431. attribute = 42
  4432. Ur'''
  4433. Class attribute docstring.
  4434. pep-257 style.
  4435. '''
  4436. UR'''
  4437. Additional class attribute docstring.
  4438. '''
  4439. '''Not a class attribute docstring.'''
  4440. pass
  4441. "
  4442. (python-tests-look-at "Class attribute docstring.")
  4443. (should (python-info-docstring-p))
  4444. (python-tests-look-at "UR'''") ;; Additional class attr docstring
  4445. (should (python-info-docstring-p))
  4446. (python-tests-look-at "'''Not a class attribute docstring.'''")
  4447. (should (not (python-info-docstring-p)))))
  4448. (ert-deftest python-info-docstring-p-6 ()
  4449. "Test class method docstring detection."
  4450. (python-tests-with-temp-buffer
  4451. "
  4452. class Class:
  4453. def __init__(self, a, b):
  4454. self.a = a
  4455. self.b = b
  4456. def __call__(self):
  4457. '''Method docstring.
  4458. pep-257-nn style.
  4459. '''
  4460. '''Additional method docstring.'''
  4461. '''Not a method docstring.'''
  4462. return self.a + self.b
  4463. "
  4464. (python-tests-look-at "Method docstring.")
  4465. (should (python-info-docstring-p))
  4466. (python-tests-look-at "'''Additional method docstring.'''")
  4467. (should (python-info-docstring-p))
  4468. (python-tests-look-at "'''Not a method docstring.'''")
  4469. (should (not (python-info-docstring-p)))))
  4470. (ert-deftest python-info-encoding-from-cookie-1 ()
  4471. "Should detect it on first line."
  4472. (python-tests-with-temp-buffer
  4473. "# coding=latin-1
  4474. foo = True # another comment
  4475. "
  4476. (should (eq (python-info-encoding-from-cookie) 'latin-1))))
  4477. (ert-deftest python-info-encoding-from-cookie-2 ()
  4478. "Should detect it on second line."
  4479. (python-tests-with-temp-buffer
  4480. "
  4481. # coding=latin-1
  4482. foo = True # another comment
  4483. "
  4484. (should (eq (python-info-encoding-from-cookie) 'latin-1))))
  4485. (ert-deftest python-info-encoding-from-cookie-3 ()
  4486. "Should not be detected on third line (and following ones)."
  4487. (python-tests-with-temp-buffer
  4488. "
  4489. # coding=latin-1
  4490. foo = True # another comment
  4491. "
  4492. (should (not (python-info-encoding-from-cookie)))))
  4493. (ert-deftest python-info-encoding-from-cookie-4 ()
  4494. "Should detect Emacs style."
  4495. (python-tests-with-temp-buffer
  4496. "# -*- coding: latin-1 -*-
  4497. foo = True # another comment"
  4498. (should (eq (python-info-encoding-from-cookie) 'latin-1))))
  4499. (ert-deftest python-info-encoding-from-cookie-5 ()
  4500. "Should detect Vim style."
  4501. (python-tests-with-temp-buffer
  4502. "# vim: set fileencoding=latin-1 :
  4503. foo = True # another comment"
  4504. (should (eq (python-info-encoding-from-cookie) 'latin-1))))
  4505. (ert-deftest python-info-encoding-from-cookie-6 ()
  4506. "First cookie wins."
  4507. (python-tests-with-temp-buffer
  4508. "# -*- coding: iso-8859-1 -*-
  4509. # vim: set fileencoding=latin-1 :
  4510. foo = True # another comment"
  4511. (should (eq (python-info-encoding-from-cookie) 'iso-8859-1))))
  4512. (ert-deftest python-info-encoding-from-cookie-7 ()
  4513. "First cookie wins."
  4514. (python-tests-with-temp-buffer
  4515. "# vim: set fileencoding=latin-1 :
  4516. # -*- coding: iso-8859-1 -*-
  4517. foo = True # another comment"
  4518. (should (eq (python-info-encoding-from-cookie) 'latin-1))))
  4519. (ert-deftest python-info-encoding-1 ()
  4520. "Should return the detected encoding from cookie."
  4521. (python-tests-with-temp-buffer
  4522. "# vim: set fileencoding=latin-1 :
  4523. foo = True # another comment"
  4524. (should (eq (python-info-encoding) 'latin-1))))
  4525. (ert-deftest python-info-encoding-2 ()
  4526. "Should default to utf-8."
  4527. (python-tests-with-temp-buffer
  4528. "# No encoding for you
  4529. foo = True # another comment"
  4530. (should (eq (python-info-encoding) 'utf-8))))
  4531. ;;; Utility functions
  4532. (ert-deftest python-util-goto-line-1 ()
  4533. (python-tests-with-temp-buffer
  4534. (concat
  4535. "# a comment
  4536. # another comment
  4537. def foo(a, b, c):
  4538. pass" (make-string 20 ?\n))
  4539. (python-util-goto-line 10)
  4540. (should (= (line-number-at-pos) 10))
  4541. (python-util-goto-line 20)
  4542. (should (= (line-number-at-pos) 20))))
  4543. (ert-deftest python-util-clone-local-variables-1 ()
  4544. (let ((buffer (generate-new-buffer
  4545. "python-util-clone-local-variables-1"))
  4546. (varcons
  4547. '((python-fill-docstring-style . django)
  4548. (python-shell-interpreter . "python")
  4549. (python-shell-interpreter-args . "manage.py shell")
  4550. (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
  4551. (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
  4552. (python-shell-extra-pythonpaths "/home/user/pylib/")
  4553. (python-shell-completion-setup-code
  4554. . "from IPython.core.completerlib import module_completion")
  4555. (python-shell-completion-string-code
  4556. . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
  4557. (python-shell-virtualenv-root
  4558. . "/home/user/.virtualenvs/project"))))
  4559. (with-current-buffer buffer
  4560. (kill-all-local-variables)
  4561. (dolist (ccons varcons)
  4562. (set (make-local-variable (car ccons)) (cdr ccons))))
  4563. (python-tests-with-temp-buffer
  4564. ""
  4565. (python-util-clone-local-variables buffer)
  4566. (dolist (ccons varcons)
  4567. (should
  4568. (equal (symbol-value (car ccons)) (cdr ccons)))))
  4569. (kill-buffer buffer)))
  4570. (ert-deftest python-util-strip-string-1 ()
  4571. (should (string= (python-util-strip-string "\t\r\n str") "str"))
  4572. (should (string= (python-util-strip-string "str \n\r") "str"))
  4573. (should (string= (python-util-strip-string "\t\r\n str \n\r ") "str"))
  4574. (should
  4575. (string= (python-util-strip-string "\n str \nin \tg \n\r") "str \nin \tg"))
  4576. (should (string= (python-util-strip-string "\n \t \n\r ") ""))
  4577. (should (string= (python-util-strip-string "") "")))
  4578. (ert-deftest python-util-forward-comment-1 ()
  4579. (python-tests-with-temp-buffer
  4580. (concat
  4581. "# a comment
  4582. # another comment
  4583. # bad indented comment
  4584. # more comments" (make-string 9999 ?\n))
  4585. (python-util-forward-comment 1)
  4586. (should (= (point) (point-max)))
  4587. (python-util-forward-comment -1)
  4588. (should (= (point) (point-min)))))
  4589. (ert-deftest python-util-valid-regexp-p-1 ()
  4590. (should (python-util-valid-regexp-p ""))
  4591. (should (python-util-valid-regexp-p python-shell-prompt-regexp))
  4592. (should (not (python-util-valid-regexp-p "\\("))))
  4593. ;;; Electricity
  4594. (ert-deftest python-parens-electric-indent-1 ()
  4595. (let ((eim electric-indent-mode))
  4596. (unwind-protect
  4597. (progn
  4598. (python-tests-with-temp-buffer
  4599. "
  4600. from django.conf.urls import patterns, include, url
  4601. from django.contrib import admin
  4602. from myapp import views
  4603. urlpatterns = patterns('',
  4604. url(r'^$', views.index
  4605. )
  4606. "
  4607. (electric-indent-mode 1)
  4608. (python-tests-look-at "views.index")
  4609. (end-of-line)
  4610. ;; Inserting commas within the same line should leave
  4611. ;; indentation unchanged.
  4612. (python-tests-self-insert ",")
  4613. (should (= (current-indentation) 4))
  4614. ;; As well as any other input happening within the same
  4615. ;; set of parens.
  4616. (python-tests-self-insert " name='index')")
  4617. (should (= (current-indentation) 4))
  4618. ;; But a comma outside it, should trigger indentation.
  4619. (python-tests-self-insert ",")
  4620. (should (= (current-indentation) 23))
  4621. ;; Newline indents to the first argument column
  4622. (python-tests-self-insert "\n")
  4623. (should (= (current-indentation) 23))
  4624. ;; All this input must not change indentation
  4625. (indent-line-to 4)
  4626. (python-tests-self-insert "url(r'^/login$', views.login)")
  4627. (should (= (current-indentation) 4))
  4628. ;; But this comma does
  4629. (python-tests-self-insert ",")
  4630. (should (= (current-indentation) 23))))
  4631. (or eim (electric-indent-mode -1)))))
  4632. (ert-deftest python-triple-quote-pairing ()
  4633. (let ((epm electric-pair-mode))
  4634. (unwind-protect
  4635. (progn
  4636. (python-tests-with-temp-buffer
  4637. "\"\"\n"
  4638. (or epm (electric-pair-mode 1))
  4639. (goto-char (1- (point-max)))
  4640. (python-tests-self-insert ?\")
  4641. (should (string= (buffer-string)
  4642. "\"\"\"\"\"\"\n"))
  4643. (should (= (point) 4)))
  4644. (python-tests-with-temp-buffer
  4645. "\n"
  4646. (python-tests-self-insert (list ?\" ?\" ?\"))
  4647. (should (string= (buffer-string)
  4648. "\"\"\"\"\"\"\n"))
  4649. (should (= (point) 4)))
  4650. (python-tests-with-temp-buffer
  4651. "\"\n\"\"\n"
  4652. (goto-char (1- (point-max)))
  4653. (python-tests-self-insert ?\")
  4654. (should (= (point) (1- (point-max))))
  4655. (should (string= (buffer-string)
  4656. "\"\n\"\"\"\n"))))
  4657. (or epm (electric-pair-mode -1)))))
  4658. ;;; Hideshow support
  4659. (ert-deftest python-hideshow-hide-levels-1 ()
  4660. "Should hide all methods when called after class start."
  4661. (let ((enabled hs-minor-mode))
  4662. (unwind-protect
  4663. (progn
  4664. (python-tests-with-temp-buffer
  4665. "
  4666. class SomeClass:
  4667. def __init__(self, arg, kwarg=1):
  4668. self.arg = arg
  4669. self.kwarg = kwarg
  4670. def filter(self, nums):
  4671. def fn(item):
  4672. return item in [self.arg, self.kwarg]
  4673. return filter(fn, nums)
  4674. def __str__(self):
  4675. return '%s-%s' % (self.arg, self.kwarg)
  4676. "
  4677. (hs-minor-mode 1)
  4678. (python-tests-look-at "class SomeClass:")
  4679. (forward-line)
  4680. (hs-hide-level 1)
  4681. (should
  4682. (string=
  4683. (python-tests-visible-string)
  4684. "
  4685. class SomeClass:
  4686. def __init__(self, arg, kwarg=1):
  4687. def filter(self, nums):
  4688. def __str__(self):"))))
  4689. (or enabled (hs-minor-mode -1)))))
  4690. (ert-deftest python-hideshow-hide-levels-2 ()
  4691. "Should hide nested methods and parens at end of defun."
  4692. (let ((enabled hs-minor-mode))
  4693. (unwind-protect
  4694. (progn
  4695. (python-tests-with-temp-buffer
  4696. "
  4697. class SomeClass:
  4698. def __init__(self, arg, kwarg=1):
  4699. self.arg = arg
  4700. self.kwarg = kwarg
  4701. def filter(self, nums):
  4702. def fn(item):
  4703. return item in [self.arg, self.kwarg]
  4704. return filter(fn, nums)
  4705. def __str__(self):
  4706. return '%s-%s' % (self.arg, self.kwarg)
  4707. "
  4708. (hs-minor-mode 1)
  4709. (python-tests-look-at "def fn(item):")
  4710. (hs-hide-block)
  4711. (should
  4712. (string=
  4713. (python-tests-visible-string)
  4714. "
  4715. class SomeClass:
  4716. def __init__(self, arg, kwarg=1):
  4717. self.arg = arg
  4718. self.kwarg = kwarg
  4719. def filter(self, nums):
  4720. def fn(item):
  4721. return filter(fn, nums)
  4722. def __str__(self):
  4723. return '%s-%s' % (self.arg, self.kwarg)
  4724. "))))
  4725. (or enabled (hs-minor-mode -1)))))
  4726. (provide 'python-tests)
  4727. ;; Local Variables:
  4728. ;; indent-tabs-mode: nil
  4729. ;; End:
  4730. ;;; python-tests.el ends here