1 ;;; python-tests.el --- Test suite for python.el
3 ;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Dependencies for testing:
33 (defmacro python-tests-with-temp-buffer (contents &rest body)
34 "Create a `python-mode' enabled temp buffer 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))
39 (let ((python-indent-guess-indent-offset nil))
42 (goto-char (point-min))
45 (defmacro python-tests-with-temp-file (contents &rest body)
46 "Create a `python-mode' enabled file with CONTENTS.
47 BODY is code to be executed within the temp buffer. Point is
48 always located at the beginning of buffer."
49 (declare (indent 1) (debug t))
50 ;; temp-file never actually used for anything?
51 `(let* ((temp-file (make-temp-file "python-tests" nil ".py"))
52 (buffer (find-file-noselect temp-file))
53 (python-indent-guess-indent-offset nil))
55 (with-current-buffer buffer
58 (goto-char (point-min))
60 (and buffer (kill-buffer buffer))
61 (delete-file temp-file))))
63 (defun python-tests-look-at (string &optional num restore-point)
64 "Move point at beginning of STRING in the current buffer.
65 Optional argument NUM defaults to 1 and is an integer indicating
66 how many occurrences must be found, when positive the search is
67 done forwards, otherwise backwards. When RESTORE-POINT is
68 non-nil the point is not moved but the position found is still
69 returned. When searching forward and point is already looking at
70 STRING, it is skipped so the next STRING occurrence is selected."
71 (let* ((num (or num 1))
72 (starting-point (point))
73 (string (regexp-quote string))
74 (search-fn (if (> num 0) #'re-search-forward #'re-search-backward))
75 (deinc-fn (if (> num 0) #'1- #'1+))
79 (while (not (= num 0))
82 ;; Moving forward and already looking at STRING, skip it.
83 (forward-char (length (match-string-no-properties 0))))
84 (and (not (funcall search-fn string nil t))
87 ;; `re-search-forward' leaves point at the end of the
88 ;; occurrence, move back so point is at the beginning
90 (forward-char (- (length (match-string-no-properties 0)))))
92 num (funcall deinc-fn num)
93 found-point (point))))
95 (and restore-point (goto-char starting-point)))))
97 (defun python-tests-self-insert (char-or-str)
98 "Call `self-insert-command' for chars in CHAR-OR-STR."
101 ((characterp char-or-str)
103 ((stringp char-or-str)
104 (string-to-list char-or-str))
106 (cl-remove-if #'characterp char-or-str))
108 (t (error "CHAR-OR-STR must be a char, string, or list of char")))))
111 (let ((last-command-event char))
112 (call-interactively 'self-insert-command)))
115 (defun python-tests-visible-string (&optional min max)
116 "Return the buffer string excluding invisible overlays.
117 Argument MIN and MAX delimit the region to be returned and
118 default to `point-min' and `point-max' respectively."
119 (let* ((min (or min (point-min)))
120 (max (or max (point-max)))
121 (buffer (current-buffer))
122 (buffer-contents (buffer-substring-no-properties min max))
124 (sort (overlays-in min max)
126 (let ((overlay-end-a (overlay-end a))
127 (overlay-end-b (overlay-end b)))
128 (> overlay-end-a overlay-end-b))))))
130 (insert buffer-contents)
131 (dolist (overlay overlays)
132 (if (overlay-get overlay 'invisible)
133 (delete-region (overlay-start overlay)
134 (overlay-end overlay))))
135 (buffer-substring-no-properties (point-min) (point-max)))))
138 ;;; Tests for your tests, so you can test while you test.
140 (ert-deftest python-tests-look-at-1 ()
141 "Test forward movement."
142 (python-tests-with-temp-buffer
143 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
144 sed do eiusmod tempor incididunt ut labore et dolore magna
146 (let ((expected (save-excursion
148 (re-search-forward "et" nil t))
151 (should (= (python-tests-look-at "et" 3 t) expected))
152 ;; Even if NUM is bigger than found occurrences the point of last
153 ;; one should be returned.
154 (should (= (python-tests-look-at "et" 6 t) expected))
155 ;; If already looking at STRING, it should skip it.
156 (dotimes (i 2) (re-search-forward "et"))
158 (should (= (python-tests-look-at "et") expected)))))
160 (ert-deftest python-tests-look-at-2 ()
161 "Test backward movement."
162 (python-tests-with-temp-buffer
163 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
164 sed do eiusmod tempor incididunt ut labore et dolore magna
168 (re-search-forward "et" nil t)
172 (re-search-forward "et" nil t))
173 (should (= (python-tests-look-at "et" -3 t) expected))
174 (should (= (python-tests-look-at "et" -6 t) expected)))))
180 ;;; Python specialized rx
183 ;;; Font-lock and syntax
185 (ert-deftest python-syntax-after-python-backspace ()
186 ;; `python-indent-dedent-line-backspace' garbles syntax
187 :expected-result :failed
188 (python-tests-with-temp-buffer
190 (goto-char (point-max))
191 (python-indent-dedent-line-backspace 1)
192 (should (string= (buffer-string) "\"\""))
193 (should (null (nth 3 (syntax-ppss))))))
198 ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
200 (ert-deftest python-indent-pep8-1 ()
202 (python-tests-with-temp-buffer
203 "# Aligned with opening delimiter
204 foo = long_function_name(var_one, var_two,
207 (should (eq (car (python-indent-context)) :no-indent))
208 (should (= (python-indent-calculate-indentation) 0))
209 (python-tests-look-at "foo = long_function_name(var_one, var_two,")
210 (should (eq (car (python-indent-context)) :after-comment))
211 (should (= (python-indent-calculate-indentation) 0))
212 (python-tests-look-at "var_three, var_four)")
213 (should (eq (car (python-indent-context)) :inside-paren))
214 (should (= (python-indent-calculate-indentation) 25))))
216 (ert-deftest python-indent-pep8-2 ()
218 (python-tests-with-temp-buffer
219 "# More indentation included to distinguish this from the rest.
220 def long_function_name(
221 var_one, var_two, var_three,
225 (should (eq (car (python-indent-context)) :no-indent))
226 (should (= (python-indent-calculate-indentation) 0))
227 (python-tests-look-at "def long_function_name(")
228 (should (eq (car (python-indent-context)) :after-comment))
229 (should (= (python-indent-calculate-indentation) 0))
230 (python-tests-look-at "var_one, var_two, var_three,")
231 (should (eq (car (python-indent-context))
232 :inside-paren-newline-start-from-block))
233 (should (= (python-indent-calculate-indentation) 8))
234 (python-tests-look-at "var_four):")
235 (should (eq (car (python-indent-context))
236 :inside-paren-newline-start-from-block))
237 (should (= (python-indent-calculate-indentation) 8))
238 (python-tests-look-at "print (var_one)")
239 (should (eq (car (python-indent-context))
241 (should (= (python-indent-calculate-indentation) 4))))
243 (ert-deftest python-indent-pep8-3 ()
245 (python-tests-with-temp-buffer
246 "# Extra indentation is not necessary.
247 foo = long_function_name(
251 (should (eq (car (python-indent-context)) :no-indent))
252 (should (= (python-indent-calculate-indentation) 0))
253 (python-tests-look-at "foo = long_function_name(")
254 (should (eq (car (python-indent-context)) :after-comment))
255 (should (= (python-indent-calculate-indentation) 0))
256 (python-tests-look-at "var_one, var_two,")
257 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
258 (should (= (python-indent-calculate-indentation) 4))
259 (python-tests-look-at "var_three, var_four)")
260 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
261 (should (= (python-indent-calculate-indentation) 4))))
263 (ert-deftest python-indent-base-case ()
264 "Check base case does not trigger errors."
265 (python-tests-with-temp-buffer
269 (goto-char (point-min))
270 (should (eq (car (python-indent-context)) :no-indent))
271 (should (= (python-indent-calculate-indentation) 0))
273 (should (eq (car (python-indent-context)) :no-indent))
274 (should (= (python-indent-calculate-indentation) 0))
276 (should (eq (car (python-indent-context)) :no-indent))
277 (should (= (python-indent-calculate-indentation) 0))))
279 (ert-deftest python-indent-after-comment-1 ()
280 "The most simple after-comment case that shouldn't fail."
281 (python-tests-with-temp-buffer
282 "# Contents will be modified to correct indentation
284 def _on_child_complete(self, child_future):
285 if self.in_terminal_state():
287 # We only complete when all our async children have entered a
288 # terminal state. At that point, if any child failed, we fail
289 # with the exception with which the first child failed.
291 (python-tests-look-at "# We only complete")
292 (should (eq (car (python-indent-context)) :after-block-end))
293 (should (= (python-indent-calculate-indentation) 8))
294 (python-tests-look-at "# terminal state")
295 (should (eq (car (python-indent-context)) :after-comment))
296 (should (= (python-indent-calculate-indentation) 8))
297 (python-tests-look-at "# with the exception")
298 (should (eq (car (python-indent-context)) :after-comment))
299 ;; This one indents relative to previous block, even given the fact
300 ;; that it was under-indented.
301 (should (= (python-indent-calculate-indentation) 4))
302 (python-tests-look-at "# terminal state" -1)
303 ;; It doesn't hurt to check again.
304 (should (eq (car (python-indent-context)) :after-comment))
306 (should (= (current-indentation) 8))
307 (python-tests-look-at "# with the exception")
308 (should (eq (car (python-indent-context)) :after-comment))
309 ;; Now everything should be lined up.
310 (should (= (python-indent-calculate-indentation) 8))))
312 (ert-deftest python-indent-after-comment-2 ()
313 "Test after-comment in weird cases."
314 (python-tests-with-temp-buffer
315 "# Contents will be modified to correct indentation
319 # This comment is badly indented because the user forced so.
320 # At this line python.el wont dedent, user is always right.
322 comment_wins_over_ender = True
326 (python-tests-look-at "# I don't do much")
327 (should (eq (car (python-indent-context)) :after-block-start))
328 (should (= (python-indent-calculate-indentation) 4))
329 (python-tests-look-at "return arg")
330 ;; Comment here just gets ignored, this line is not a comment so
331 ;; the rules won't apply here.
332 (should (eq (car (python-indent-context)) :after-block-start))
333 (should (= (python-indent-calculate-indentation) 4))
334 (python-tests-look-at "# This comment is badly indented")
335 (should (eq (car (python-indent-context)) :after-block-end))
336 ;; The return keyword do make indentation lose a level...
337 (should (= (python-indent-calculate-indentation) 0))
338 ;; ...but the current indentation was forced by the user.
339 (python-tests-look-at "# At this line python.el wont dedent")
340 (should (eq (car (python-indent-context)) :after-comment))
341 (should (= (python-indent-calculate-indentation) 4))
342 ;; Should behave the same for blank lines: potentially a comment.
344 (should (eq (car (python-indent-context)) :after-comment))
345 (should (= (python-indent-calculate-indentation) 4))
346 (python-tests-look-at "comment_wins_over_ender")
347 ;; The comment won over the ender because the user said so.
348 (should (eq (car (python-indent-context)) :after-comment))
349 (should (= (python-indent-calculate-indentation) 4))
350 ;; The indentation calculated fine for the assignment, but the user
351 ;; choose to force it back to the first column. Next line should
353 (python-tests-look-at "# yeah, that.")
354 (should (eq (car (python-indent-context)) :after-line))
355 (should (= (python-indent-calculate-indentation) 0))))
357 (ert-deftest python-indent-after-comment-3 ()
358 "Test after-comment in buggy case."
359 (python-tests-with-temp-buffer
363 def something(self, arg):
370 def method(self, a, b):
373 (python-tests-look-at "@adecorator")
374 (should (eq (car (python-indent-context)) :after-comment))
375 (should (= (python-indent-calculate-indentation) 4))))
377 (ert-deftest python-indent-inside-paren-1 ()
378 "The most simple inside-paren case that shouldn't fail."
379 (python-tests-with-temp-buffer
397 (python-tests-look-at "data = {")
398 (should (eq (car (python-indent-context)) :no-indent))
399 (should (= (python-indent-calculate-indentation) 0))
400 (python-tests-look-at "'key':")
401 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
402 (should (= (python-indent-calculate-indentation) 4))
403 (python-tests-look-at "{")
404 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
405 (should (= (python-indent-calculate-indentation) 4))
406 (python-tests-look-at "'objlist': [")
407 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
408 (should (= (python-indent-calculate-indentation) 8))
409 (python-tests-look-at "{")
410 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
411 (should (= (python-indent-calculate-indentation) 12))
412 (python-tests-look-at "'pk': 1,")
413 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
414 (should (= (python-indent-calculate-indentation) 16))
415 (python-tests-look-at "'name': 'first',")
416 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
417 (should (= (python-indent-calculate-indentation) 16))
418 (python-tests-look-at "},")
419 (should (eq (car (python-indent-context))
420 :inside-paren-at-closing-nested-paren))
421 (should (= (python-indent-calculate-indentation) 12))
422 (python-tests-look-at "{")
423 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
424 (should (= (python-indent-calculate-indentation) 12))
425 (python-tests-look-at "'pk': 2,")
426 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
427 (should (= (python-indent-calculate-indentation) 16))
428 (python-tests-look-at "'name': 'second',")
429 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
430 (should (= (python-indent-calculate-indentation) 16))
431 (python-tests-look-at "}")
432 (should (eq (car (python-indent-context))
433 :inside-paren-at-closing-nested-paren))
434 (should (= (python-indent-calculate-indentation) 12))
435 (python-tests-look-at "]")
436 (should (eq (car (python-indent-context))
437 :inside-paren-at-closing-nested-paren))
438 (should (= (python-indent-calculate-indentation) 8))
439 (python-tests-look-at "}")
440 (should (eq (car (python-indent-context))
441 :inside-paren-at-closing-nested-paren))
442 (should (= (python-indent-calculate-indentation) 4))
443 (python-tests-look-at "}")
444 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
445 (should (= (python-indent-calculate-indentation) 0))))
447 (ert-deftest python-indent-inside-paren-2 ()
448 "Another more compact paren group style."
449 (python-tests-with-temp-buffer
460 (python-tests-look-at "data = {")
461 (should (eq (car (python-indent-context)) :no-indent))
462 (should (= (python-indent-calculate-indentation) 0))
463 (python-tests-look-at "'objlist': [")
464 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
465 (should (= (python-indent-calculate-indentation) 4))
466 (python-tests-look-at "{'pk': 1,")
467 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
468 (should (= (python-indent-calculate-indentation) 8))
469 (python-tests-look-at "'name': 'first'},")
470 (should (eq (car (python-indent-context)) :inside-paren))
471 (should (= (python-indent-calculate-indentation) 9))
472 (python-tests-look-at "{'pk': 2,")
473 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
474 (should (= (python-indent-calculate-indentation) 8))
475 (python-tests-look-at "'name': 'second'}")
476 (should (eq (car (python-indent-context)) :inside-paren))
477 (should (= (python-indent-calculate-indentation) 9))
478 (python-tests-look-at "]")
479 (should (eq (car (python-indent-context))
480 :inside-paren-at-closing-nested-paren))
481 (should (= (python-indent-calculate-indentation) 4))
482 (python-tests-look-at "}}")
483 (should (eq (car (python-indent-context))
484 :inside-paren-at-closing-nested-paren))
485 (should (= (python-indent-calculate-indentation) 0))
486 (python-tests-look-at "}")
487 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
488 (should (= (python-indent-calculate-indentation) 0))))
490 (ert-deftest python-indent-inside-paren-3 ()
491 "The simplest case possible."
492 (python-tests-with-temp-buffer
499 (python-tests-look-at "data = ('these',")
500 (should (eq (car (python-indent-context)) :no-indent))
501 (should (= (python-indent-calculate-indentation) 0))
503 (should (eq (car (python-indent-context)) :inside-paren))
504 (should (= (python-indent-calculate-indentation) 8))
506 (should (eq (car (python-indent-context)) :inside-paren))
507 (should (= (python-indent-calculate-indentation) 8))
509 (should (eq (car (python-indent-context)) :inside-paren))
510 (should (= (python-indent-calculate-indentation) 8))))
512 (ert-deftest python-indent-inside-paren-4 ()
513 "Respect indentation of first column."
514 (python-tests-with-temp-buffer
516 data = [ [ 'these', 'are'],
519 (python-tests-look-at "data = [ [ 'these', 'are'],")
520 (should (eq (car (python-indent-context)) :no-indent))
521 (should (= (python-indent-calculate-indentation) 0))
523 (should (eq (car (python-indent-context)) :inside-paren))
524 (should (= (python-indent-calculate-indentation) 9))))
526 (ert-deftest python-indent-inside-paren-5 ()
527 "Test when :inside-paren initial parens are skipped in context start."
528 (python-tests-with-temp-buffer
530 while ((not some_condition) and
532 do_something_interesting(
535 (python-tests-look-at "while ((not some_condition) and")
536 (should (eq (car (python-indent-context)) :no-indent))
537 (should (= (python-indent-calculate-indentation) 0))
539 (should (eq (car (python-indent-context)) :inside-paren))
540 (should (= (python-indent-calculate-indentation) 7))
542 (should (eq (car (python-indent-context)) :after-block-start))
543 (should (= (python-indent-calculate-indentation) 4))
545 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
546 (should (= (python-indent-calculate-indentation) 8))))
548 (ert-deftest python-indent-inside-paren-6 ()
549 "This should be aligned.."
550 (python-tests-with-temp-buffer
552 CHOICES = (('some', 'choice'),
553 ('another', 'choice'),
556 (python-tests-look-at "CHOICES = (('some', 'choice'),")
557 (should (eq (car (python-indent-context)) :no-indent))
558 (should (= (python-indent-calculate-indentation) 0))
560 (should (eq (car (python-indent-context)) :inside-paren))
561 (should (= (python-indent-calculate-indentation) 11))
563 (should (eq (car (python-indent-context)) :inside-paren))
564 (should (= (python-indent-calculate-indentation) 11))))
566 (ert-deftest python-indent-inside-paren-7 ()
567 "Test for Bug#21762."
568 (python-tests-with-temp-buffer
569 "import re as myre\nvar = [\n"
570 (goto-char (point-max))
571 ;; This signals an error if the test fails
572 (should (eq (car (python-indent-context)) :inside-paren-newline-start))))
574 (ert-deftest python-indent-after-block-1 ()
575 "The most simple after-block case that shouldn't fail."
576 (python-tests-with-temp-buffer
578 def foo(a, b, c=True):
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))))
586 (ert-deftest python-indent-after-block-2 ()
587 "A weird (malformed) multiline block statement."
588 (python-tests-with-temp-buffer
594 (goto-char (point-max))
595 (should (eq (car (python-indent-context)) :after-block-start))
596 (should (= (python-indent-calculate-indentation) 4))))
598 (ert-deftest python-indent-after-block-3 ()
599 "A weird (malformed) sample, usually found in python shells."
600 (python-tests-with-temp-buffer
609 (python-tests-look-at "pass")
610 (should (eq (car (python-indent-context)) :after-block-start))
611 (should (= (python-indent-calculate-indentation) 4))
612 (python-tests-look-at "something")
614 (should (eq (car (python-indent-context)) :after-line))
615 (should (= (python-indent-calculate-indentation) 0))))
617 (ert-deftest python-indent-after-backslash-1 ()
618 "The most common case."
619 (python-tests-with-temp-buffer
621 from foo.bar.baz import something, something_1 \\\\
622 something_2 something_3, \\\\
623 something_4, something_5
625 (python-tests-look-at "from foo.bar.baz import something, something_1")
626 (should (eq (car (python-indent-context)) :no-indent))
627 (should (= (python-indent-calculate-indentation) 0))
628 (python-tests-look-at "something_2 something_3,")
629 (should (eq (car (python-indent-context)) :after-backslash-first-line))
630 (should (= (python-indent-calculate-indentation) 4))
631 (python-tests-look-at "something_4, something_5")
632 (should (eq (car (python-indent-context)) :after-backslash))
633 (should (= (python-indent-calculate-indentation) 4))
634 (goto-char (point-max))
635 (should (eq (car (python-indent-context)) :after-line))
636 (should (= (python-indent-calculate-indentation) 0))))
638 (ert-deftest python-indent-after-backslash-2 ()
639 "A pretty extreme complicated case."
640 (python-tests-with-temp-buffer
642 objects = Thing.objects.all() \\\\
652 (python-tests-look-at "objects = Thing.objects.all()")
653 (should (eq (car (python-indent-context)) :no-indent))
654 (should (= (python-indent-calculate-indentation) 0))
655 (python-tests-look-at ".filter(")
656 (should (eq (car (python-indent-context))
657 :after-backslash-dotted-continuation))
658 (should (= (python-indent-calculate-indentation) 23))
659 (python-tests-look-at "type='toy',")
660 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
661 (should (= (python-indent-calculate-indentation) 27))
662 (python-tests-look-at "status='bought'")
663 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
664 (should (= (python-indent-calculate-indentation) 27))
665 (python-tests-look-at ") \\\\")
666 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
667 (should (= (python-indent-calculate-indentation) 23))
668 (python-tests-look-at ".aggregate(")
669 (should (eq (car (python-indent-context))
670 :after-backslash-dotted-continuation))
671 (should (= (python-indent-calculate-indentation) 23))
672 (python-tests-look-at "Sum('amount')")
673 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
674 (should (= (python-indent-calculate-indentation) 27))
675 (python-tests-look-at ") \\\\")
676 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
677 (should (= (python-indent-calculate-indentation) 23))
678 (python-tests-look-at ".values_list()")
679 (should (eq (car (python-indent-context))
680 :after-backslash-dotted-continuation))
681 (should (= (python-indent-calculate-indentation) 23))
683 (should (eq (car (python-indent-context)) :after-line))
684 (should (= (python-indent-calculate-indentation) 0))))
686 (ert-deftest python-indent-after-backslash-3 ()
687 "Backslash continuation from block start."
688 (python-tests-with-temp-buffer
690 with open('/path/to/some/file/you/want/to/read') as file_1, \\\\
691 open('/path/to/some/file/being/written', 'w') as file_2:
692 file_2.write(file_1.read())
694 (python-tests-look-at
695 "with open('/path/to/some/file/you/want/to/read') as file_1, \\\\")
696 (should (eq (car (python-indent-context)) :no-indent))
697 (should (= (python-indent-calculate-indentation) 0))
698 (python-tests-look-at
699 "open('/path/to/some/file/being/written', 'w') as file_2")
700 (should (eq (car (python-indent-context))
701 :after-backslash-block-continuation))
702 (should (= (python-indent-calculate-indentation) 5))
703 (python-tests-look-at "file_2.write(file_1.read())")
704 (should (eq (car (python-indent-context)) :after-block-start))
705 (should (= (python-indent-calculate-indentation) 4))))
707 (ert-deftest python-indent-after-backslash-4 ()
708 "Backslash continuation from assignment."
709 (python-tests-with-temp-buffer
711 super_awful_assignment = some_calculation() and \\\\
712 another_calculation() and \\\\
713 some_final_calculation()
715 (python-tests-look-at
716 "super_awful_assignment = some_calculation() and \\\\")
717 (should (eq (car (python-indent-context)) :no-indent))
718 (should (= (python-indent-calculate-indentation) 0))
719 (python-tests-look-at "another_calculation() and \\\\")
720 (should (eq (car (python-indent-context))
721 :after-backslash-assignment-continuation))
722 (should (= (python-indent-calculate-indentation) 25))
723 (python-tests-look-at "some_final_calculation()")
724 (should (eq (car (python-indent-context)) :after-backslash))
725 (should (= (python-indent-calculate-indentation) 25))))
727 (ert-deftest python-indent-after-backslash-5 ()
728 "Dotted continuation bizarre example."
729 (python-tests-with-temp-buffer
731 def delete_all_things():
736 (python-tests-look-at "Thing \\\\")
737 (should (eq (car (python-indent-context)) :after-block-start))
738 (should (= (python-indent-calculate-indentation) 4))
739 (python-tests-look-at ".objects.all() \\\\")
740 (should (eq (car (python-indent-context)) :after-backslash-first-line))
741 (should (= (python-indent-calculate-indentation) 8))
742 (python-tests-look-at ".delete()")
743 (should (eq (car (python-indent-context))
744 :after-backslash-dotted-continuation))
745 (should (= (python-indent-calculate-indentation) 16))))
747 (ert-deftest python-indent-block-enders-1 ()
748 "Test de-indentation for pass keyword."
749 (python-tests-with-temp-buffer
762 (python-tests-look-at "3)")
764 (should (= (python-indent-calculate-indentation) 8))
765 (python-tests-look-at "pass")
767 (should (eq (car (python-indent-context)) :after-block-end))
768 (should (= (python-indent-calculate-indentation) 8))))
770 (ert-deftest python-indent-block-enders-2 ()
771 "Test de-indentation for return keyword."
772 (python-tests-with-temp-buffer
775 '''raise lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
777 eiusmod tempor incididunt ut labore et dolore magna aliqua.
780 \"return (1, 2, 3).\"
786 (python-tests-look-at "def")
787 (should (= (python-indent-calculate-indentation) 4))
788 (python-tests-look-at "if")
789 (should (= (python-indent-calculate-indentation) 8))
790 (python-tests-look-at "return")
791 (should (= (python-indent-calculate-indentation) 12))
792 (goto-char (point-max))
793 (should (eq (car (python-indent-context)) :after-block-end))
794 (should (= (python-indent-calculate-indentation) 8))))
796 (ert-deftest python-indent-block-enders-3 ()
797 "Test de-indentation for continue keyword."
798 (python-tests-with-temp-buffer
804 (python-tests-look-at "if")
805 (should (= (python-indent-calculate-indentation) 4))
806 (python-tests-look-at "continue")
807 (should (= (python-indent-calculate-indentation) 8))
809 (should (eq (car (python-indent-context)) :after-block-end))
810 (should (= (python-indent-calculate-indentation) 4))))
812 (ert-deftest python-indent-block-enders-4 ()
813 "Test de-indentation for break keyword."
814 (python-tests-with-temp-buffer
820 (python-tests-look-at "if")
821 (should (= (python-indent-calculate-indentation) 4))
822 (python-tests-look-at "break")
823 (should (= (python-indent-calculate-indentation) 8))
825 (should (eq (car (python-indent-context)) :after-block-end))
826 (should (= (python-indent-calculate-indentation) 4))))
828 (ert-deftest python-indent-block-enders-5 ()
829 "Test de-indentation for raise keyword."
830 (python-tests-with-temp-buffer
834 raise ValueError('Element cannot be None')
836 (python-tests-look-at "if")
837 (should (= (python-indent-calculate-indentation) 4))
838 (python-tests-look-at "raise")
839 (should (= (python-indent-calculate-indentation) 8))
841 (should (eq (car (python-indent-context)) :after-block-end))
842 (should (= (python-indent-calculate-indentation) 4))))
844 (ert-deftest python-indent-dedenters-1 ()
845 "Test de-indentation for the elif keyword."
846 (python-tests-with-temp-buffer
855 (python-tests-look-at "elif\n")
856 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
857 (should (= (python-indent-calculate-indentation) 0))
858 (should (= (python-indent-calculate-indentation t) 0))))
860 (ert-deftest python-indent-dedenters-2 ()
861 "Test de-indentation for the else keyword."
862 (python-tests-with-temp-buffer
868 msg = 'Error saving to disk'
870 logger.exception(msg)
873 logger.exception('Unhandled exception')
878 (python-tests-look-at "else\n")
879 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
880 (should (= (python-indent-calculate-indentation) 8))
881 (python-indent-line t)
882 (should (= (python-indent-calculate-indentation t) 4))
883 (python-indent-line t)
884 (should (= (python-indent-calculate-indentation t) 0))
885 (python-indent-line t)
886 (should (= (python-indent-calculate-indentation t) 8))))
888 (ert-deftest python-indent-dedenters-3 ()
889 "Test de-indentation for the except keyword."
890 (python-tests-with-temp-buffer
897 (python-tests-look-at "except\n")
898 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
899 (should (= (python-indent-calculate-indentation) 4))
900 (python-indent-line t)
901 (should (= (python-indent-calculate-indentation t) 4))))
903 (ert-deftest python-indent-dedenters-4 ()
904 "Test de-indentation for the finally keyword."
905 (python-tests-with-temp-buffer
912 (python-tests-look-at "finally\n")
913 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
914 (should (= (python-indent-calculate-indentation) 4))
915 (python-indent-line t)
916 (should (= (python-indent-calculate-indentation) 4))))
918 (ert-deftest python-indent-dedenters-5 ()
919 "Test invalid levels are skipped in a complex example."
920 (python-tests-with-temp-buffer
926 msg = 'Error saving to disk'
928 logger.exception(msg)
934 (python-tests-look-at "else\n")
935 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
936 (should (= (python-indent-calculate-indentation) 8))
937 (should (= (python-indent-calculate-indentation t) 0))
938 (python-indent-line t)
939 (should (= (python-indent-calculate-indentation t) 8))))
941 (ert-deftest python-indent-dedenters-6 ()
942 "Test indentation is zero when no opening block for dedenter."
943 (python-tests-with-temp-buffer
950 (python-tests-look-at "else\n")
951 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
952 (should (= (python-indent-calculate-indentation) 0))
953 (should (= (python-indent-calculate-indentation t) 0))))
955 (ert-deftest python-indent-dedenters-7 ()
956 "Test indentation case from Bug#15163."
957 (python-tests-with-temp-buffer
966 (python-tests-look-at "else:" 2)
967 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
968 (should (= (python-indent-calculate-indentation) 0))
969 (should (= (python-indent-calculate-indentation t) 0))))
971 (ert-deftest python-indent-dedenters-8 ()
972 "Test indentation for Bug#18432."
973 (python-tests-with-temp-buffer
981 (python-tests-look-at "elif (a == 3 or")
982 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
983 (should (= (python-indent-calculate-indentation) 0))
984 (should (= (python-indent-calculate-indentation t) 0))
985 (python-tests-look-at "a == 4):\n")
986 (should (eq (car (python-indent-context)) :inside-paren))
987 (should (= (python-indent-calculate-indentation) 6))
989 (should (= (python-indent-calculate-indentation t) 4))
990 (python-indent-line t)
991 (should (= (python-indent-calculate-indentation t) 0))
992 (python-indent-line t)
993 (should (= (python-indent-calculate-indentation t) 6))))
995 (ert-deftest python-indent-inside-string-1 ()
996 "Test indentation for strings."
997 (python-tests-with-temp-buffer
1005 (python-tests-look-at "multiline = '''")
1006 (should (eq (car (python-indent-context)) :no-indent))
1007 (should (= (python-indent-calculate-indentation) 0))
1008 (python-tests-look-at "bunch")
1009 (should (eq (car (python-indent-context)) :inside-string))
1010 (should (= (python-indent-calculate-indentation) 0))
1011 (python-tests-look-at "of")
1012 (should (eq (car (python-indent-context)) :inside-string))
1013 (should (= (python-indent-calculate-indentation) 0))
1014 (python-tests-look-at "lines")
1015 (should (eq (car (python-indent-context)) :inside-string))
1016 (should (= (python-indent-calculate-indentation) 0))
1017 (python-tests-look-at "'''")
1018 (should (eq (car (python-indent-context)) :inside-string))
1019 (should (= (python-indent-calculate-indentation) 0))))
1021 (ert-deftest python-indent-inside-string-2 ()
1022 "Test indentation for docstrings."
1023 (python-tests-with-temp-buffer
1025 def fn(a, b, c=True):
1032 (python-tests-look-at "'''docstring")
1033 (should (eq (car (python-indent-context)) :after-block-start))
1034 (should (= (python-indent-calculate-indentation) 4))
1035 (python-tests-look-at "bunch")
1036 (should (eq (car (python-indent-context)) :inside-docstring))
1037 (should (= (python-indent-calculate-indentation) 4))
1038 (python-tests-look-at "of")
1039 (should (eq (car (python-indent-context)) :inside-docstring))
1040 ;; Any indentation deeper than the base-indent must remain unmodified.
1041 (should (= (python-indent-calculate-indentation) 8))
1042 (python-tests-look-at "lines")
1043 (should (eq (car (python-indent-context)) :inside-docstring))
1044 (should (= (python-indent-calculate-indentation) 4))
1045 (python-tests-look-at "'''")
1046 (should (eq (car (python-indent-context)) :inside-docstring))
1047 (should (= (python-indent-calculate-indentation) 4))))
1049 (ert-deftest python-indent-inside-string-3 ()
1050 "Test indentation for nested strings."
1051 (python-tests-with-temp-buffer
1053 def fn(a, b, c=True):
1060 (python-tests-look-at "some_var = '''")
1061 (should (eq (car (python-indent-context)) :after-block-start))
1062 (should (= (python-indent-calculate-indentation) 4))
1063 (python-tests-look-at "bunch")
1064 (should (eq (car (python-indent-context)) :inside-string))
1065 (should (= (python-indent-calculate-indentation) 4))
1066 (python-tests-look-at "of")
1067 (should (eq (car (python-indent-context)) :inside-string))
1068 (should (= (python-indent-calculate-indentation) 4))
1069 (python-tests-look-at "lines")
1070 (should (eq (car (python-indent-context)) :inside-string))
1071 (should (= (python-indent-calculate-indentation) 4))
1072 (python-tests-look-at "'''")
1073 (should (eq (car (python-indent-context)) :inside-string))
1074 (should (= (python-indent-calculate-indentation) 4))))
1076 (ert-deftest python-indent-electric-colon-1 ()
1077 "Test indentation case from Bug#18228."
1078 (python-tests-with-temp-buffer
1085 (python-tests-look-at "def b()")
1086 (goto-char (line-end-position))
1087 (python-tests-self-insert ":")
1088 (should (= (current-indentation) 0))))
1090 (ert-deftest python-indent-electric-colon-2 ()
1091 "Test indentation case for dedenter."
1092 (python-tests-with-temp-buffer
1098 (python-tests-look-at "else")
1099 (goto-char (line-end-position))
1100 (python-tests-self-insert ":")
1101 (should (= (current-indentation) 0))))
1103 (ert-deftest python-indent-electric-colon-3 ()
1104 "Test indentation case for multi-line dedenter."
1105 (python-tests-with-temp-buffer
1113 (python-tests-look-at "that)")
1114 (goto-char (line-end-position))
1115 (python-tests-self-insert ":")
1116 (python-tests-look-at "elif" -1)
1117 (should (= (current-indentation) 0))
1118 (python-tests-look-at "and")
1119 (should (= (current-indentation) 6))
1120 (python-tests-look-at "that)")
1121 (should (= (current-indentation) 6))))
1123 (ert-deftest python-indent-region-1 ()
1124 "Test indentation case from Bug#18843."
1132 (python-tests-with-temp-buffer
1134 (python-indent-region (point-min) (point-max))
1135 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1138 (ert-deftest python-indent-region-2 ()
1139 "Test region indentation on comments."
1149 (python-tests-with-temp-buffer
1151 (python-indent-region (point-min) (point-max))
1152 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1155 (ert-deftest python-indent-region-3 ()
1156 "Test region indentation on comments."
1173 (python-tests-with-temp-buffer
1175 (python-indent-region (point-min) (point-max))
1176 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1179 (ert-deftest python-indent-region-4 ()
1180 "Test region indentation block starts, dedenters and enders."
1197 (python-tests-with-temp-buffer
1199 (python-indent-region (point-min) (point-max))
1200 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1203 (ert-deftest python-indent-region-5 ()
1204 "Test region indentation for docstrings."
1214 this is an arbitrarily
1228 this is an arbitrarily
1233 (python-tests-with-temp-buffer
1235 (python-indent-region (point-min) (point-max))
1236 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1242 (ert-deftest python-mark-defun-1 ()
1243 """Test `python-mark-defun' with point at defun symbol start."""
1244 (python-tests-with-temp-buffer
1263 (let ((expected-mark-beginning-position
1265 (python-tests-look-at "class A:")
1267 (expected-mark-end-position-1
1269 (python-tests-look-at "pass")
1272 (expected-mark-end-position-2
1274 (python-tests-look-at "return self.b")
1277 (expected-mark-end-position-3
1279 (python-tests-look-at "'''docstring'''")
1282 ;; Select class A only, with point at bol.
1283 (python-mark-defun 1)
1284 (should (= (point) expected-mark-beginning-position))
1285 (should (= (marker-position (mark-marker))
1286 expected-mark-end-position-1))
1287 ;; expand to class B, start position should remain the same.
1288 (python-mark-defun 1)
1289 (should (= (point) expected-mark-beginning-position))
1290 (should (= (marker-position (mark-marker))
1291 expected-mark-end-position-2))
1292 ;; expand to class C, start position should remain the same.
1293 (python-mark-defun 1)
1294 (should (= (point) expected-mark-beginning-position))
1295 (should (= (marker-position (mark-marker))
1296 expected-mark-end-position-3)))))
1298 (ert-deftest python-mark-defun-2 ()
1299 """Test `python-mark-defun' with point at nested defun symbol start."""
1300 (python-tests-with-temp-buffer
1319 (let ((expected-mark-beginning-position
1321 (python-tests-look-at "def __init__(self):")
1322 (1- (line-beginning-position))))
1323 (expected-mark-end-position-1
1325 (python-tests-look-at "self.b = 'b'")
1328 (expected-mark-end-position-2
1330 (python-tests-look-at "return self.b")
1333 (expected-mark-end-position-3
1335 (python-tests-look-at "'''docstring'''")
1338 ;; Select B.__init only, with point at its start.
1339 (python-mark-defun 1)
1340 (should (= (point) expected-mark-beginning-position))
1341 (should (= (marker-position (mark-marker))
1342 expected-mark-end-position-1))
1343 ;; expand to B.fun, start position should remain the same.
1344 (python-mark-defun 1)
1345 (should (= (point) expected-mark-beginning-position))
1346 (should (= (marker-position (mark-marker))
1347 expected-mark-end-position-2))
1348 ;; expand to class C, start position should remain the same.
1349 (python-mark-defun 1)
1350 (should (= (point) expected-mark-beginning-position))
1351 (should (= (marker-position (mark-marker))
1352 expected-mark-end-position-3)))))
1354 (ert-deftest python-mark-defun-3 ()
1355 """Test `python-mark-defun' with point inside defun symbol."""
1356 (python-tests-with-temp-buffer
1375 (let ((expected-mark-beginning-position
1377 (python-tests-look-at "def fun(self):")
1378 (python-tests-look-at "(self):")
1379 (1- (line-beginning-position))))
1380 (expected-mark-end-position
1382 (python-tests-look-at "return self.b")
1385 ;; Should select B.fun, despite point is inside the defun symbol.
1386 (python-mark-defun 1)
1387 (should (= (point) expected-mark-beginning-position))
1388 (should (= (marker-position (mark-marker))
1389 expected-mark-end-position)))))
1394 (ert-deftest python-nav-beginning-of-defun-1 ()
1395 (python-tests-with-temp-buffer
1397 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1398 '''print decorated function call data to stdout.
1402 @decoratorFunctionWithArguments('arg1', 'arg2')
1403 def func(a, b, c=True):
1408 print 'Inside wwrap()'
1409 def wrapped_f(*args):
1410 print 'Inside wrapped_f()'
1411 print 'Decorator arguments:', arg1, arg2, arg3
1413 print 'After f(*args)'
1417 (python-tests-look-at "return wrap")
1418 (should (= (save-excursion
1419 (python-nav-beginning-of-defun)
1422 (python-tests-look-at "def wrapped_f(*args):" -1)
1425 (python-tests-look-at "def wrapped_f(*args):" -1)
1426 (should (= (save-excursion
1427 (python-nav-beginning-of-defun)
1430 (python-tests-look-at "def wwrap(f):" -1)
1433 (python-tests-look-at "def wwrap(f):" -1)
1434 (should (= (save-excursion
1435 (python-nav-beginning-of-defun)
1438 (python-tests-look-at "def decoratorFunctionWithArguments" -1)
1442 (ert-deftest python-nav-beginning-of-defun-2 ()
1443 (python-tests-with-temp-buffer
1459 ;; Nested defuns, are handled with care.
1460 (python-tests-look-at "def c(self):")
1461 (should (= (save-excursion
1462 (python-nav-beginning-of-defun)
1465 (python-tests-look-at "def m(self):" -1)
1468 ;; Defuns on same levels should be respected.
1469 (python-tests-look-at "def a():" -1)
1470 (should (= (save-excursion
1471 (python-nav-beginning-of-defun)
1474 (python-tests-look-at "def b():" -1)
1477 ;; Jump to a top level defun.
1478 (python-tests-look-at "def b():" -1)
1479 (should (= (save-excursion
1480 (python-nav-beginning-of-defun)
1483 (python-tests-look-at "def m(self):" -1)
1486 ;; Jump to a top level defun again.
1487 (python-tests-look-at "def m(self):" -1)
1488 (should (= (save-excursion
1489 (python-nav-beginning-of-defun)
1492 (python-tests-look-at "class C(object):" -1)
1496 (ert-deftest python-nav-end-of-defun-1 ()
1497 (python-tests-with-temp-buffer
1513 (should (= (save-excursion
1514 (python-tests-look-at "class C(object):")
1515 (python-nav-end-of-defun)
1519 (should (= (save-excursion
1520 (python-tests-look-at "def m(self):")
1521 (python-nav-end-of-defun)
1524 (python-tests-look-at "def c(self):")
1527 (should (= (save-excursion
1528 (python-tests-look-at "def b():")
1529 (python-nav-end-of-defun)
1532 (python-tests-look-at "def b():")
1535 (should (= (save-excursion
1536 (python-tests-look-at "def c(self):")
1537 (python-nav-end-of-defun)
1542 (ert-deftest python-nav-end-of-defun-2 ()
1543 (python-tests-with-temp-buffer
1545 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1546 '''print decorated function call data to stdout.
1550 @decoratorFunctionWithArguments('arg1', 'arg2')
1551 def func(a, b, c=True):
1556 print 'Inside wwrap()'
1557 def wrapped_f(*args):
1558 print 'Inside wrapped_f()'
1559 print 'Decorator arguments:', arg1, arg2, arg3
1561 print 'After f(*args)'
1565 (should (= (save-excursion
1566 (python-tests-look-at "def decoratorFunctionWithArguments")
1567 (python-nav-end-of-defun)
1571 (should (= (save-excursion
1572 (python-tests-look-at "@decoratorFunctionWithArguments")
1573 (python-nav-end-of-defun)
1577 (should (= (save-excursion
1578 (python-tests-look-at "def wwrap(f):")
1579 (python-nav-end-of-defun)
1582 (python-tests-look-at "return wwrap")
1583 (line-beginning-position))))
1584 (should (= (save-excursion
1585 (python-tests-look-at "def wrapped_f(*args):")
1586 (python-nav-end-of-defun)
1589 (python-tests-look-at "return wrapped_f")
1590 (line-beginning-position))))
1591 (should (= (save-excursion
1592 (python-tests-look-at "f(*args)")
1593 (python-nav-end-of-defun)
1596 (python-tests-look-at "return wrapped_f")
1597 (line-beginning-position))))))
1599 (ert-deftest python-nav-backward-defun-1 ()
1600 (python-tests-with-temp-buffer
1602 class A(object): # A
1610 class B(object): # B
1612 class C(object): # C
1626 (goto-char (point-max))
1627 (should (= (save-excursion (python-nav-backward-defun))
1628 (python-tests-look-at " def c(self): # c" -1)))
1629 (should (= (save-excursion (python-nav-backward-defun))
1630 (python-tests-look-at " def d(self): # d" -1)))
1631 (should (= (save-excursion (python-nav-backward-defun))
1632 (python-tests-look-at " class C(object): # C" -1)))
1633 (should (= (save-excursion (python-nav-backward-defun))
1634 (python-tests-look-at " class B(object): # B" -1)))
1635 (should (= (save-excursion (python-nav-backward-defun))
1636 (python-tests-look-at " def b(self): # b" -1)))
1637 (should (= (save-excursion (python-nav-backward-defun))
1638 (python-tests-look-at " def a(self): # a" -1)))
1639 (should (= (save-excursion (python-nav-backward-defun))
1640 (python-tests-look-at "class A(object): # A" -1)))
1641 (should (not (python-nav-backward-defun)))))
1643 (ert-deftest python-nav-backward-defun-2 ()
1644 (python-tests-with-temp-buffer
1646 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1647 '''print decorated function call data to stdout.
1651 @decoratorFunctionWithArguments('arg1', 'arg2')
1652 def func(a, b, c=True):
1657 print 'Inside wwrap()'
1658 def wrapped_f(*args):
1659 print 'Inside wrapped_f()'
1660 print 'Decorator arguments:', arg1, arg2, arg3
1662 print 'After f(*args)'
1666 (goto-char (point-max))
1667 (should (= (save-excursion (python-nav-backward-defun))
1668 (python-tests-look-at " def wrapped_f(*args):" -1)))
1669 (should (= (save-excursion (python-nav-backward-defun))
1670 (python-tests-look-at " def wwrap(f):" -1)))
1671 (should (= (save-excursion (python-nav-backward-defun))
1672 (python-tests-look-at "def decoratorFunctionWithArguments(arg1, arg2, arg3):" -1)))
1673 (should (not (python-nav-backward-defun)))))
1675 (ert-deftest python-nav-backward-defun-3 ()
1676 (python-tests-with-temp-buffer
1692 (goto-char (point-min))
1693 (let ((point (python-tests-look-at "class A(object):")))
1694 (should (not (python-nav-backward-defun)))
1695 (should (= point (point))))))
1697 (ert-deftest python-nav-forward-defun-1 ()
1698 (python-tests-with-temp-buffer
1700 class A(object): # A
1708 class B(object): # B
1710 class C(object): # C
1724 (goto-char (point-min))
1725 (should (= (save-excursion (python-nav-forward-defun))
1726 (python-tests-look-at "(object): # A")))
1727 (should (= (save-excursion (python-nav-forward-defun))
1728 (python-tests-look-at "(self): # a")))
1729 (should (= (save-excursion (python-nav-forward-defun))
1730 (python-tests-look-at "(self): # b")))
1731 (should (= (save-excursion (python-nav-forward-defun))
1732 (python-tests-look-at "(object): # B")))
1733 (should (= (save-excursion (python-nav-forward-defun))
1734 (python-tests-look-at "(object): # C")))
1735 (should (= (save-excursion (python-nav-forward-defun))
1736 (python-tests-look-at "(self): # d")))
1737 (should (= (save-excursion (python-nav-forward-defun))
1738 (python-tests-look-at "(self): # c")))
1739 (should (not (python-nav-forward-defun)))))
1741 (ert-deftest python-nav-forward-defun-2 ()
1742 (python-tests-with-temp-buffer
1744 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1745 '''print decorated function call data to stdout.
1749 @decoratorFunctionWithArguments('arg1', 'arg2')
1750 def func(a, b, c=True):
1755 print 'Inside wwrap()'
1756 def wrapped_f(*args):
1757 print 'Inside wrapped_f()'
1758 print 'Decorator arguments:', arg1, arg2, arg3
1760 print 'After f(*args)'
1764 (goto-char (point-min))
1765 (should (= (save-excursion (python-nav-forward-defun))
1766 (python-tests-look-at "(arg1, arg2, arg3):")))
1767 (should (= (save-excursion (python-nav-forward-defun))
1768 (python-tests-look-at "(f):")))
1769 (should (= (save-excursion (python-nav-forward-defun))
1770 (python-tests-look-at "(*args):")))
1771 (should (not (python-nav-forward-defun)))))
1773 (ert-deftest python-nav-forward-defun-3 ()
1774 (python-tests-with-temp-buffer
1790 (goto-char (point-min))
1791 (let ((point (python-tests-look-at "(object):")))
1792 (should (not (python-nav-forward-defun)))
1793 (should (= point (point))))))
1795 (ert-deftest python-nav-beginning-of-statement-1 ()
1796 (python-tests-with-temp-buffer
1806 v3 = ('this is a string'
1811 # this is a comment, yo
1812 'continue previous line')
1818 (python-tests-look-at "v2 =")
1819 (python-util-forward-comment -1)
1820 (should (= (save-excursion
1821 (python-nav-beginning-of-statement)
1823 (python-tests-look-at "v1 =" -1 t)))
1824 (python-tests-look-at "v3 =")
1825 (python-util-forward-comment -1)
1826 (should (= (save-excursion
1827 (python-nav-beginning-of-statement)
1829 (python-tests-look-at "v2 =" -1 t)))
1830 (python-tests-look-at "v4 =")
1831 (python-util-forward-comment -1)
1832 (should (= (save-excursion
1833 (python-nav-beginning-of-statement)
1835 (python-tests-look-at "v3 =" -1 t)))
1836 (goto-char (point-max))
1837 (python-util-forward-comment -1)
1838 (should (= (save-excursion
1839 (python-nav-beginning-of-statement)
1841 (python-tests-look-at "v4 =" -1 t)))))
1843 (ert-deftest python-nav-end-of-statement-1 ()
1844 (python-tests-with-temp-buffer
1854 v3 = ('this is a string'
1859 # this is a comment, yo
1860 'continue previous line')
1866 (python-tests-look-at "v1 =")
1867 (should (= (save-excursion
1868 (python-nav-end-of-statement)
1871 (python-tests-look-at "789")
1872 (line-end-position))))
1873 (python-tests-look-at "v2 =")
1874 (should (= (save-excursion
1875 (python-nav-end-of-statement)
1878 (python-tests-look-at "value4)")
1879 (line-end-position))))
1880 (python-tests-look-at "v3 =")
1881 (should (= (save-excursion
1882 (python-nav-end-of-statement)
1885 (python-tests-look-at
1886 "'continue previous line')")
1887 (line-end-position))))
1888 (python-tests-look-at "v4 =")
1889 (should (= (save-excursion
1890 (python-nav-end-of-statement)
1893 (goto-char (point-max))
1894 (python-util-forward-comment -1)
1897 (ert-deftest python-nav-forward-statement-1 ()
1898 (python-tests-with-temp-buffer
1908 v3 = ('this is a string'
1913 # this is a comment, yo
1914 'continue previous line')
1920 (python-tests-look-at "v1 =")
1921 (should (= (save-excursion
1922 (python-nav-forward-statement)
1924 (python-tests-look-at "v2 =")))
1925 (should (= (save-excursion
1926 (python-nav-forward-statement)
1928 (python-tests-look-at "v3 =")))
1929 (should (= (save-excursion
1930 (python-nav-forward-statement)
1932 (python-tests-look-at "v4 =")))
1933 (should (= (save-excursion
1934 (python-nav-forward-statement)
1938 (ert-deftest python-nav-backward-statement-1 ()
1939 (python-tests-with-temp-buffer
1949 v3 = ('this is a string'
1954 # this is a comment, yo
1955 'continue previous line')
1961 (goto-char (point-max))
1962 (should (= (save-excursion
1963 (python-nav-backward-statement)
1965 (python-tests-look-at "v4 =" -1)))
1966 (should (= (save-excursion
1967 (python-nav-backward-statement)
1969 (python-tests-look-at "v3 =" -1)))
1970 (should (= (save-excursion
1971 (python-nav-backward-statement)
1973 (python-tests-look-at "v2 =" -1)))
1974 (should (= (save-excursion
1975 (python-nav-backward-statement)
1977 (python-tests-look-at "v1 =" -1)))))
1979 (ert-deftest python-nav-backward-statement-2 ()
1980 :expected-result :failed
1981 (python-tests-with-temp-buffer
1992 ;; FIXME: For some reason `python-nav-backward-statement' is moving
1993 ;; back two sentences when starting from 'value4)'.
1994 (goto-char (point-max))
1995 (python-util-forward-comment -1)
1996 (should (= (save-excursion
1997 (python-nav-backward-statement)
1999 (python-tests-look-at "v2 =" -1 t)))))
2001 (ert-deftest python-nav-beginning-of-block-1 ()
2002 (python-tests-with-temp-buffer
2004 def decoratorFunctionWithArguments(arg1, arg2, arg3):
2005 '''print decorated function call data to stdout.
2009 @decoratorFunctionWithArguments('arg1', 'arg2')
2010 def func(a, b, c=True):
2015 print 'Inside wwrap()'
2016 def wrapped_f(*args):
2017 print 'Inside wrapped_f()'
2018 print 'Decorator arguments:', arg1, arg2, arg3
2020 print 'After f(*args)'
2024 (python-tests-look-at "return wwrap")
2025 (should (= (save-excursion
2026 (python-nav-beginning-of-block)
2028 (python-tests-look-at "def decoratorFunctionWithArguments" -1)))
2029 (python-tests-look-at "print 'Inside wwrap()'")
2030 (should (= (save-excursion
2031 (python-nav-beginning-of-block)
2033 (python-tests-look-at "def wwrap(f):" -1)))
2034 (python-tests-look-at "print 'After f(*args)'")
2036 (should (= (save-excursion
2037 (python-nav-beginning-of-block)
2039 (python-tests-look-at "def wrapped_f(*args):" -1)))
2040 (python-tests-look-at "return wrapped_f")
2041 (should (= (save-excursion
2042 (python-nav-beginning-of-block)
2044 (python-tests-look-at "def wwrap(f):" -1)))))
2046 (ert-deftest python-nav-end-of-block-1 ()
2047 (python-tests-with-temp-buffer
2049 def decoratorFunctionWithArguments(arg1, arg2, arg3):
2050 '''print decorated function call data to stdout.
2054 @decoratorFunctionWithArguments('arg1', 'arg2')
2055 def func(a, b, c=True):
2060 print 'Inside wwrap()'
2061 def wrapped_f(*args):
2062 print 'Inside wrapped_f()'
2063 print 'Decorator arguments:', arg1, arg2, arg3
2065 print 'After f(*args)'
2069 (python-tests-look-at "def decoratorFunctionWithArguments")
2070 (should (= (save-excursion
2071 (python-nav-end-of-block)
2074 (goto-char (point-max))
2075 (python-util-forward-comment -1)
2077 (python-tests-look-at "def wwrap(f):")
2078 (should (= (save-excursion
2079 (python-nav-end-of-block)
2082 (python-tests-look-at "return wrapped_f")
2083 (line-end-position))))
2085 (should (= (save-excursion
2086 (python-nav-end-of-block)
2089 (python-tests-look-at "return wrapped_f")
2090 (line-end-position))))
2091 (python-tests-look-at "f(*args)")
2092 (should (= (save-excursion
2093 (python-nav-end-of-block)
2096 (python-tests-look-at "print 'After f(*args)'")
2097 (line-end-position))))))
2099 (ert-deftest python-nav-forward-block-1 ()
2100 "This also accounts as a test for `python-nav-backward-block'."
2101 (python-tests-with-temp-buffer
2103 if request.user.is_authenticated():
2107 profile = request.user.get_profile()
2108 except Profile.DoesNotExist:
2109 profile = Profile.objects.create(user=request.user)
2112 profile.recalculate_stats()
2114 profile.clear_stats()
2119 (should (= (save-excursion (python-nav-forward-block))
2120 (python-tests-look-at "if request.user.is_authenticated():")))
2121 (should (= (save-excursion (python-nav-forward-block))
2122 (python-tests-look-at "try:")))
2123 (should (= (save-excursion (python-nav-forward-block))
2124 (python-tests-look-at "except Profile.DoesNotExist:")))
2125 (should (= (save-excursion (python-nav-forward-block))
2126 (python-tests-look-at "else:")))
2127 (should (= (save-excursion (python-nav-forward-block))
2128 (python-tests-look-at "if profile.stats:")))
2129 (should (= (save-excursion (python-nav-forward-block))
2130 (python-tests-look-at "else:")))
2131 (should (= (save-excursion (python-nav-forward-block))
2132 (python-tests-look-at "finally:")))
2133 ;; When point is at the last block, leave it there and return nil
2134 (should (not (save-excursion (python-nav-forward-block))))
2135 ;; Move backwards, and even if the number of moves is less than the
2136 ;; provided argument return the point.
2137 (should (= (save-excursion (python-nav-forward-block -10))
2138 (python-tests-look-at
2139 "if request.user.is_authenticated():" -1)))))
2141 (ert-deftest python-nav-forward-sexp-1 ()
2142 (python-tests-with-temp-buffer
2148 (python-tests-look-at "a()")
2149 (python-nav-forward-sexp)
2150 (should (looking-at "$"))
2151 (should (save-excursion
2153 (looking-at "a()")))
2154 (python-nav-forward-sexp)
2155 (should (looking-at "$"))
2156 (should (save-excursion
2158 (looking-at "b()")))
2159 (python-nav-forward-sexp)
2160 (should (looking-at "$"))
2161 (should (save-excursion
2163 (looking-at "c()")))
2164 ;; The default behavior when next to a paren should do what lisp
2165 ;; does and, otherwise `blink-matching-open' breaks.
2166 (python-nav-forward-sexp -1)
2167 (should (looking-at "()"))
2168 (should (save-excursion
2170 (looking-at "c()")))
2172 ;; Skipping parens should jump to `bolp'
2173 (python-nav-forward-sexp -1 nil t)
2174 (should (looking-at "c()"))
2178 (python-nav-forward-sexp -1)
2179 (should (looking-at "()"))
2180 (python-nav-forward-sexp -1)
2181 (should (looking-at "b()"))
2183 (python-nav-forward-sexp -1 nil t)
2184 (should (looking-at "b()"))
2188 (python-nav-forward-sexp -1)
2189 (should (looking-at "()"))
2190 (python-nav-forward-sexp -1)
2191 (should (looking-at "a()"))
2193 (python-nav-forward-sexp -1 nil t)
2194 (should (looking-at "a()"))))
2196 (ert-deftest python-nav-forward-sexp-2 ()
2197 (python-tests-with-temp-buffer
2206 (python-tests-look-at "aa =")
2207 (python-nav-forward-sexp)
2208 (should (looking-at " = bbb"))
2209 (python-nav-forward-sexp)
2210 (should (looking-at "$"))
2211 (should (save-excursion
2212 (back-to-indentation)
2213 (looking-at "aaa = bbb")))
2214 (python-nav-forward-sexp)
2215 (should (looking-at "$"))
2216 (should (save-excursion
2217 (back-to-indentation)
2218 (looking-at "ccc = ddd")))
2219 (python-nav-forward-sexp)
2220 (should (looking-at "$"))
2221 (should (save-excursion
2222 (back-to-indentation)
2223 (looking-at "eee = fff")))
2224 (python-nav-forward-sexp)
2225 (should (looking-at "$"))
2226 (should (save-excursion
2227 (back-to-indentation)
2228 (looking-at "return ggg")))
2229 (python-nav-forward-sexp -1)
2230 (should (looking-at "def func():"))))
2232 (ert-deftest python-nav-forward-sexp-3 ()
2233 (python-tests-with-temp-buffer
2235 from some_module import some_sub_module
2236 from another_module import another_sub_module
2238 def another_statement():
2241 (python-tests-look-at "some_module")
2242 (python-nav-forward-sexp)
2243 (should (looking-at " import"))
2244 (python-nav-forward-sexp)
2245 (should (looking-at " some_sub_module"))
2246 (python-nav-forward-sexp)
2247 (should (looking-at "$"))
2250 (back-to-indentation)
2252 "from some_module import some_sub_module")))
2253 (python-nav-forward-sexp)
2254 (should (looking-at "$"))
2257 (back-to-indentation)
2259 "from another_module import another_sub_module")))
2260 (python-nav-forward-sexp)
2261 (should (looking-at "$"))
2264 (back-to-indentation)
2267 (python-nav-forward-sexp -1)
2268 (should (looking-at "def another_statement():"))
2269 (python-nav-forward-sexp -1)
2270 (should (looking-at "from another_module import another_sub_module"))
2271 (python-nav-forward-sexp -1)
2272 (should (looking-at "from some_module import some_sub_module"))))
2274 (ert-deftest python-nav-forward-sexp-safe-1 ()
2275 (python-tests-with-temp-buffer
2277 profile = Profile.objects.create(user=request.user)
2280 (python-tests-look-at "profile =")
2281 (python-nav-forward-sexp-safe 1)
2282 (should (looking-at "$"))
2283 (beginning-of-line 1)
2284 (python-tests-look-at "user=request.user")
2285 (python-nav-forward-sexp-safe -1)
2286 (should (looking-at "(user=request.user)"))
2287 (python-nav-forward-sexp-safe -4)
2288 (should (looking-at "profile ="))
2289 (python-tests-look-at "user=request.user")
2290 (python-nav-forward-sexp-safe 3)
2291 (should (looking-at ")"))
2292 (python-nav-forward-sexp-safe 1)
2293 (should (looking-at "$"))
2294 (python-nav-forward-sexp-safe 1)
2295 (should (looking-at "$"))))
2297 (ert-deftest python-nav-up-list-1 ()
2298 (python-tests-with-temp-buffer
2302 return [i for i in range(3)]
2304 (python-tests-look-at "3)]")
2305 (python-nav-up-list)
2306 (should (looking-at "]"))
2307 (python-nav-up-list)
2308 (should (looking-at "$"))))
2310 (ert-deftest python-nav-backward-up-list-1 ()
2311 :expected-result :failed
2312 (python-tests-with-temp-buffer
2316 return [i for i in range(3)]
2318 (python-tests-look-at "3)]")
2319 (python-nav-backward-up-list)
2320 (should (looking-at "(3)\\]"))
2321 (python-nav-backward-up-list)
2323 "\\[i for i in range(3)\\]"))
2324 ;; FIXME: Need to move to beginning-of-statement.
2325 (python-nav-backward-up-list)
2327 "return \\[i for i in range(3)\\]"))
2328 (python-nav-backward-up-list)
2329 (should (looking-at "if True:"))
2330 (python-nav-backward-up-list)
2331 (should (looking-at "def f():"))))
2333 (ert-deftest python-indent-dedent-line-backspace-1 ()
2334 "Check de-indentation on first call. Bug#18319."
2335 (python-tests-with-temp-buffer
2341 (python-tests-look-at "if False:")
2342 (call-interactively #'python-indent-dedent-line-backspace)
2343 (should (zerop (current-indentation)))
2344 ;; XXX: This should be a call to `undo' but it's triggering errors.
2346 (should (= (current-indentation) 4))
2347 (call-interactively #'python-indent-dedent-line-backspace)
2348 (should (zerop (current-indentation)))))
2350 (ert-deftest python-indent-dedent-line-backspace-2 ()
2351 "Check de-indentation with tabs. Bug#19730."
2352 (let ((tab-width 8))
2353 (python-tests-with-temp-buffer
2358 (python-tests-look-at "abcdefg")
2359 (goto-char (line-end-position))
2360 (call-interactively #'python-indent-dedent-line-backspace)
2362 (string= (buffer-substring-no-properties
2363 (line-beginning-position) (line-end-position))
2366 (ert-deftest python-indent-dedent-line-backspace-3 ()
2367 "Paranoid check of de-indentation with tabs. Bug#19730."
2368 (let ((tab-width 8))
2369 (python-tests-with-temp-buffer
2375 (python-tests-look-at "abcdefg")
2376 (goto-char (line-end-position))
2377 (call-interactively #'python-indent-dedent-line-backspace)
2379 (string= (buffer-substring-no-properties
2380 (line-beginning-position) (line-end-position))
2382 (back-to-indentation)
2383 (call-interactively #'python-indent-dedent-line-backspace)
2385 (string= (buffer-substring-no-properties
2386 (line-beginning-position) (line-end-position))
2388 (call-interactively #'python-indent-dedent-line-backspace)
2390 (string= (buffer-substring-no-properties
2391 (line-beginning-position) (line-end-position))
2393 (call-interactively #'python-indent-dedent-line-backspace)
2395 (string= (buffer-substring-no-properties
2396 (line-beginning-position) (line-end-position))
2400 ;;; Shell integration
2402 (defvar python-tests-shell-interpreter "python")
2404 (ert-deftest python-shell-get-process-name-1 ()
2405 "Check process name calculation sans `buffer-file-name'."
2406 (python-tests-with-temp-buffer
2408 (should (string= (python-shell-get-process-name nil)
2409 python-shell-buffer-name))
2410 (should (string= (python-shell-get-process-name t)
2411 (format "%s[%s]" python-shell-buffer-name (buffer-name))))))
2413 (ert-deftest python-shell-get-process-name-2 ()
2414 "Check process name calculation with `buffer-file-name'."
2415 (python-tests-with-temp-file
2417 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
2418 ;; should be respected.
2419 (should (string= (python-shell-get-process-name nil)
2420 python-shell-buffer-name))
2422 (python-shell-get-process-name t)
2423 (format "%s[%s]" python-shell-buffer-name (buffer-name))))))
2425 (ert-deftest python-shell-internal-get-process-name-1 ()
2426 "Check the internal process name is buffer-unique sans `buffer-file-name'."
2427 (python-tests-with-temp-buffer
2429 (should (string= (python-shell-internal-get-process-name)
2430 (format "%s[%s]" python-shell-internal-buffer-name (buffer-name))))))
2432 (ert-deftest python-shell-internal-get-process-name-2 ()
2433 "Check the internal process name is buffer-unique with `buffer-file-name'."
2434 (python-tests-with-temp-file
2436 (should (string= (python-shell-internal-get-process-name)
2437 (format "%s[%s]" python-shell-internal-buffer-name (buffer-name))))))
2439 (ert-deftest python-shell-calculate-command-1 ()
2440 "Check the command to execute is calculated correctly.
2441 Using `python-shell-interpreter' and
2442 `python-shell-interpreter-args'."
2443 (skip-unless (executable-find python-tests-shell-interpreter))
2444 (let ((python-shell-interpreter (executable-find
2445 python-tests-shell-interpreter))
2446 (python-shell-interpreter-args "-B"))
2449 (shell-quote-argument python-shell-interpreter)
2450 python-shell-interpreter-args)
2451 (python-shell-calculate-command)))))
2453 (ert-deftest python-shell-calculate-pythonpath-1 ()
2454 "Test PYTHONPATH calculation."
2455 (let ((process-environment '("PYTHONPATH=/path0"))
2456 (python-shell-extra-pythonpaths '("/path1" "/path2")))
2457 (should (string= (python-shell-calculate-pythonpath)
2458 (concat "/path1" path-separator
2459 "/path2" path-separator "/path0")))))
2461 (ert-deftest python-shell-calculate-pythonpath-2 ()
2462 "Test existing paths are moved to front."
2463 (let ((process-environment
2464 (list (concat "PYTHONPATH=/path0" path-separator "/path1")))
2465 (python-shell-extra-pythonpaths '("/path1" "/path2")))
2466 (should (string= (python-shell-calculate-pythonpath)
2467 (concat "/path1" path-separator
2468 "/path2" path-separator "/path0")))))
2470 (ert-deftest python-shell-calculate-process-environment-1 ()
2471 "Test `python-shell-process-environment' modification."
2472 (let* ((python-shell-process-environment
2473 '("TESTVAR1=value1" "TESTVAR2=value2"))
2474 (process-environment (python-shell-calculate-process-environment)))
2475 (should (equal (getenv "TESTVAR1") "value1"))
2476 (should (equal (getenv "TESTVAR2") "value2"))))
2478 (ert-deftest python-shell-calculate-process-environment-2 ()
2479 "Test `python-shell-extra-pythonpaths' modification."
2480 (let* ((process-environment process-environment)
2481 (original-pythonpath (setenv "PYTHONPATH" "/path0"))
2482 (python-shell-extra-pythonpaths '("/path1" "/path2"))
2483 (process-environment (python-shell-calculate-process-environment)))
2484 (should (equal (getenv "PYTHONPATH")
2485 (concat "/path1" path-separator
2486 "/path2" path-separator "/path0")))))
2488 (ert-deftest python-shell-calculate-process-environment-3 ()
2489 "Test `python-shell-virtualenv-root' modification."
2490 (let* ((python-shell-virtualenv-root "/env")
2491 (process-environment
2492 (let (process-environment process-environment)
2493 (setenv "PYTHONHOME" "/home")
2494 (setenv "VIRTUAL_ENV")
2495 (python-shell-calculate-process-environment))))
2496 (should (not (getenv "PYTHONHOME")))
2497 (should (string= (getenv "VIRTUAL_ENV") "/env"))))
2499 (ert-deftest python-shell-calculate-process-environment-4 ()
2500 "Test PYTHONUNBUFFERED when `python-shell-unbuffered' is non-nil."
2501 (let* ((python-shell-unbuffered t)
2502 (process-environment
2503 (let ((process-environment process-environment))
2504 (setenv "PYTHONUNBUFFERED")
2505 (python-shell-calculate-process-environment))))
2506 (should (string= (getenv "PYTHONUNBUFFERED") "1"))))
2508 (ert-deftest python-shell-calculate-process-environment-5 ()
2509 "Test PYTHONUNBUFFERED when `python-shell-unbuffered' is nil."
2510 (let* ((python-shell-unbuffered nil)
2511 (process-environment
2512 (let ((process-environment process-environment))
2513 (setenv "PYTHONUNBUFFERED")
2514 (python-shell-calculate-process-environment))))
2515 (should (not (getenv "PYTHONUNBUFFERED")))))
2517 (ert-deftest python-shell-calculate-process-environment-6 ()
2518 "Test PYTHONUNBUFFERED=1 when `python-shell-unbuffered' is nil."
2519 (let* ((python-shell-unbuffered nil)
2520 (process-environment
2521 (let ((process-environment process-environment))
2522 (setenv "PYTHONUNBUFFERED" "1")
2523 (python-shell-calculate-process-environment))))
2524 ;; User default settings must remain untouched:
2525 (should (string= (getenv "PYTHONUNBUFFERED") "1"))))
2527 (ert-deftest python-shell-calculate-process-environment-7 ()
2528 "Test no side-effects on `process-environment'."
2529 (let* ((python-shell-process-environment
2530 '("TESTVAR1=value1" "TESTVAR2=value2"))
2531 (python-shell-virtualenv-root "/env")
2532 (python-shell-unbuffered t)
2533 (python-shell-extra-pythonpaths'("/path1" "/path2"))
2534 (original-process-environment (copy-sequence process-environment)))
2535 (python-shell-calculate-process-environment)
2536 (should (equal process-environment original-process-environment))))
2538 (ert-deftest python-shell-calculate-process-environment-8 ()
2539 "Test no side-effects on `tramp-remote-process-environment'."
2540 (let* ((default-directory "/ssh::/example/dir/")
2541 (python-shell-process-environment
2542 '("TESTVAR1=value1" "TESTVAR2=value2"))
2543 (python-shell-virtualenv-root "/env")
2544 (python-shell-unbuffered t)
2545 (python-shell-extra-pythonpaths'("/path1" "/path2"))
2546 (original-process-environment
2547 (copy-sequence tramp-remote-process-environment)))
2548 (python-shell-calculate-process-environment)
2549 (should (equal tramp-remote-process-environment original-process-environment))))
2551 (ert-deftest python-shell-calculate-exec-path-1 ()
2552 "Test `python-shell-exec-path' modification."
2553 (let* ((exec-path '("/path0"))
2554 (python-shell-exec-path '("/path1" "/path2"))
2555 (new-exec-path (python-shell-calculate-exec-path)))
2556 (should (equal new-exec-path '("/path1" "/path2" "/path0")))))
2558 (ert-deftest python-shell-calculate-exec-path-2 ()
2559 "Test `python-shell-virtualenv-root' modification."
2560 (let* ((exec-path '("/path0"))
2561 (python-shell-virtualenv-root "/env")
2562 (new-exec-path (python-shell-calculate-exec-path)))
2563 (should (equal new-exec-path
2564 (list (expand-file-name "/env/bin") "/path0")))))
2566 (ert-deftest python-shell-calculate-exec-path-3 ()
2567 "Test complete `python-shell-virtualenv-root' modification."
2568 (let* ((exec-path '("/path0"))
2569 (python-shell-exec-path '("/path1" "/path2"))
2570 (python-shell-virtualenv-root "/env")
2571 (new-exec-path (python-shell-calculate-exec-path)))
2572 (should (equal new-exec-path
2573 (list (expand-file-name "/env/bin")
2574 "/path1" "/path2" "/path0")))))
2576 (ert-deftest python-shell-calculate-exec-path-4 ()
2577 "Test complete `python-shell-virtualenv-root' with remote."
2578 (let* ((default-directory "/ssh::/example/dir/")
2579 (python-shell-remote-exec-path '("/path0"))
2580 (python-shell-exec-path '("/path1" "/path2"))
2581 (python-shell-virtualenv-root "/env")
2582 (new-exec-path (python-shell-calculate-exec-path)))
2583 (should (equal new-exec-path
2584 (list (expand-file-name "/env/bin")
2585 "/path1" "/path2" "/path0")))))
2587 (ert-deftest python-shell-calculate-exec-path-5 ()
2588 "Test no side-effects on `exec-path'."
2589 (let* ((exec-path '("/path0"))
2590 (python-shell-exec-path '("/path1" "/path2"))
2591 (python-shell-virtualenv-root "/env")
2592 (original-exec-path (copy-sequence exec-path)))
2593 (python-shell-calculate-exec-path)
2594 (should (equal exec-path original-exec-path))))
2596 (ert-deftest python-shell-calculate-exec-path-6 ()
2597 "Test no side-effects on `python-shell-remote-exec-path'."
2598 (let* ((default-directory "/ssh::/example/dir/")
2599 (python-shell-remote-exec-path '("/path0"))
2600 (python-shell-exec-path '("/path1" "/path2"))
2601 (python-shell-virtualenv-root "/env")
2602 (original-exec-path (copy-sequence python-shell-remote-exec-path)))
2603 (python-shell-calculate-exec-path)
2604 (should (equal python-shell-remote-exec-path original-exec-path))))
2606 (ert-deftest python-shell-with-environment-1 ()
2607 "Test environment with local `default-directory'."
2608 (let* ((exec-path '("/path0"))
2609 (python-shell-exec-path '("/path1" "/path2"))
2610 (original-exec-path exec-path)
2611 (python-shell-virtualenv-root "/env"))
2612 (python-shell-with-environment
2613 (should (equal exec-path
2614 (list (expand-file-name "/env/bin")
2615 "/path1" "/path2" "/path0")))
2616 (should (not (getenv "PYTHONHOME")))
2617 (should (string= (getenv "VIRTUAL_ENV") "/env")))
2618 (should (equal exec-path original-exec-path))))
2620 (ert-deftest python-shell-with-environment-2 ()
2621 "Test environment with remote `default-directory'."
2622 (let* ((default-directory "/ssh::/example/dir/")
2623 (python-shell-remote-exec-path '("/remote1" "/remote2"))
2624 (python-shell-exec-path '("/path1" "/path2"))
2625 (tramp-remote-process-environment '("EMACS=t"))
2626 (original-process-environment (copy-sequence tramp-remote-process-environment))
2627 (python-shell-virtualenv-root "/env"))
2628 (python-shell-with-environment
2629 (should (equal (python-shell-calculate-exec-path)
2630 (list (expand-file-name "/env/bin")
2631 "/path1" "/path2" "/remote1" "/remote2")))
2632 (let ((process-environment (python-shell-calculate-process-environment)))
2633 (should (not (getenv "PYTHONHOME")))
2634 (should (string= (getenv "VIRTUAL_ENV") "/env"))
2635 (should (equal tramp-remote-process-environment process-environment))))
2636 (should (equal tramp-remote-process-environment original-process-environment))))
2638 (ert-deftest python-shell-with-environment-3 ()
2639 "Test `python-shell-with-environment' is idempotent."
2640 (let* ((python-shell-extra-pythonpaths '("/example/dir/"))
2641 (python-shell-exec-path '("path1" "path2"))
2642 (python-shell-virtualenv-root "/home/user/env")
2644 (python-shell-with-environment
2645 (list exec-path process-environment)))
2647 (python-shell-with-environment
2648 (python-shell-with-environment
2649 (list exec-path process-environment)))))
2650 (should (equal single-call nested-call))))
2652 (ert-deftest python-shell-make-comint-1 ()
2653 "Check comint creation for global shell buffer."
2654 (skip-unless (executable-find python-tests-shell-interpreter))
2655 ;; The interpreter can get killed too quickly to allow it to clean
2656 ;; up the tempfiles that the default python-shell-setup-codes create,
2657 ;; so it leaves tempfiles behind, which is a minor irritation.
2658 (let* ((python-shell-setup-codes nil)
2659 (python-shell-interpreter
2660 (executable-find python-tests-shell-interpreter))
2661 (proc-name (python-shell-get-process-name nil))
2663 (python-tests-with-temp-buffer
2664 "" (python-shell-make-comint
2665 (python-shell-calculate-command) proc-name)))
2666 (process (get-buffer-process shell-buffer)))
2669 (set-process-query-on-exit-flag process nil)
2670 (should (process-live-p process))
2671 (with-current-buffer shell-buffer
2672 (should (eq major-mode 'inferior-python-mode))
2673 (should (string= (buffer-name) (format "*%s*" proc-name)))))
2674 (kill-buffer shell-buffer))))
2676 (ert-deftest python-shell-make-comint-2 ()
2677 "Check comint creation for internal shell buffer."
2678 (skip-unless (executable-find python-tests-shell-interpreter))
2679 (let* ((python-shell-setup-codes nil)
2680 (python-shell-interpreter
2681 (executable-find python-tests-shell-interpreter))
2682 (proc-name (python-shell-internal-get-process-name))
2684 (python-tests-with-temp-buffer
2685 "" (python-shell-make-comint
2686 (python-shell-calculate-command) proc-name nil t)))
2687 (process (get-buffer-process shell-buffer)))
2690 (set-process-query-on-exit-flag process nil)
2691 (should (process-live-p process))
2692 (with-current-buffer shell-buffer
2693 (should (eq major-mode 'inferior-python-mode))
2694 (should (string= (buffer-name) (format " *%s*" proc-name)))))
2695 (kill-buffer shell-buffer))))
2697 (ert-deftest python-shell-make-comint-3 ()
2698 "Check comint creation with overridden python interpreter and args.
2699 The command passed to `python-shell-make-comint' as argument must
2700 locally override global values set in `python-shell-interpreter'
2701 and `python-shell-interpreter-args' in the new shell buffer."
2702 (skip-unless (executable-find python-tests-shell-interpreter))
2703 (let* ((python-shell-setup-codes nil)
2704 (python-shell-interpreter "interpreter")
2705 (python-shell-interpreter-args "--some-args")
2706 (proc-name (python-shell-get-process-name nil))
2707 (interpreter-override
2708 (concat (executable-find python-tests-shell-interpreter) " " "-i"))
2710 (python-tests-with-temp-buffer
2711 "" (python-shell-make-comint interpreter-override proc-name nil)))
2712 (process (get-buffer-process shell-buffer)))
2715 (set-process-query-on-exit-flag process nil)
2716 (should (process-live-p process))
2717 (with-current-buffer shell-buffer
2718 (should (eq major-mode 'inferior-python-mode))
2719 (should (file-equal-p
2720 python-shell-interpreter
2721 (executable-find python-tests-shell-interpreter)))
2722 (should (string= python-shell-interpreter-args "-i"))))
2723 (kill-buffer shell-buffer))))
2725 (ert-deftest python-shell-make-comint-4 ()
2726 "Check shell calculated prompts regexps are set."
2727 (skip-unless (executable-find python-tests-shell-interpreter))
2728 (let* ((process-environment process-environment)
2729 (python-shell-setup-codes nil)
2730 (python-shell-interpreter
2731 (executable-find python-tests-shell-interpreter))
2732 (python-shell-interpreter-args "-i")
2733 (python-shell--prompt-calculated-input-regexp nil)
2734 (python-shell--prompt-calculated-output-regexp nil)
2735 (python-shell-prompt-detect-enabled t)
2736 (python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2737 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2738 (python-shell-prompt-regexp "in")
2739 (python-shell-prompt-block-regexp "block")
2740 (python-shell-prompt-pdb-regexp "pdf")
2741 (python-shell-prompt-output-regexp "output")
2742 (startup-code (concat "import sys\n"
2743 "sys.ps1 = 'py> '\n"
2744 "sys.ps2 = '..> '\n"
2745 "sys.ps3 = 'out '\n"))
2746 (startup-file (python-shell--save-temp-file startup-code))
2747 (proc-name (python-shell-get-process-name nil))
2750 (setenv "PYTHONSTARTUP" startup-file)
2751 (python-tests-with-temp-buffer
2752 "" (python-shell-make-comint
2753 (python-shell-calculate-command) proc-name nil))))
2754 (process (get-buffer-process shell-buffer)))
2757 (set-process-query-on-exit-flag process nil)
2758 (should (process-live-p process))
2759 (with-current-buffer shell-buffer
2760 (should (eq major-mode 'inferior-python-mode))
2762 python-shell--prompt-calculated-input-regexp
2763 (concat "^\\(extralargeinputprompt\\|\\.\\.> \\|"
2764 "block\\|py> \\|pdf\\|sml\\|in\\)")))
2766 python-shell--prompt-calculated-output-regexp
2767 "^\\(extralargeoutputprompt\\|output\\|out \\|sml\\)"))))
2768 (delete-file startup-file)
2769 (kill-buffer shell-buffer))))
2771 (ert-deftest python-shell-get-process-1 ()
2772 "Check dedicated shell process preference over global."
2773 (skip-unless (executable-find python-tests-shell-interpreter))
2774 (python-tests-with-temp-file
2776 (let* ((python-shell-setup-codes nil)
2777 (python-shell-interpreter
2778 (executable-find python-tests-shell-interpreter))
2779 (global-proc-name (python-shell-get-process-name nil))
2780 (dedicated-proc-name (python-shell-get-process-name t))
2781 (global-shell-buffer
2782 (python-shell-make-comint
2783 (python-shell-calculate-command) global-proc-name))
2784 (dedicated-shell-buffer
2785 (python-shell-make-comint
2786 (python-shell-calculate-command) dedicated-proc-name))
2787 (global-process (get-buffer-process global-shell-buffer))
2788 (dedicated-process (get-buffer-process dedicated-shell-buffer)))
2791 (set-process-query-on-exit-flag global-process nil)
2792 (set-process-query-on-exit-flag dedicated-process nil)
2793 ;; Prefer dedicated if global also exists.
2794 (should (equal (python-shell-get-process) dedicated-process))
2795 (kill-buffer dedicated-shell-buffer)
2796 ;; If there's only global, use it.
2797 (should (equal (python-shell-get-process) global-process))
2798 (kill-buffer global-shell-buffer)
2799 ;; No buffer available.
2800 (should (not (python-shell-get-process))))
2801 (ignore-errors (kill-buffer global-shell-buffer))
2802 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
2804 (ert-deftest python-shell-internal-get-or-create-process-1 ()
2805 "Check internal shell process creation fallback."
2806 (skip-unless (executable-find python-tests-shell-interpreter))
2807 (python-tests-with-temp-file
2809 (should (not (process-live-p (python-shell-internal-get-process-name))))
2810 (let* ((python-shell-interpreter
2811 (executable-find python-tests-shell-interpreter))
2812 (internal-process-name (python-shell-internal-get-process-name))
2813 (internal-process (python-shell-internal-get-or-create-process))
2814 (internal-shell-buffer (process-buffer internal-process)))
2817 (set-process-query-on-exit-flag internal-process nil)
2818 (should (equal (process-name internal-process)
2819 internal-process-name))
2820 (should (equal internal-process
2821 (python-shell-internal-get-or-create-process)))
2822 ;; Assert the internal process is not a user process
2823 (should (not (python-shell-get-process)))
2824 (kill-buffer internal-shell-buffer))
2825 (ignore-errors (kill-buffer internal-shell-buffer))))))
2827 (ert-deftest python-shell-prompt-detect-1 ()
2828 "Check prompt autodetection."
2829 (skip-unless (executable-find python-tests-shell-interpreter))
2830 (let ((process-environment process-environment))
2831 ;; Ensure no startup file is enabled
2832 (setenv "PYTHONSTARTUP" "")
2833 (should python-shell-prompt-detect-enabled)
2834 (should (equal (python-shell-prompt-detect) '(">>> " "... " "")))))
2836 (ert-deftest python-shell-prompt-detect-2 ()
2837 "Check prompt autodetection with startup file. Bug#17370."
2838 (skip-unless (executable-find python-tests-shell-interpreter))
2839 (let* ((process-environment process-environment)
2840 (startup-code (concat "import sys\n"
2841 "sys.ps1 = 'py> '\n"
2842 "sys.ps2 = '..> '\n"
2843 "sys.ps3 = 'out '\n"))
2844 (startup-file (python-shell--save-temp-file startup-code)))
2847 ;; Ensure startup file is enabled
2848 (setenv "PYTHONSTARTUP" startup-file)
2849 (should python-shell-prompt-detect-enabled)
2850 (should (equal (python-shell-prompt-detect) '("py> " "..> " "out "))))
2851 (ignore-errors (delete-file startup-file)))))
2853 (ert-deftest python-shell-prompt-detect-3 ()
2854 "Check prompts are not autodetected when feature is disabled."
2855 (skip-unless (executable-find python-tests-shell-interpreter))
2856 (let ((process-environment process-environment)
2857 (python-shell-prompt-detect-enabled nil))
2858 ;; Ensure no startup file is enabled
2859 (should (not python-shell-prompt-detect-enabled))
2860 (should (not (python-shell-prompt-detect)))))
2862 (ert-deftest python-shell-prompt-detect-4 ()
2863 "Check warning is shown when detection fails."
2864 (skip-unless (executable-find python-tests-shell-interpreter))
2865 (let* ((process-environment process-environment)
2866 ;; Trigger failure by removing prompts in the startup file
2867 (startup-code (concat "import sys\n"
2871 (startup-file (python-shell--save-temp-file startup-code)))
2874 (kill-buffer (get-buffer-create "*Warnings*"))
2875 (should (not (get-buffer "*Warnings*")))
2876 (setenv "PYTHONSTARTUP" startup-file)
2877 (should python-shell-prompt-detect-failure-warning)
2878 (should python-shell-prompt-detect-enabled)
2879 (should (not (python-shell-prompt-detect)))
2880 (should (get-buffer "*Warnings*")))
2881 (ignore-errors (delete-file startup-file)))))
2883 (ert-deftest python-shell-prompt-detect-5 ()
2884 "Check disabled warnings are not shown when detection fails."
2885 (skip-unless (executable-find python-tests-shell-interpreter))
2886 (let* ((process-environment process-environment)
2887 (startup-code (concat "import sys\n"
2891 (startup-file (python-shell--save-temp-file startup-code))
2892 (python-shell-prompt-detect-failure-warning nil))
2895 (kill-buffer (get-buffer-create "*Warnings*"))
2896 (should (not (get-buffer "*Warnings*")))
2897 (setenv "PYTHONSTARTUP" startup-file)
2898 (should (not python-shell-prompt-detect-failure-warning))
2899 (should python-shell-prompt-detect-enabled)
2900 (should (not (python-shell-prompt-detect)))
2901 (should (not (get-buffer "*Warnings*"))))
2902 (ignore-errors (delete-file startup-file)))))
2904 (ert-deftest python-shell-prompt-detect-6 ()
2905 "Warnings are not shown when detection is disabled."
2906 (skip-unless (executable-find python-tests-shell-interpreter))
2907 (let* ((process-environment process-environment)
2908 (startup-code (concat "import sys\n"
2912 (startup-file (python-shell--save-temp-file startup-code))
2913 (python-shell-prompt-detect-failure-warning t)
2914 (python-shell-prompt-detect-enabled nil))
2917 (kill-buffer (get-buffer-create "*Warnings*"))
2918 (should (not (get-buffer "*Warnings*")))
2919 (setenv "PYTHONSTARTUP" startup-file)
2920 (should python-shell-prompt-detect-failure-warning)
2921 (should (not python-shell-prompt-detect-enabled))
2922 (should (not (python-shell-prompt-detect)))
2923 (should (not (get-buffer "*Warnings*"))))
2924 (ignore-errors (delete-file startup-file)))))
2926 (ert-deftest python-shell-prompt-validate-regexps-1 ()
2927 "Check `python-shell-prompt-input-regexps' are validated."
2928 (let* ((python-shell-prompt-input-regexps '("\\("))
2929 (error-data (should-error (python-shell-prompt-validate-regexps)
2930 :type 'user-error)))
2932 (string= (cadr error-data)
2934 "Invalid regexp \\( in `python-shell-prompt-input-regexps'")))))
2936 (ert-deftest python-shell-prompt-validate-regexps-2 ()
2937 "Check `python-shell-prompt-output-regexps' are validated."
2938 (let* ((python-shell-prompt-output-regexps '("\\("))
2939 (error-data (should-error (python-shell-prompt-validate-regexps)
2940 :type 'user-error)))
2942 (string= (cadr error-data)
2944 "Invalid regexp \\( in `python-shell-prompt-output-regexps'")))))
2946 (ert-deftest python-shell-prompt-validate-regexps-3 ()
2947 "Check `python-shell-prompt-regexp' is validated."
2948 (let* ((python-shell-prompt-regexp "\\(")
2949 (error-data (should-error (python-shell-prompt-validate-regexps)
2950 :type 'user-error)))
2952 (string= (cadr error-data)
2954 "Invalid regexp \\( in `python-shell-prompt-regexp'")))))
2956 (ert-deftest python-shell-prompt-validate-regexps-4 ()
2957 "Check `python-shell-prompt-block-regexp' is validated."
2958 (let* ((python-shell-prompt-block-regexp "\\(")
2959 (error-data (should-error (python-shell-prompt-validate-regexps)
2960 :type 'user-error)))
2962 (string= (cadr error-data)
2964 "Invalid regexp \\( in `python-shell-prompt-block-regexp'")))))
2966 (ert-deftest python-shell-prompt-validate-regexps-5 ()
2967 "Check `python-shell-prompt-pdb-regexp' is validated."
2968 (let* ((python-shell-prompt-pdb-regexp "\\(")
2969 (error-data (should-error (python-shell-prompt-validate-regexps)
2970 :type 'user-error)))
2972 (string= (cadr error-data)
2974 "Invalid regexp \\( in `python-shell-prompt-pdb-regexp'")))))
2976 (ert-deftest python-shell-prompt-validate-regexps-6 ()
2977 "Check `python-shell-prompt-output-regexp' is validated."
2978 (let* ((python-shell-prompt-output-regexp "\\(")
2979 (error-data (should-error (python-shell-prompt-validate-regexps)
2980 :type 'user-error)))
2982 (string= (cadr error-data)
2984 "Invalid regexp \\( in `python-shell-prompt-output-regexp'")))))
2986 (ert-deftest python-shell-prompt-validate-regexps-7 ()
2987 "Check default regexps are valid."
2988 ;; should not signal error
2989 (python-shell-prompt-validate-regexps))
2991 (ert-deftest python-shell-prompt-set-calculated-regexps-1 ()
2992 "Check regexps are validated."
2993 (let* ((python-shell-prompt-output-regexp '("\\("))
2994 (python-shell--prompt-calculated-input-regexp nil)
2995 (python-shell--prompt-calculated-output-regexp nil)
2996 (python-shell-prompt-detect-enabled nil)
2997 (error-data (should-error (python-shell-prompt-set-calculated-regexps)
2998 :type 'user-error)))
3000 (string= (cadr error-data)
3002 "Invalid regexp \\( in `python-shell-prompt-output-regexp'")))))
3004 (ert-deftest python-shell-prompt-set-calculated-regexps-2 ()
3005 "Check `python-shell-prompt-input-regexps' are set."
3006 (let* ((python-shell-prompt-input-regexps '("my" "prompt"))
3007 (python-shell-prompt-output-regexps '(""))
3008 (python-shell-prompt-regexp "")
3009 (python-shell-prompt-block-regexp "")
3010 (python-shell-prompt-pdb-regexp "")
3011 (python-shell-prompt-output-regexp "")
3012 (python-shell--prompt-calculated-input-regexp nil)
3013 (python-shell--prompt-calculated-output-regexp nil)
3014 (python-shell-prompt-detect-enabled nil))
3015 (python-shell-prompt-set-calculated-regexps)
3016 (should (string= python-shell--prompt-calculated-input-regexp
3017 "^\\(prompt\\|my\\|\\)"))))
3019 (ert-deftest python-shell-prompt-set-calculated-regexps-3 ()
3020 "Check `python-shell-prompt-output-regexps' are set."
3021 (let* ((python-shell-prompt-input-regexps '(""))
3022 (python-shell-prompt-output-regexps '("my" "prompt"))
3023 (python-shell-prompt-regexp "")
3024 (python-shell-prompt-block-regexp "")
3025 (python-shell-prompt-pdb-regexp "")
3026 (python-shell-prompt-output-regexp "")
3027 (python-shell--prompt-calculated-input-regexp nil)
3028 (python-shell--prompt-calculated-output-regexp nil)
3029 (python-shell-prompt-detect-enabled nil))
3030 (python-shell-prompt-set-calculated-regexps)
3031 (should (string= python-shell--prompt-calculated-output-regexp
3032 "^\\(prompt\\|my\\|\\)"))))
3034 (ert-deftest python-shell-prompt-set-calculated-regexps-4 ()
3035 "Check user defined prompts are set."
3036 (let* ((python-shell-prompt-input-regexps '(""))
3037 (python-shell-prompt-output-regexps '(""))
3038 (python-shell-prompt-regexp "prompt")
3039 (python-shell-prompt-block-regexp "block")
3040 (python-shell-prompt-pdb-regexp "pdb")
3041 (python-shell-prompt-output-regexp "output")
3042 (python-shell--prompt-calculated-input-regexp nil)
3043 (python-shell--prompt-calculated-output-regexp nil)
3044 (python-shell-prompt-detect-enabled nil))
3045 (python-shell-prompt-set-calculated-regexps)
3046 (should (string= python-shell--prompt-calculated-input-regexp
3047 "^\\(prompt\\|block\\|pdb\\|\\)"))
3048 (should (string= python-shell--prompt-calculated-output-regexp
3049 "^\\(output\\|\\)"))))
3051 (ert-deftest python-shell-prompt-set-calculated-regexps-5 ()
3052 "Check order of regexps (larger first)."
3053 (let* ((python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
3054 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
3055 (python-shell-prompt-regexp "in")
3056 (python-shell-prompt-block-regexp "block")
3057 (python-shell-prompt-pdb-regexp "pdf")
3058 (python-shell-prompt-output-regexp "output")
3059 (python-shell--prompt-calculated-input-regexp nil)
3060 (python-shell--prompt-calculated-output-regexp nil)
3061 (python-shell-prompt-detect-enabled nil))
3062 (python-shell-prompt-set-calculated-regexps)
3063 (should (string= python-shell--prompt-calculated-input-regexp
3064 "^\\(extralargeinputprompt\\|block\\|pdf\\|sml\\|in\\)"))
3065 (should (string= python-shell--prompt-calculated-output-regexp
3066 "^\\(extralargeoutputprompt\\|output\\|sml\\)"))))
3068 (ert-deftest python-shell-prompt-set-calculated-regexps-6 ()
3069 "Check detected prompts are included `regexp-quote'd."
3070 (skip-unless (executable-find python-tests-shell-interpreter))
3071 (let* ((python-shell-prompt-input-regexps '(""))
3072 (python-shell-prompt-output-regexps '(""))
3073 (python-shell-prompt-regexp "")
3074 (python-shell-prompt-block-regexp "")
3075 (python-shell-prompt-pdb-regexp "")
3076 (python-shell-prompt-output-regexp "")
3077 (python-shell--prompt-calculated-input-regexp nil)
3078 (python-shell--prompt-calculated-output-regexp nil)
3079 (python-shell-prompt-detect-enabled t)
3080 (process-environment process-environment)
3081 (startup-code (concat "import sys\n"
3082 "sys.ps1 = 'p.> '\n"
3083 "sys.ps2 = '..> '\n"
3084 "sys.ps3 = 'o.t '\n"))
3085 (startup-file (python-shell--save-temp-file startup-code)))
3088 (setenv "PYTHONSTARTUP" startup-file)
3089 (python-shell-prompt-set-calculated-regexps)
3090 (should (string= python-shell--prompt-calculated-input-regexp
3091 "^\\(\\.\\.> \\|p\\.> \\|\\)"))
3092 (should (string= python-shell--prompt-calculated-output-regexp
3093 "^\\(o\\.t \\|\\)")))
3094 (ignore-errors (delete-file startup-file)))))
3096 (ert-deftest python-shell-buffer-substring-1 ()
3097 "Selecting a substring of the whole buffer must match its contents."
3098 (python-tests-with-temp-buffer
3100 class Foo(models.Model):
3104 class Bar(models.Model):
3107 (should (string= (buffer-string)
3108 (python-shell-buffer-substring (point-min) (point-max))))))
3110 (ert-deftest python-shell-buffer-substring-2 ()
3111 "Main block should be removed if NOMAIN is non-nil."
3112 (python-tests-with-temp-buffer
3114 class Foo(models.Model):
3117 class Bar(models.Model):
3120 if __name__ == \"__main__\":
3124 (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
3126 class Foo(models.Model):
3129 class Bar(models.Model):
3137 (ert-deftest python-shell-buffer-substring-3 ()
3138 "Main block should be removed if NOMAIN is non-nil."
3139 (python-tests-with-temp-buffer
3141 class Foo(models.Model):
3144 if __name__ == \"__main__\":
3148 class Bar(models.Model):
3151 (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
3153 class Foo(models.Model):
3160 class Bar(models.Model):
3164 (ert-deftest python-shell-buffer-substring-4 ()
3165 "Coding cookie should be added for substrings."
3166 (python-tests-with-temp-buffer
3169 class Foo(models.Model):
3172 if __name__ == \"__main__\":
3176 class Bar(models.Model):
3179 (should (string= (python-shell-buffer-substring
3180 (python-tests-look-at "class Foo(models.Model):")
3181 (progn (python-nav-forward-sexp) (point)))
3182 "# -*- coding: latin-1 -*-
3184 class Foo(models.Model):
3187 (ert-deftest python-shell-buffer-substring-5 ()
3188 "The proper amount of blank lines is added for a substring."
3189 (python-tests-with-temp-buffer
3192 class Foo(models.Model):
3195 if __name__ == \"__main__\":
3199 class Bar(models.Model):
3202 (should (string= (python-shell-buffer-substring
3203 (python-tests-look-at "class Bar(models.Model):")
3204 (progn (python-nav-forward-sexp) (point)))
3205 "# -*- coding: latin-1 -*-
3214 class Bar(models.Model):
3217 (ert-deftest python-shell-buffer-substring-6 ()
3218 "Handle substring with coding cookie in the second line."
3219 (python-tests-with-temp-buffer
3223 class Foo(models.Model):
3226 if __name__ == \"__main__\":
3230 class Bar(models.Model):
3233 (should (string= (python-shell-buffer-substring
3234 (python-tests-look-at "# coding: latin-1")
3235 (python-tests-look-at "if __name__ == \"__main__\":"))
3236 "# -*- coding: latin-1 -*-
3239 class Foo(models.Model):
3244 (ert-deftest python-shell-buffer-substring-7 ()
3245 "Ensure first coding cookie gets precedence."
3246 (python-tests-with-temp-buffer
3250 class Foo(models.Model):
3253 if __name__ == \"__main__\":
3257 class Bar(models.Model):
3260 (should (string= (python-shell-buffer-substring
3261 (python-tests-look-at "# coding: latin-1")
3262 (python-tests-look-at "if __name__ == \"__main__\":"))
3263 "# -*- coding: utf-8 -*-
3266 class Foo(models.Model):
3271 (ert-deftest python-shell-buffer-substring-8 ()
3272 "Ensure first coding cookie gets precedence when sending whole buffer."
3273 (python-tests-with-temp-buffer
3277 class Foo(models.Model):
3280 (should (string= (python-shell-buffer-substring (point-min) (point-max))
3284 class Foo(models.Model):
3288 (ert-deftest python-shell-buffer-substring-9 ()
3289 "Check substring starting from `point-min'."
3290 (python-tests-with-temp-buffer
3293 class Foo(models.Model):
3296 class Bar(models.Model):
3299 (should (string= (python-shell-buffer-substring
3301 (python-tests-look-at "class Bar(models.Model):"))
3304 class Foo(models.Model):
3309 (ert-deftest python-shell-buffer-substring-10 ()
3310 "Check substring from partial block."
3311 (python-tests-with-temp-buffer
3316 (should (string= (python-shell-buffer-substring
3317 (python-tests-look-at "print ('a')")
3324 (ert-deftest python-shell-buffer-substring-11 ()
3325 "Check substring from partial block and point within indentation."
3326 (python-tests-with-temp-buffer
3331 (should (string= (python-shell-buffer-substring
3333 (python-tests-look-at "print ('a')")
3342 (ert-deftest python-shell-buffer-substring-12 ()
3343 "Check substring from partial block and point in whitespace."
3344 (python-tests-with-temp-buffer
3352 (should (string= (python-shell-buffer-substring
3353 (python-tests-look-at "# Whitespace")
3365 ;;; Shell completion
3367 (ert-deftest python-shell-completion-native-interpreter-disabled-p-1 ()
3368 (let* ((python-shell-completion-native-disabled-interpreters (list "pypy"))
3369 (python-shell-interpreter "/some/path/to/bin/pypy"))
3370 (should (python-shell-completion-native-interpreter-disabled-p))))
3375 ;;; PDB Track integration
3378 ;;; Symbol completion
3395 (ert-deftest python-eldoc--get-symbol-at-point-1 ()
3396 "Test paren handling."
3397 (python-tests-with-temp-buffer
3400 map(codecs.open('somefile'
3402 (python-tests-look-at "ap(xx")
3403 (should (string= (python-eldoc--get-symbol-at-point) "map"))
3404 (goto-char (line-end-position))
3405 (should (string= (python-eldoc--get-symbol-at-point) "map"))
3406 (python-tests-look-at "('somefile'")
3407 (should (string= (python-eldoc--get-symbol-at-point) "map"))
3408 (goto-char (line-end-position))
3409 (should (string= (python-eldoc--get-symbol-at-point) "codecs.open"))))
3411 (ert-deftest python-eldoc--get-symbol-at-point-2 ()
3412 "Ensure self is replaced with the class name."
3413 (python-tests-with-temp-buffer
3417 def some_method(self, n):
3421 return self.some_method(1234)
3424 (python-tests-look-at "self.some_method")
3425 (should (string= (python-eldoc--get-symbol-at-point)
3426 "TheClass.some_method"))
3427 (python-tests-look-at "1234)")
3428 (should (string= (python-eldoc--get-symbol-at-point)
3429 "TheClass.some_method"))))
3431 (ert-deftest python-eldoc--get-symbol-at-point-3 ()
3432 "Ensure symbol is found when point is at end of buffer."
3433 (python-tests-with-temp-buffer
3438 (goto-char (point-max))
3439 (should (string= (python-eldoc--get-symbol-at-point)
3442 (ert-deftest python-eldoc--get-symbol-at-point-4 ()
3443 "Ensure symbol is found when point is at whitespace."
3444 (python-tests-with-temp-buffer
3446 some_symbol some_other_symbol
3448 (python-tests-look-at " some_other_symbol")
3449 (should (string= (python-eldoc--get-symbol-at-point)
3455 (ert-deftest python-imenu-create-index-1 ()
3456 (python-tests-with-temp-buffer
3458 class Foo(models.Model):
3462 class Bar(models.Model):
3466 def decorator(arg1, arg2, arg3):
3467 '''print decorated function call data to stdout.
3471 @decorator('arg1', 'arg2')
3472 def func(a, b, c=True):
3478 def wrapped_f(*args):
3480 print ('Decorator arguments:', arg1, arg2, arg3)
3482 print ('called f(*args)')
3500 (goto-char (point-max))
3503 (cons "Foo (class)" (copy-marker 2))
3504 (cons "Bar (class)" (copy-marker 38))
3507 (cons "*function definition*" (copy-marker 74))
3510 (cons "*function definition*" (copy-marker 254))
3511 (cons "wrapped_f (def)" (copy-marker 294))))
3514 (cons "*class definition*" (copy-marker 519))
3515 (cons "a (def)" (copy-marker 539))
3516 (cons "b (def)" (copy-marker 570))
3519 (cons "*class definition*" (copy-marker 601))
3520 (cons "c (def)" (copy-marker 626)))))
3521 (python-imenu-create-index)))))
3523 (ert-deftest python-imenu-create-index-2 ()
3524 (python-tests-with-temp-buffer
3534 (goto-char (point-max))
3539 (cons "*class definition*" (copy-marker 2))
3542 (cons "*function definition*" (copy-marker 21))
3543 (cons "foo1 (def)" (copy-marker 40)))
3544 (cons "foobar (def)" (copy-marker 78))))
3545 (python-imenu-create-index)))))
3547 (ert-deftest python-imenu-create-index-3 ()
3548 (python-tests-with-temp-buffer
3557 (goto-char (point-max))
3562 (cons "*class definition*" (copy-marker 2))
3565 (cons "*function definition*" (copy-marker 21))
3566 (cons "foo1 (def)" (copy-marker 40))
3567 (cons "foo2 (def)" (copy-marker 77)))))
3568 (python-imenu-create-index)))))
3570 (ert-deftest python-imenu-create-index-4 ()
3571 (python-tests-with-temp-buffer
3584 (goto-char (point-max))
3589 (cons "*class definition*" (copy-marker 2))
3592 (cons "*class definition*" (copy-marker 21))
3593 (cons "__init__ (def)" (copy-marker 44))
3594 (cons "__str__ (def)" (copy-marker 90)))
3595 (cons "__init__ (def)" (copy-marker 135))))
3596 (python-imenu-create-index)))))
3598 (ert-deftest python-imenu-create-flat-index-1 ()
3599 (python-tests-with-temp-buffer
3601 class Foo(models.Model):
3605 class Bar(models.Model):
3609 def decorator(arg1, arg2, arg3):
3610 '''print decorated function call data to stdout.
3614 @decorator('arg1', 'arg2')
3615 def func(a, b, c=True):
3621 def wrapped_f(*args):
3623 print ('Decorator arguments:', arg1, arg2, arg3)
3625 print ('called f(*args)')
3643 (goto-char (point-max))
3645 (list (cons "Foo" (copy-marker 2))
3646 (cons "Bar" (copy-marker 38))
3647 (cons "decorator" (copy-marker 74))
3648 (cons "decorator.wrap" (copy-marker 254))
3649 (cons "decorator.wrap.wrapped_f" (copy-marker 294))
3650 (cons "Baz" (copy-marker 519))
3651 (cons "Baz.a" (copy-marker 539))
3652 (cons "Baz.b" (copy-marker 570))
3653 (cons "Baz.Frob" (copy-marker 601))
3654 (cons "Baz.Frob.c" (copy-marker 626)))
3655 (python-imenu-create-flat-index)))))
3657 (ert-deftest python-imenu-create-flat-index-2 ()
3658 (python-tests-with-temp-buffer
3671 (goto-char (point-max))
3674 (cons "Foo" (copy-marker 2))
3675 (cons "Foo.Bar" (copy-marker 21))
3676 (cons "Foo.Bar.__init__" (copy-marker 44))
3677 (cons "Foo.Bar.__str__" (copy-marker 90))
3678 (cons "Foo.__init__" (copy-marker 135)))
3679 (python-imenu-create-flat-index)))))
3684 (ert-deftest python-info-current-defun-1 ()
3685 (python-tests-with-temp-buffer
3690 (should (string= "foo" (python-info-current-defun)))
3691 (should (string= "def foo" (python-info-current-defun t)))
3693 (should (not (python-info-current-defun)))
3694 (indent-for-tab-command)
3695 (should (string= "foo" (python-info-current-defun)))
3696 (should (string= "def foo" (python-info-current-defun t)))))
3698 (ert-deftest python-info-current-defun-2 ()
3699 (python-tests-with-temp-buffer
3705 return [i for i in range(3)]
3719 (should (string= "C" (python-info-current-defun)))
3720 (should (string= "class C" (python-info-current-defun t)))
3721 (python-tests-look-at "return [i for ")
3722 (should (string= "C.m" (python-info-current-defun)))
3723 (should (string= "def C.m" (python-info-current-defun t)))
3724 (python-tests-look-at "def b():")
3725 (should (string= "C.m.b" (python-info-current-defun)))
3726 (should (string= "def C.m.b" (python-info-current-defun t)))
3728 (indent-for-tab-command)
3729 (python-indent-dedent-line-backspace 1)
3730 (should (string= "C.m" (python-info-current-defun)))
3731 (should (string= "def C.m" (python-info-current-defun t)))
3732 (python-tests-look-at "def c(self):")
3734 (indent-for-tab-command)
3735 (should (string= "C.m.a" (python-info-current-defun)))
3736 (should (string= "def C.m.a" (python-info-current-defun t)))
3737 (python-indent-dedent-line-backspace 1)
3738 (should (string= "C.m" (python-info-current-defun)))
3739 (should (string= "def C.m" (python-info-current-defun t)))
3740 (python-indent-dedent-line-backspace 1)
3741 (should (string= "C" (python-info-current-defun)))
3742 (should (string= "class C" (python-info-current-defun t)))
3743 (python-tests-look-at "def c(self):")
3744 (should (string= "C.c" (python-info-current-defun)))
3745 (should (string= "def C.c" (python-info-current-defun t)))
3746 (python-tests-look-at "do_c()")
3747 (should (string= "C.c" (python-info-current-defun)))
3748 (should (string= "def C.c" (python-info-current-defun t)))))
3750 (ert-deftest python-info-current-defun-3 ()
3751 (python-tests-with-temp-buffer
3753 def decoratorFunctionWithArguments(arg1, arg2, arg3):
3754 '''print decorated function call data to stdout.
3758 @decoratorFunctionWithArguments('arg1', 'arg2')
3759 def func(a, b, c=True):
3764 print 'Inside wwrap()'
3765 def wrapped_f(*args):
3766 print 'Inside wrapped_f()'
3767 print 'Decorator arguments:', arg1, arg2, arg3
3769 print 'After f(*args)'
3773 (python-tests-look-at "def wwrap(f):")
3775 (should (not (python-info-current-defun)))
3776 (indent-for-tab-command 1)
3777 (should (string= (python-info-current-defun)
3778 "decoratorFunctionWithArguments"))
3779 (should (string= (python-info-current-defun t)
3780 "def decoratorFunctionWithArguments"))
3781 (python-tests-look-at "def wrapped_f(*args):")
3782 (should (string= (python-info-current-defun)
3783 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
3784 (should (string= (python-info-current-defun t)
3785 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
3786 (python-tests-look-at "return wrapped_f")
3787 (should (string= (python-info-current-defun)
3788 "decoratorFunctionWithArguments.wwrap"))
3789 (should (string= (python-info-current-defun t)
3790 "def decoratorFunctionWithArguments.wwrap"))
3792 (python-tests-look-at "return wwrap")
3793 (should (string= (python-info-current-defun)
3794 "decoratorFunctionWithArguments"))
3795 (should (string= (python-info-current-defun t)
3796 "def decoratorFunctionWithArguments"))))
3798 (ert-deftest python-info-current-symbol-1 ()
3799 (python-tests-with-temp-buffer
3809 (python-tests-look-at "self.c()")
3810 (should (string= "self.c" (python-info-current-symbol)))
3811 (should (string= "C.c" (python-info-current-symbol t)))))
3813 (ert-deftest python-info-current-symbol-2 ()
3814 (python-tests-with-temp-buffer
3826 (python-tests-look-at "self.c()")
3827 (should (string= "self.c" (python-info-current-symbol)))
3828 (should (string= "C.M.c" (python-info-current-symbol t)))))
3830 (ert-deftest python-info-current-symbol-3 ()
3831 "Keywords should not be considered symbols."
3832 :expected-result :failed
3833 (python-tests-with-temp-buffer
3838 ;; FIXME: keywords are not symbols.
3839 (python-tests-look-at "class C")
3840 (should (not (python-info-current-symbol)))
3841 (should (not (python-info-current-symbol t)))
3842 (python-tests-look-at "C(object)")
3843 (should (string= "C" (python-info-current-symbol)))
3844 (should (string= "class C" (python-info-current-symbol t)))))
3846 (ert-deftest python-info-statement-starts-block-p-1 ()
3847 (python-tests-with-temp-buffer
3849 def long_function_name(
3850 var_one, var_two, var_three,
3854 (python-tests-look-at "def long_function_name")
3855 (should (python-info-statement-starts-block-p))
3856 (python-tests-look-at "print (var_one)")
3857 (python-util-forward-comment -1)
3858 (should (python-info-statement-starts-block-p))))
3860 (ert-deftest python-info-statement-starts-block-p-2 ()
3861 (python-tests-with-temp-buffer
3863 if width == 0 and height == 0 and \\\\
3864 color == 'red' and emphasis == 'strong' or \\\\
3866 raise ValueError('sorry, you lose')
3868 (python-tests-look-at "if width == 0 and")
3869 (should (python-info-statement-starts-block-p))
3870 (python-tests-look-at "raise ValueError(")
3871 (python-util-forward-comment -1)
3872 (should (python-info-statement-starts-block-p))))
3874 (ert-deftest python-info-statement-ends-block-p-1 ()
3875 (python-tests-with-temp-buffer
3877 def long_function_name(
3878 var_one, var_two, var_three,
3882 (python-tests-look-at "print (var_one)")
3883 (should (python-info-statement-ends-block-p))))
3885 (ert-deftest python-info-statement-ends-block-p-2 ()
3886 (python-tests-with-temp-buffer
3888 if width == 0 and height == 0 and \\\\
3889 color == 'red' and emphasis == 'strong' or \\\\
3896 (python-tests-look-at "raise ValueError(")
3897 (should (python-info-statement-ends-block-p))))
3899 (ert-deftest python-info-beginning-of-statement-p-1 ()
3900 (python-tests-with-temp-buffer
3902 def long_function_name(
3903 var_one, var_two, var_three,
3907 (python-tests-look-at "def long_function_name")
3908 (should (python-info-beginning-of-statement-p))
3910 (should (not (python-info-beginning-of-statement-p)))
3911 (python-tests-look-at "print (var_one)")
3912 (should (python-info-beginning-of-statement-p))
3913 (goto-char (line-beginning-position))
3914 (should (not (python-info-beginning-of-statement-p)))))
3916 (ert-deftest python-info-beginning-of-statement-p-2 ()
3917 (python-tests-with-temp-buffer
3919 if width == 0 and height == 0 and \\\\
3920 color == 'red' and emphasis == 'strong' or \\\\
3927 (python-tests-look-at "if width == 0 and")
3928 (should (python-info-beginning-of-statement-p))
3930 (should (not (python-info-beginning-of-statement-p)))
3931 (python-tests-look-at "raise ValueError(")
3932 (should (python-info-beginning-of-statement-p))
3933 (goto-char (line-beginning-position))
3934 (should (not (python-info-beginning-of-statement-p)))))
3936 (ert-deftest python-info-end-of-statement-p-1 ()
3937 (python-tests-with-temp-buffer
3939 def long_function_name(
3940 var_one, var_two, var_three,
3944 (python-tests-look-at "def long_function_name")
3945 (should (not (python-info-end-of-statement-p)))
3947 (should (not (python-info-end-of-statement-p)))
3948 (python-tests-look-at "print (var_one)")
3949 (python-util-forward-comment -1)
3950 (should (python-info-end-of-statement-p))
3951 (python-tests-look-at "print (var_one)")
3952 (should (not (python-info-end-of-statement-p)))
3954 (should (python-info-end-of-statement-p))))
3956 (ert-deftest python-info-end-of-statement-p-2 ()
3957 (python-tests-with-temp-buffer
3959 if width == 0 and height == 0 and \\\\
3960 color == 'red' and emphasis == 'strong' or \\\\
3967 (python-tests-look-at "if width == 0 and")
3968 (should (not (python-info-end-of-statement-p)))
3970 (should (not (python-info-end-of-statement-p)))
3971 (python-tests-look-at "raise ValueError(")
3972 (python-util-forward-comment -1)
3973 (should (python-info-end-of-statement-p))
3974 (python-tests-look-at "raise ValueError(")
3975 (should (not (python-info-end-of-statement-p)))
3977 (should (not (python-info-end-of-statement-p)))
3978 (goto-char (point-max))
3979 (python-util-forward-comment -1)
3980 (should (python-info-end-of-statement-p))))
3982 (ert-deftest python-info-beginning-of-block-p-1 ()
3983 (python-tests-with-temp-buffer
3985 def long_function_name(
3986 var_one, var_two, var_three,
3990 (python-tests-look-at "def long_function_name")
3991 (should (python-info-beginning-of-block-p))
3992 (python-tests-look-at "var_one, var_two, var_three,")
3993 (should (not (python-info-beginning-of-block-p)))
3994 (python-tests-look-at "print (var_one)")
3995 (should (not (python-info-beginning-of-block-p)))))
3997 (ert-deftest python-info-beginning-of-block-p-2 ()
3998 (python-tests-with-temp-buffer
4000 if width == 0 and height == 0 and \\\\
4001 color == 'red' and emphasis == 'strong' or \\\\
4008 (python-tests-look-at "if width == 0 and")
4009 (should (python-info-beginning-of-block-p))
4010 (python-tests-look-at "color == 'red' and emphasis")
4011 (should (not (python-info-beginning-of-block-p)))
4012 (python-tests-look-at "raise ValueError(")
4013 (should (not (python-info-beginning-of-block-p)))))
4015 (ert-deftest python-info-end-of-block-p-1 ()
4016 (python-tests-with-temp-buffer
4018 def long_function_name(
4019 var_one, var_two, var_three,
4023 (python-tests-look-at "def long_function_name")
4024 (should (not (python-info-end-of-block-p)))
4025 (python-tests-look-at "var_one, var_two, var_three,")
4026 (should (not (python-info-end-of-block-p)))
4027 (python-tests-look-at "var_four):")
4029 (should (not (python-info-end-of-block-p)))
4030 (python-tests-look-at "print (var_one)")
4031 (should (not (python-info-end-of-block-p)))
4033 (should (python-info-end-of-block-p))))
4035 (ert-deftest python-info-end-of-block-p-2 ()
4036 (python-tests-with-temp-buffer
4038 if width == 0 and height == 0 and \\\\
4039 color == 'red' and emphasis == 'strong' or \\\\
4046 (python-tests-look-at "if width == 0 and")
4047 (should (not (python-info-end-of-block-p)))
4048 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
4049 (should (not (python-info-end-of-block-p)))
4050 (python-tests-look-at "highlight > 100:")
4052 (should (not (python-info-end-of-block-p)))
4053 (python-tests-look-at "raise ValueError(")
4054 (should (not (python-info-end-of-block-p)))
4056 (should (not (python-info-end-of-block-p)))
4057 (goto-char (point-max))
4058 (python-util-forward-comment -1)
4059 (should (python-info-end-of-block-p))))
4061 (ert-deftest python-info-dedenter-opening-block-position-1 ()
4062 (python-tests-with-temp-buffer
4064 if request.user.is_authenticated():
4066 profile = request.user.get_profile()
4067 except Profile.DoesNotExist:
4068 profile = Profile.objects.create(user=request.user)
4071 profile.recalculate_stats()
4073 profile.clear_stats()
4078 (python-tests-look-at "try:")
4079 (should (not (python-info-dedenter-opening-block-position)))
4080 (python-tests-look-at "except Profile.DoesNotExist:")
4081 (should (= (python-tests-look-at "try:" -1 t)
4082 (python-info-dedenter-opening-block-position)))
4083 (python-tests-look-at "else:")
4084 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -1 t)
4085 (python-info-dedenter-opening-block-position)))
4086 (python-tests-look-at "if profile.stats:")
4087 (should (not (python-info-dedenter-opening-block-position)))
4088 (python-tests-look-at "else:")
4089 (should (= (python-tests-look-at "if profile.stats:" -1 t)
4090 (python-info-dedenter-opening-block-position)))
4091 (python-tests-look-at "finally:")
4092 (should (= (python-tests-look-at "else:" -2 t)
4093 (python-info-dedenter-opening-block-position)))))
4095 (ert-deftest python-info-dedenter-opening-block-position-2 ()
4096 (python-tests-with-temp-buffer
4098 if request.user.is_authenticated():
4099 profile = Profile.objects.get_or_create(user=request.user)
4101 profile.recalculate_stats()
4108 (python-tests-look-at "'else': 'do it'")
4109 (should (not (python-info-dedenter-opening-block-position)))
4110 (python-tests-look-at "'else'")
4111 (should (not (python-info-dedenter-opening-block-position)))))
4113 (ert-deftest python-info-dedenter-opening-block-position-3 ()
4114 (python-tests-with-temp-buffer
4120 msg = 'Error saving to disk'
4122 logger.exception(msg)
4125 logger.exception('Unhandled exception')
4130 (python-tests-look-at "try:")
4131 (should (not (python-info-dedenter-opening-block-position)))
4133 (python-tests-look-at "except IOError:")
4134 (should (= (python-tests-look-at "try:" -1 t)
4135 (python-info-dedenter-opening-block-position)))
4137 (python-tests-look-at "except Exception:")
4138 (should (= (python-tests-look-at "except IOError:" -1 t)
4139 (python-info-dedenter-opening-block-position)))
4141 (python-tests-look-at "if hide_details:")
4142 (should (not (python-info-dedenter-opening-block-position)))
4144 ;; check indentation modifies the detected opening block
4145 (python-tests-look-at "else")
4146 (should (= (python-tests-look-at "if hide_details:" -1 t)
4147 (python-info-dedenter-opening-block-position)))
4150 (should (= (python-tests-look-at "if hide_details:" -1 t)
4151 (python-info-dedenter-opening-block-position)))
4154 (should (= (python-tests-look-at "except Exception:" -1 t)
4155 (python-info-dedenter-opening-block-position)))
4158 (should (= (python-tests-look-at "if save:" -1 t)
4159 (python-info-dedenter-opening-block-position)))))
4161 (ert-deftest python-info-dedenter-opening-block-positions-1 ()
4162 (python-tests-with-temp-buffer
4168 msg = 'Error saving to disk'
4170 logger.exception(msg)
4173 logger.exception('Unhandled exception')
4178 (python-tests-look-at "try:")
4179 (should (not (python-info-dedenter-opening-block-positions)))
4181 (python-tests-look-at "except IOError:")
4184 (python-tests-look-at "try:" -1 t))
4185 (python-info-dedenter-opening-block-positions)))
4187 (python-tests-look-at "except Exception:")
4190 (python-tests-look-at "except IOError:" -1 t))
4191 (python-info-dedenter-opening-block-positions)))
4193 (python-tests-look-at "if hide_details:")
4194 (should (not (python-info-dedenter-opening-block-positions)))
4196 ;; check indentation does not modify the detected opening blocks
4197 (python-tests-look-at "else")
4200 (python-tests-look-at "if hide_details:" -1 t)
4201 (python-tests-look-at "except Exception:" -1 t)
4202 (python-tests-look-at "if save:" -1 t))
4203 (python-info-dedenter-opening-block-positions)))
4208 (python-tests-look-at "if hide_details:" -1 t)
4209 (python-tests-look-at "except Exception:" -1 t)
4210 (python-tests-look-at "if save:" -1 t))
4211 (python-info-dedenter-opening-block-positions)))
4216 (python-tests-look-at "if hide_details:" -1 t)
4217 (python-tests-look-at "except Exception:" -1 t)
4218 (python-tests-look-at "if save:" -1 t))
4219 (python-info-dedenter-opening-block-positions)))
4224 (python-tests-look-at "if hide_details:" -1 t)
4225 (python-tests-look-at "except Exception:" -1 t)
4226 (python-tests-look-at "if save:" -1 t))
4227 (python-info-dedenter-opening-block-positions)))))
4229 (ert-deftest python-info-dedenter-opening-block-positions-2 ()
4230 "Test detection of opening blocks for elif."
4231 (python-tests-with-temp-buffer
4240 (python-tests-look-at "elif var3:")
4243 (python-tests-look-at "if var2:" -1 t)
4244 (python-tests-look-at "if var:" -1 t))
4245 (python-info-dedenter-opening-block-positions)))
4247 (python-tests-look-at "elif\n")
4250 (python-tests-look-at "elif var3:" -1 t)
4251 (python-tests-look-at "if var:" -1 t))
4252 (python-info-dedenter-opening-block-positions)))))
4254 (ert-deftest python-info-dedenter-opening-block-positions-3 ()
4255 "Test detection of opening blocks for else."
4256 (python-tests-with-temp-buffer
4278 (python-tests-look-at "else\n")
4281 (python-tests-look-at "elif var3:" -1 t)
4282 (python-tests-look-at "if var:" -1 t)
4283 (python-tests-look-at "except:" -1 t))
4284 (python-info-dedenter-opening-block-positions)))
4286 (python-tests-look-at "else\n")
4289 (python-tests-look-at "while var5:" -1 t)
4290 (python-tests-look-at "if var4:" -1 t))
4291 (python-info-dedenter-opening-block-positions)))
4293 (python-tests-look-at "else\n")
4296 (python-tests-look-at "if value > 0:" -1 t)
4297 (python-tests-look-at "for value in var6:" -1 t)
4298 (python-tests-look-at "if var4:" -1 t))
4299 (python-info-dedenter-opening-block-positions)))))
4301 (ert-deftest python-info-dedenter-opening-block-positions-4 ()
4302 "Test detection of opening blocks for except."
4303 (python-tests-with-temp-buffer
4311 (python-tests-look-at "except ValueError:")
4313 (equal (list (python-tests-look-at "try:" -1 t))
4314 (python-info-dedenter-opening-block-positions)))
4316 (python-tests-look-at "except\n")
4318 (equal (list (python-tests-look-at "except ValueError:" -1 t))
4319 (python-info-dedenter-opening-block-positions)))))
4321 (ert-deftest python-info-dedenter-opening-block-positions-5 ()
4322 "Test detection of opening blocks for finally."
4323 (python-tests-with-temp-buffer
4332 logger.exception('something went wrong')
4336 something_else_else()
4338 logger.exception('something else went wrong')
4343 (python-tests-look-at "finally\n")
4345 (equal (list (python-tests-look-at "try:" -1 t))
4346 (python-info-dedenter-opening-block-positions)))
4348 (python-tests-look-at "finally\n")
4350 (equal (list (python-tests-look-at "except:" -1 t))
4351 (python-info-dedenter-opening-block-positions)))
4353 (python-tests-look-at "finally\n")
4355 (equal (list (python-tests-look-at "else:" -1 t))
4356 (python-info-dedenter-opening-block-positions)))))
4358 (ert-deftest python-info-dedenter-opening-block-message-1 ()
4359 "Test dedenters inside strings are ignored."
4360 (python-tests-with-temp-buffer
4365 logger.exception('something went wrong')
4368 (python-tests-look-at "except\n")
4369 (should (not (python-info-dedenter-opening-block-message)))))
4371 (ert-deftest python-info-dedenter-opening-block-message-2 ()
4372 "Test except keyword."
4373 (python-tests-with-temp-buffer
4378 logger.exception('something went wrong')
4380 (python-tests-look-at "except:")
4383 (substring-no-properties
4384 (python-info-dedenter-opening-block-message))))
4388 (substring-no-properties
4389 (python-info-dedenter-opening-block-message))))))
4391 (ert-deftest python-info-dedenter-opening-block-message-3 ()
4392 "Test else keyword."
4393 (python-tests-with-temp-buffer
4398 logger.exception('something went wrong')
4400 logger.debug('all good')
4402 (python-tests-look-at "else:")
4405 (substring-no-properties
4406 (python-info-dedenter-opening-block-message))))
4410 (substring-no-properties
4411 (python-info-dedenter-opening-block-message))))))
4413 (ert-deftest python-info-dedenter-opening-block-message-4 ()
4414 "Test finally keyword."
4415 (python-tests-with-temp-buffer
4420 logger.exception('something went wrong')
4422 logger.debug('all good')
4426 (python-tests-look-at "finally:")
4429 (substring-no-properties
4430 (python-info-dedenter-opening-block-message))))
4434 (substring-no-properties
4435 (python-info-dedenter-opening-block-message))))))
4437 (ert-deftest python-info-dedenter-opening-block-message-5 ()
4438 "Test elif keyword."
4439 (python-tests-with-temp-buffer
4445 (python-tests-look-at "elif b:")
4448 (substring-no-properties
4449 (python-info-dedenter-opening-block-message))))
4453 (substring-no-properties
4454 (python-info-dedenter-opening-block-message))))))
4457 (ert-deftest python-info-dedenter-statement-p-1 ()
4458 "Test dedenters inside strings are ignored."
4459 (python-tests-with-temp-buffer
4464 logger.exception('something went wrong')
4467 (python-tests-look-at "except\n")
4468 (should (not (python-info-dedenter-statement-p)))))
4470 (ert-deftest python-info-dedenter-statement-p-2 ()
4471 "Test except keyword."
4472 (python-tests-with-temp-buffer
4477 logger.exception('something went wrong')
4479 (python-tests-look-at "except:")
4480 (should (= (point) (python-info-dedenter-statement-p)))
4482 (should (= (save-excursion
4483 (back-to-indentation)
4485 (python-info-dedenter-statement-p)))))
4487 (ert-deftest python-info-dedenter-statement-p-3 ()
4488 "Test else keyword."
4489 (python-tests-with-temp-buffer
4494 logger.exception('something went wrong')
4496 logger.debug('all good')
4498 (python-tests-look-at "else:")
4499 (should (= (point) (python-info-dedenter-statement-p)))
4501 (should (= (save-excursion
4502 (back-to-indentation)
4504 (python-info-dedenter-statement-p)))))
4506 (ert-deftest python-info-dedenter-statement-p-4 ()
4507 "Test finally keyword."
4508 (python-tests-with-temp-buffer
4513 logger.exception('something went wrong')
4515 logger.debug('all good')
4519 (python-tests-look-at "finally:")
4520 (should (= (point) (python-info-dedenter-statement-p)))
4522 (should (= (save-excursion
4523 (back-to-indentation)
4525 (python-info-dedenter-statement-p)))))
4527 (ert-deftest python-info-dedenter-statement-p-5 ()
4528 "Test elif keyword."
4529 (python-tests-with-temp-buffer
4535 (python-tests-look-at "elif b:")
4536 (should (= (point) (python-info-dedenter-statement-p)))
4538 (should (= (save-excursion
4539 (back-to-indentation)
4541 (python-info-dedenter-statement-p)))))
4543 (ert-deftest python-info-line-ends-backslash-p-1 ()
4544 (python-tests-with-temp-buffer
4546 objects = Thing.objects.all() \\\\
4556 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
4557 (should (python-info-line-ends-backslash-p 3))
4558 (should (python-info-line-ends-backslash-p 4))
4559 (should (python-info-line-ends-backslash-p 5))
4560 (should (python-info-line-ends-backslash-p 6)) ; ) \...
4561 (should (python-info-line-ends-backslash-p 7))
4562 (should (python-info-line-ends-backslash-p 8))
4563 (should (python-info-line-ends-backslash-p 9))
4564 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
4566 (ert-deftest python-info-beginning-of-backslash-1 ()
4567 (python-tests-with-temp-buffer
4569 objects = Thing.objects.all() \\\\
4580 (second (python-tests-look-at ".filter("))
4581 (third (python-tests-look-at ".aggregate(")))
4582 (should (= first (python-info-beginning-of-backslash 2)))
4583 (should (= second (python-info-beginning-of-backslash 3)))
4584 (should (= second (python-info-beginning-of-backslash 4)))
4585 (should (= second (python-info-beginning-of-backslash 5)))
4586 (should (= second (python-info-beginning-of-backslash 6)))
4587 (should (= third (python-info-beginning-of-backslash 7)))
4588 (should (= third (python-info-beginning-of-backslash 8)))
4589 (should (= third (python-info-beginning-of-backslash 9)))
4590 (should (not (python-info-beginning-of-backslash 10))))))
4592 (ert-deftest python-info-continuation-line-p-1 ()
4593 (python-tests-with-temp-buffer
4595 if width == 0 and height == 0 and \\\\
4596 color == 'red' and emphasis == 'strong' or \\\\
4603 (python-tests-look-at "if width == 0 and height == 0 and")
4604 (should (not (python-info-continuation-line-p)))
4605 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
4606 (should (python-info-continuation-line-p))
4607 (python-tests-look-at "highlight > 100:")
4608 (should (python-info-continuation-line-p))
4609 (python-tests-look-at "raise ValueError(")
4610 (should (not (python-info-continuation-line-p)))
4611 (python-tests-look-at "'sorry, you lose'")
4612 (should (python-info-continuation-line-p))
4614 (should (python-info-continuation-line-p))
4615 (python-tests-look-at ")")
4616 (should (python-info-continuation-line-p))
4618 (should (not (python-info-continuation-line-p)))))
4620 (ert-deftest python-info-block-continuation-line-p-1 ()
4621 (python-tests-with-temp-buffer
4623 if width == 0 and height == 0 and \\\\
4624 color == 'red' and emphasis == 'strong' or \\\\
4631 (python-tests-look-at "if width == 0 and")
4632 (should (not (python-info-block-continuation-line-p)))
4633 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
4634 (should (= (python-info-block-continuation-line-p)
4635 (python-tests-look-at "if width == 0 and" -1 t)))
4636 (python-tests-look-at "highlight > 100:")
4637 (should (not (python-info-block-continuation-line-p)))))
4639 (ert-deftest python-info-block-continuation-line-p-2 ()
4640 (python-tests-with-temp-buffer
4647 (python-tests-look-at "def foo(a,")
4648 (should (not (python-info-block-continuation-line-p)))
4649 (python-tests-look-at "b,")
4650 (should (= (python-info-block-continuation-line-p)
4651 (python-tests-look-at "def foo(a," -1 t)))
4652 (python-tests-look-at "c):")
4653 (should (not (python-info-block-continuation-line-p)))))
4655 (ert-deftest python-info-assignment-statement-p-1 ()
4656 (python-tests-with-temp-buffer
4658 data = foo(), bar() \\\\
4662 (python-tests-look-at "data = foo(), bar()")
4663 (should (python-info-assignment-statement-p))
4664 (should (python-info-assignment-statement-p t))
4665 (python-tests-look-at "baz(), 4")
4666 (should (python-info-assignment-statement-p))
4667 (should (not (python-info-assignment-statement-p t)))
4668 (python-tests-look-at "5, 6")
4669 (should (python-info-assignment-statement-p))
4670 (should (not (python-info-assignment-statement-p t)))))
4672 (ert-deftest python-info-assignment-statement-p-2 ()
4673 (python-tests-with-temp-buffer
4675 data = (foo(), bar()
4679 (python-tests-look-at "data = (foo(), bar()")
4680 (should (python-info-assignment-statement-p))
4681 (should (python-info-assignment-statement-p t))
4682 (python-tests-look-at "baz(), 4")
4683 (should (python-info-assignment-statement-p))
4684 (should (not (python-info-assignment-statement-p t)))
4685 (python-tests-look-at "5, 6)")
4686 (should (python-info-assignment-statement-p))
4687 (should (not (python-info-assignment-statement-p t)))))
4689 (ert-deftest python-info-assignment-statement-p-3 ()
4690 (python-tests-with-temp-buffer
4694 (python-tests-look-at "data '=' 42")
4695 (should (not (python-info-assignment-statement-p)))
4696 (should (not (python-info-assignment-statement-p t)))))
4698 (ert-deftest python-info-assignment-continuation-line-p-1 ()
4699 (python-tests-with-temp-buffer
4701 data = foo(), bar() \\\\
4705 (python-tests-look-at "data = foo(), bar()")
4706 (should (not (python-info-assignment-continuation-line-p)))
4707 (python-tests-look-at "baz(), 4")
4708 (should (= (python-info-assignment-continuation-line-p)
4709 (python-tests-look-at "foo()," -1 t)))
4710 (python-tests-look-at "5, 6")
4711 (should (not (python-info-assignment-continuation-line-p)))))
4713 (ert-deftest python-info-assignment-continuation-line-p-2 ()
4714 (python-tests-with-temp-buffer
4716 data = (foo(), bar()
4720 (python-tests-look-at "data = (foo(), bar()")
4721 (should (not (python-info-assignment-continuation-line-p)))
4722 (python-tests-look-at "baz(), 4")
4723 (should (= (python-info-assignment-continuation-line-p)
4724 (python-tests-look-at "(foo()," -1 t)))
4725 (python-tests-look-at "5, 6)")
4726 (should (not (python-info-assignment-continuation-line-p)))))
4728 (ert-deftest python-info-looking-at-beginning-of-defun-1 ()
4729 (python-tests-with-temp-buffer
4731 def decorat0r(deff):
4742 (python-tests-look-at "def decorat0r(deff):")
4743 (should (python-info-looking-at-beginning-of-defun))
4744 (python-tests-look-at "def foo(arg):")
4745 (should (not (python-info-looking-at-beginning-of-defun)))
4746 (python-tests-look-at "def wrap():")
4747 (should (python-info-looking-at-beginning-of-defun))
4748 (python-tests-look-at "deff()")
4749 (should (not (python-info-looking-at-beginning-of-defun)))))
4751 (ert-deftest python-info-current-line-comment-p-1 ()
4752 (python-tests-with-temp-buffer
4755 foo = True # another comment
4759 print ('bar') # print bar
4761 (python-tests-look-at "# this is a comment")
4762 (should (python-info-current-line-comment-p))
4763 (python-tests-look-at "foo = True # another comment")
4764 (should (not (python-info-current-line-comment-p)))
4765 (python-tests-look-at "'#this is a string'")
4766 (should (not (python-info-current-line-comment-p)))
4767 (python-tests-look-at "# more comments")
4768 (should (python-info-current-line-comment-p))
4769 (python-tests-look-at "print ('bar') # print bar")
4770 (should (not (python-info-current-line-comment-p)))))
4772 (ert-deftest python-info-current-line-empty-p ()
4773 (python-tests-with-temp-buffer
4777 foo = True # another comment
4779 (should (python-info-current-line-empty-p))
4780 (python-tests-look-at "# this is a comment")
4781 (should (not (python-info-current-line-empty-p)))
4783 (should (python-info-current-line-empty-p))))
4785 (ert-deftest python-info-docstring-p-1 ()
4786 "Test module docstring detection."
4787 (python-tests-with-temp-buffer
4788 "# -*- coding: utf-8 -*-
4792 Module Docstring Django style.
4794 u'''Additional module docstring.'''
4795 '''Not a module docstring.'''
4797 (python-tests-look-at "Module Docstring Django style.")
4798 (should (python-info-docstring-p))
4799 (python-tests-look-at "u'''Additional module docstring.'''")
4800 (should (python-info-docstring-p))
4801 (python-tests-look-at "'''Not a module docstring.'''")
4802 (should (not (python-info-docstring-p)))))
4804 (ert-deftest python-info-docstring-p-2 ()
4805 "Test variable docstring detection."
4806 (python-tests-with-temp-buffer
4809 U'''Variable docstring.'''
4810 '''Additional variable docstring.'''
4811 '''Not a variable docstring.'''
4813 (python-tests-look-at "Variable docstring.")
4814 (should (python-info-docstring-p))
4815 (python-tests-look-at "u'''Additional variable docstring.'''")
4816 (should (python-info-docstring-p))
4817 (python-tests-look-at "'''Not a variable docstring.'''")
4818 (should (not (python-info-docstring-p)))))
4820 (ert-deftest python-info-docstring-p-3 ()
4821 "Test function docstring detection."
4822 (python-tests-with-temp-buffer
4830 R'''Additional function docstring.'''
4831 '''Not a function docstring.'''
4834 (python-tests-look-at "Function docstring.")
4835 (should (python-info-docstring-p))
4836 (python-tests-look-at "R'''Additional function docstring.'''")
4837 (should (python-info-docstring-p))
4838 (python-tests-look-at "'''Not a function docstring.'''")
4839 (should (not (python-info-docstring-p)))))
4841 (ert-deftest python-info-docstring-p-4 ()
4842 "Test class docstring detection."
4843 (python-tests-with-temp-buffer
4852 Additional class docstring.
4854 '''Not a class docstring.'''
4857 (python-tests-look-at "Class docstring.")
4858 (should (python-info-docstring-p))
4859 (python-tests-look-at "uR'''") ;; Additional class docstring
4860 (should (python-info-docstring-p))
4861 (python-tests-look-at "'''Not a class docstring.'''")
4862 (should (not (python-info-docstring-p)))))
4864 (ert-deftest python-info-docstring-p-5 ()
4865 "Test class attribute docstring detection."
4866 (python-tests-with-temp-buffer
4871 Class attribute docstring.
4877 Additional class attribute docstring.
4879 '''Not a class attribute docstring.'''
4882 (python-tests-look-at "Class attribute docstring.")
4883 (should (python-info-docstring-p))
4884 (python-tests-look-at "UR'''") ;; Additional class attr docstring
4885 (should (python-info-docstring-p))
4886 (python-tests-look-at "'''Not a class attribute docstring.'''")
4887 (should (not (python-info-docstring-p)))))
4889 (ert-deftest python-info-docstring-p-6 ()
4890 "Test class method docstring detection."
4891 (python-tests-with-temp-buffer
4895 def __init__(self, a, b):
4900 '''Method docstring.
4904 '''Additional method docstring.'''
4905 '''Not a method docstring.'''
4906 return self.a + self.b
4908 (python-tests-look-at "Method docstring.")
4909 (should (python-info-docstring-p))
4910 (python-tests-look-at "'''Additional method docstring.'''")
4911 (should (python-info-docstring-p))
4912 (python-tests-look-at "'''Not a method docstring.'''")
4913 (should (not (python-info-docstring-p)))))
4915 (ert-deftest python-info-encoding-from-cookie-1 ()
4916 "Should detect it on first line."
4917 (python-tests-with-temp-buffer
4920 foo = True # another comment
4922 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4924 (ert-deftest python-info-encoding-from-cookie-2 ()
4925 "Should detect it on second line."
4926 (python-tests-with-temp-buffer
4930 foo = True # another comment
4932 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4934 (ert-deftest python-info-encoding-from-cookie-3 ()
4935 "Should not be detected on third line (and following ones)."
4936 (python-tests-with-temp-buffer
4940 foo = True # another comment
4942 (should (not (python-info-encoding-from-cookie)))))
4944 (ert-deftest python-info-encoding-from-cookie-4 ()
4945 "Should detect Emacs style."
4946 (python-tests-with-temp-buffer
4947 "# -*- coding: latin-1 -*-
4949 foo = True # another comment"
4950 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4952 (ert-deftest python-info-encoding-from-cookie-5 ()
4953 "Should detect Vim style."
4954 (python-tests-with-temp-buffer
4955 "# vim: set fileencoding=latin-1 :
4957 foo = True # another comment"
4958 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4960 (ert-deftest python-info-encoding-from-cookie-6 ()
4961 "First cookie wins."
4962 (python-tests-with-temp-buffer
4963 "# -*- coding: iso-8859-1 -*-
4964 # vim: set fileencoding=latin-1 :
4966 foo = True # another comment"
4967 (should (eq (python-info-encoding-from-cookie) 'iso-8859-1))))
4969 (ert-deftest python-info-encoding-from-cookie-7 ()
4970 "First cookie wins."
4971 (python-tests-with-temp-buffer
4972 "# vim: set fileencoding=latin-1 :
4973 # -*- coding: iso-8859-1 -*-
4975 foo = True # another comment"
4976 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4978 (ert-deftest python-info-encoding-1 ()
4979 "Should return the detected encoding from cookie."
4980 (python-tests-with-temp-buffer
4981 "# vim: set fileencoding=latin-1 :
4983 foo = True # another comment"
4984 (should (eq (python-info-encoding) 'latin-1))))
4986 (ert-deftest python-info-encoding-2 ()
4987 "Should default to utf-8."
4988 (python-tests-with-temp-buffer
4989 "# No encoding for you
4991 foo = True # another comment"
4992 (should (eq (python-info-encoding) 'utf-8))))
4995 ;;; Utility functions
4997 (ert-deftest python-util-goto-line-1 ()
4998 (python-tests-with-temp-buffer
5003 pass" (make-string 20 ?\n))
5004 (python-util-goto-line 10)
5005 (should (= (line-number-at-pos) 10))
5006 (python-util-goto-line 20)
5007 (should (= (line-number-at-pos) 20))))
5009 (ert-deftest python-util-clone-local-variables-1 ()
5010 (let ((buffer (generate-new-buffer
5011 "python-util-clone-local-variables-1"))
5013 '((python-fill-docstring-style . django)
5014 (python-shell-interpreter . "python")
5015 (python-shell-interpreter-args . "manage.py shell")
5016 (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
5017 (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
5018 (python-shell-extra-pythonpaths "/home/user/pylib/")
5019 (python-shell-completion-setup-code
5020 . "from IPython.core.completerlib import module_completion")
5021 (python-shell-completion-string-code
5022 . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
5023 (python-shell-virtualenv-root
5024 . "/home/user/.virtualenvs/project"))))
5025 (with-current-buffer buffer
5026 (kill-all-local-variables)
5027 (dolist (ccons varcons)
5028 (set (make-local-variable (car ccons)) (cdr ccons))))
5029 (python-tests-with-temp-buffer
5031 (python-util-clone-local-variables buffer)
5032 (dolist (ccons varcons)
5034 (equal (symbol-value (car ccons)) (cdr ccons)))))
5035 (kill-buffer buffer)))
5037 (ert-deftest python-util-strip-string-1 ()
5038 (should (string= (python-util-strip-string "\t\r\n str") "str"))
5039 (should (string= (python-util-strip-string "str \n\r") "str"))
5040 (should (string= (python-util-strip-string "\t\r\n str \n\r ") "str"))
5042 (string= (python-util-strip-string "\n str \nin \tg \n\r") "str \nin \tg"))
5043 (should (string= (python-util-strip-string "\n \t \n\r ") ""))
5044 (should (string= (python-util-strip-string "") "")))
5046 (ert-deftest python-util-forward-comment-1 ()
5047 (python-tests-with-temp-buffer
5051 # bad indented comment
5052 # more comments" (make-string 9999 ?\n))
5053 (python-util-forward-comment 1)
5054 (should (= (point) (point-max)))
5055 (python-util-forward-comment -1)
5056 (should (= (point) (point-min)))))
5058 (ert-deftest python-util-valid-regexp-p-1 ()
5059 (should (python-util-valid-regexp-p ""))
5060 (should (python-util-valid-regexp-p python-shell-prompt-regexp))
5061 (should (not (python-util-valid-regexp-p "\\("))))
5066 (ert-deftest python-parens-electric-indent-1 ()
5067 (let ((eim electric-indent-mode))
5070 (python-tests-with-temp-buffer
5072 from django.conf.urls import patterns, include, url
5074 from django.contrib import admin
5076 from myapp import views
5079 urlpatterns = patterns('',
5080 url(r'^$', views.index
5083 (electric-indent-mode 1)
5084 (python-tests-look-at "views.index")
5087 ;; Inserting commas within the same line should leave
5088 ;; indentation unchanged.
5089 (python-tests-self-insert ",")
5090 (should (= (current-indentation) 4))
5092 ;; As well as any other input happening within the same
5094 (python-tests-self-insert " name='index')")
5095 (should (= (current-indentation) 4))
5097 ;; But a comma outside it, should trigger indentation.
5098 (python-tests-self-insert ",")
5099 (should (= (current-indentation) 23))
5101 ;; Newline indents to the first argument column
5102 (python-tests-self-insert "\n")
5103 (should (= (current-indentation) 23))
5105 ;; All this input must not change indentation
5107 (python-tests-self-insert "url(r'^/login$', views.login)")
5108 (should (= (current-indentation) 4))
5110 ;; But this comma does
5111 (python-tests-self-insert ",")
5112 (should (= (current-indentation) 23))))
5113 (or eim (electric-indent-mode -1)))))
5115 (ert-deftest python-triple-quote-pairing ()
5116 (let ((epm electric-pair-mode))
5119 (python-tests-with-temp-buffer
5121 (or epm (electric-pair-mode 1))
5122 (goto-char (1- (point-max)))
5123 (python-tests-self-insert ?\")
5124 (should (string= (buffer-string)
5126 (should (= (point) 4)))
5127 (python-tests-with-temp-buffer
5129 (python-tests-self-insert (list ?\" ?\" ?\"))
5130 (should (string= (buffer-string)
5132 (should (= (point) 4)))
5133 (python-tests-with-temp-buffer
5135 (goto-char (1- (point-max)))
5136 (python-tests-self-insert ?\")
5137 (should (= (point) (1- (point-max))))
5138 (should (string= (buffer-string)
5140 (or epm (electric-pair-mode -1)))))
5143 ;;; Hideshow support
5145 (ert-deftest python-hideshow-hide-levels-1 ()
5146 "Should hide all methods when called after class start."
5147 (let ((enabled hs-minor-mode))
5150 (python-tests-with-temp-buffer
5154 def __init__(self, arg, kwarg=1):
5158 def filter(self, nums):
5160 return item in [self.arg, self.kwarg]
5161 return filter(fn, nums)
5164 return '%s-%s' % (self.arg, self.kwarg)
5167 (python-tests-look-at "class SomeClass:")
5172 (python-tests-visible-string)
5176 def __init__(self, arg, kwarg=1):
5177 def filter(self, nums):
5178 def __str__(self):"))))
5179 (or enabled (hs-minor-mode -1)))))
5181 (ert-deftest python-hideshow-hide-levels-2 ()
5182 "Should hide nested methods and parens at end of defun."
5183 (let ((enabled hs-minor-mode))
5186 (python-tests-with-temp-buffer
5190 def __init__(self, arg, kwarg=1):
5194 def filter(self, nums):
5196 return item in [self.arg, self.kwarg]
5197 return filter(fn, nums)
5200 return '%s-%s' % (self.arg, self.kwarg)
5203 (python-tests-look-at "def fn(item):")
5207 (python-tests-visible-string)
5211 def __init__(self, arg, kwarg=1):
5215 def filter(self, nums):
5217 return filter(fn, nums)
5220 return '%s-%s' % (self.arg, self.kwarg)
5222 (or enabled (hs-minor-mode -1)))))
5226 (provide 'python-tests)
5229 ;; indent-tabs-mode: nil
5232 ;;; python-tests.el ends here