1 ;;; python-tests.el --- Test suite for python.el
3 ;; Copyright (C) 2013-2015 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:
32 (defmacro python-tests-with-temp-buffer
(contents &rest body
)
33 "Create a `python-mode' enabled temp buffer with CONTENTS.
34 BODY is code to be executed within the temp buffer. Point is
35 always located at the beginning of buffer."
36 (declare (indent 1) (debug t
))
40 (goto-char (point-min))
43 (defmacro python-tests-with-temp-file
(contents &rest body
)
44 "Create a `python-mode' enabled file with CONTENTS.
45 BODY is code to be executed within the temp buffer. Point is
46 always located at the beginning of buffer."
47 (declare (indent 1) (debug t
))
48 ;; temp-file never actually used for anything?
49 `(let* ((temp-file (make-temp-file "python-tests" nil
".py"))
50 (buffer (find-file-noselect temp-file
)))
52 (with-current-buffer buffer
55 (goto-char (point-min))
57 (and buffer
(kill-buffer buffer
))
58 (delete-file temp-file
))))
60 (defun python-tests-look-at (string &optional num restore-point
)
61 "Move point at beginning of STRING in the current buffer.
62 Optional argument NUM defaults to 1 and is an integer indicating
63 how many occurrences must be found, when positive the search is
64 done forwards, otherwise backwards. When RESTORE-POINT is
65 non-nil the point is not moved but the position found is still
66 returned. When searching forward and point is already looking at
67 STRING, it is skipped so the next STRING occurrence is selected."
68 (let* ((num (or num
1))
69 (starting-point (point))
70 (string (regexp-quote string
))
71 (search-fn (if (> num
0) #'re-search-forward
#'re-search-backward
))
72 (deinc-fn (if (> num
0) #'1-
#'1+))
76 (while (not (= num
0))
79 ;; Moving forward and already looking at STRING, skip it.
80 (forward-char (length (match-string-no-properties 0))))
81 (and (not (funcall search-fn string nil t
))
84 ;; `re-search-forward' leaves point at the end of the
85 ;; occurrence, move back so point is at the beginning
87 (forward-char (- (length (match-string-no-properties 0)))))
89 num
(funcall deinc-fn num
)
90 found-point
(point))))
92 (and restore-point
(goto-char starting-point
)))))
94 (defun python-tests-self-insert (char-or-str)
95 "Call `self-insert-command' for chars in CHAR-OR-STR."
98 ((characterp char-or-str
)
100 ((stringp char-or-str
)
101 (string-to-list char-or-str
))
103 (cl-remove-if #'characterp char-or-str
))
105 (t (error "CHAR-OR-STR must be a char, string, or list of char")))))
108 (let ((last-command-event char
))
109 (call-interactively 'self-insert-command
)))
112 (defun python-tests-visible-string (&optional min max
)
113 "Return the buffer string excluding invisible overlays.
114 Argument MIN and MAX delimit the region to be returned and
115 default to `point-min' and `point-max' respectively."
116 (let* ((min (or min
(point-min)))
117 (max (or max
(point-max)))
118 (buffer (current-buffer))
119 (buffer-contents (buffer-substring-no-properties min max
))
121 (sort (overlays-in min max
)
123 (let ((overlay-end-a (overlay-end a
))
124 (overlay-end-b (overlay-end b
)))
125 (> overlay-end-a overlay-end-b
))))))
127 (insert buffer-contents
)
128 (dolist (overlay overlays
)
129 (if (overlay-get overlay
'invisible
)
130 (delete-region (overlay-start overlay
)
131 (overlay-end overlay
))))
132 (buffer-substring-no-properties (point-min) (point-max)))))
135 ;;; Tests for your tests, so you can test while you test.
137 (ert-deftest python-tests-look-at-1
()
138 "Test forward movement."
139 (python-tests-with-temp-buffer
140 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
141 sed do eiusmod tempor incididunt ut labore et dolore magna
143 (let ((expected (save-excursion
145 (re-search-forward "et" nil t
))
148 (should (= (python-tests-look-at "et" 3 t
) expected
))
149 ;; Even if NUM is bigger than found occurrences the point of last
150 ;; one should be returned.
151 (should (= (python-tests-look-at "et" 6 t
) expected
))
152 ;; If already looking at STRING, it should skip it.
153 (dotimes (i 2) (re-search-forward "et"))
155 (should (= (python-tests-look-at "et") expected
)))))
157 (ert-deftest python-tests-look-at-2
()
158 "Test backward movement."
159 (python-tests-with-temp-buffer
160 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
161 sed do eiusmod tempor incididunt ut labore et dolore magna
165 (re-search-forward "et" nil t
)
169 (re-search-forward "et" nil t
))
170 (should (= (python-tests-look-at "et" -
3 t
) expected
))
171 (should (= (python-tests-look-at "et" -
6 t
) expected
)))))
177 ;;; Python specialized rx
180 ;;; Font-lock and syntax
182 (ert-deftest python-syntax-after-python-backspace
()
183 ;; `python-indent-dedent-line-backspace' garbles syntax
184 :expected-result
:failed
185 (python-tests-with-temp-buffer
187 (goto-char (point-max))
188 (python-indent-dedent-line-backspace 1)
189 (should (string= (buffer-string) "\"\""))
190 (should (null (nth 3 (syntax-ppss))))))
195 ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
197 (ert-deftest python-indent-pep8-1
()
199 (python-tests-with-temp-buffer
200 "# Aligned with opening delimiter
201 foo = long_function_name(var_one, var_two,
204 (should (eq (car (python-indent-context)) :no-indent
))
205 (should (= (python-indent-calculate-indentation) 0))
206 (python-tests-look-at "foo = long_function_name(var_one, var_two,")
207 (should (eq (car (python-indent-context)) :after-comment
))
208 (should (= (python-indent-calculate-indentation) 0))
209 (python-tests-look-at "var_three, var_four)")
210 (should (eq (car (python-indent-context)) :inside-paren
))
211 (should (= (python-indent-calculate-indentation) 25))))
213 (ert-deftest python-indent-pep8-2
()
215 (python-tests-with-temp-buffer
216 "# More indentation included to distinguish this from the rest.
217 def long_function_name(
218 var_one, var_two, var_three,
222 (should (eq (car (python-indent-context)) :no-indent
))
223 (should (= (python-indent-calculate-indentation) 0))
224 (python-tests-look-at "def long_function_name(")
225 (should (eq (car (python-indent-context)) :after-comment
))
226 (should (= (python-indent-calculate-indentation) 0))
227 (python-tests-look-at "var_one, var_two, var_three,")
228 (should (eq (car (python-indent-context))
229 :inside-paren-newline-start-from-block
))
230 (should (= (python-indent-calculate-indentation) 8))
231 (python-tests-look-at "var_four):")
232 (should (eq (car (python-indent-context))
233 :inside-paren-newline-start-from-block
))
234 (should (= (python-indent-calculate-indentation) 8))
235 (python-tests-look-at "print (var_one)")
236 (should (eq (car (python-indent-context))
238 (should (= (python-indent-calculate-indentation) 4))))
240 (ert-deftest python-indent-pep8-3
()
242 (python-tests-with-temp-buffer
243 "# Extra indentation is not necessary.
244 foo = long_function_name(
248 (should (eq (car (python-indent-context)) :no-indent
))
249 (should (= (python-indent-calculate-indentation) 0))
250 (python-tests-look-at "foo = long_function_name(")
251 (should (eq (car (python-indent-context)) :after-comment
))
252 (should (= (python-indent-calculate-indentation) 0))
253 (python-tests-look-at "var_one, var_two,")
254 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
255 (should (= (python-indent-calculate-indentation) 4))
256 (python-tests-look-at "var_three, var_four)")
257 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
258 (should (= (python-indent-calculate-indentation) 4))))
260 (ert-deftest python-indent-base-case
()
261 "Check base case does not trigger errors."
262 (python-tests-with-temp-buffer
266 (goto-char (point-min))
267 (should (eq (car (python-indent-context)) :no-indent
))
268 (should (= (python-indent-calculate-indentation) 0))
270 (should (eq (car (python-indent-context)) :after-line
))
271 (should (= (python-indent-calculate-indentation) 0))
273 (should (eq (car (python-indent-context)) :after-line
))
274 (should (= (python-indent-calculate-indentation) 0))))
276 (ert-deftest python-indent-after-comment-1
()
277 "The most simple after-comment case that shouldn't fail."
278 (python-tests-with-temp-buffer
279 "# Contents will be modified to correct indentation
281 def _on_child_complete(self, child_future):
282 if self.in_terminal_state():
284 # We only complete when all our async children have entered a
285 # terminal state. At that point, if any child failed, we fail
286 # with the exception with which the first child failed.
288 (python-tests-look-at "# We only complete")
289 (should (eq (car (python-indent-context)) :after-block-end
))
290 (should (= (python-indent-calculate-indentation) 8))
291 (python-tests-look-at "# terminal state")
292 (should (eq (car (python-indent-context)) :after-comment
))
293 (should (= (python-indent-calculate-indentation) 8))
294 (python-tests-look-at "# with the exception")
295 (should (eq (car (python-indent-context)) :after-comment
))
296 ;; This one indents relative to previous block, even given the fact
297 ;; that it was under-indented.
298 (should (= (python-indent-calculate-indentation) 4))
299 (python-tests-look-at "# terminal state" -
1)
300 ;; It doesn't hurt to check again.
301 (should (eq (car (python-indent-context)) :after-comment
))
303 (should (= (current-indentation) 8))
304 (python-tests-look-at "# with the exception")
305 (should (eq (car (python-indent-context)) :after-comment
))
306 ;; Now everything should be lined up.
307 (should (= (python-indent-calculate-indentation) 8))))
309 (ert-deftest python-indent-after-comment-2
()
310 "Test after-comment in weird cases."
311 (python-tests-with-temp-buffer
312 "# Contents will be modified to correct indentation
316 # This comment is badly indented because the user forced so.
317 # At this line python.el wont dedent, user is always right.
319 comment_wins_over_ender = True
323 (python-tests-look-at "# I don't do much")
324 (should (eq (car (python-indent-context)) :after-block-start
))
325 (should (= (python-indent-calculate-indentation) 4))
326 (python-tests-look-at "return arg")
327 ;; Comment here just gets ignored, this line is not a comment so
328 ;; the rules won't apply here.
329 (should (eq (car (python-indent-context)) :after-block-start
))
330 (should (= (python-indent-calculate-indentation) 4))
331 (python-tests-look-at "# This comment is badly indented")
332 (should (eq (car (python-indent-context)) :after-block-end
))
333 ;; The return keyword do make indentation lose a level...
334 (should (= (python-indent-calculate-indentation) 0))
335 ;; ...but the current indentation was forced by the user.
336 (python-tests-look-at "# At this line python.el wont dedent")
337 (should (eq (car (python-indent-context)) :after-comment
))
338 (should (= (python-indent-calculate-indentation) 4))
339 ;; Should behave the same for blank lines: potentially a comment.
341 (should (eq (car (python-indent-context)) :after-comment
))
342 (should (= (python-indent-calculate-indentation) 4))
343 (python-tests-look-at "comment_wins_over_ender")
344 ;; The comment won over the ender because the user said so.
345 (should (eq (car (python-indent-context)) :after-comment
))
346 (should (= (python-indent-calculate-indentation) 4))
347 ;; The indentation calculated fine for the assignment, but the user
348 ;; choose to force it back to the first column. Next line should
350 (python-tests-look-at "# yeah, that.")
351 (should (eq (car (python-indent-context)) :after-line
))
352 (should (= (python-indent-calculate-indentation) 0))))
354 (ert-deftest python-indent-after-comment-3
()
355 "Test after-comment in buggy case."
356 (python-tests-with-temp-buffer
360 def something(self, arg):
367 def method(self, a, b):
370 (python-tests-look-at "@adecorator")
371 (should (eq (car (python-indent-context)) :after-comment
))
372 (should (= (python-indent-calculate-indentation) 4))))
374 (ert-deftest python-indent-inside-paren-1
()
375 "The most simple inside-paren case that shouldn't fail."
376 (python-tests-with-temp-buffer
394 (python-tests-look-at "data = {")
395 (should (eq (car (python-indent-context)) :after-line
))
396 (should (= (python-indent-calculate-indentation) 0))
397 (python-tests-look-at "'key':")
398 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
399 (should (= (python-indent-calculate-indentation) 4))
400 (python-tests-look-at "{")
401 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
402 (should (= (python-indent-calculate-indentation) 4))
403 (python-tests-look-at "'objlist': [")
404 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
405 (should (= (python-indent-calculate-indentation) 8))
406 (python-tests-look-at "{")
407 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
408 (should (= (python-indent-calculate-indentation) 12))
409 (python-tests-look-at "'pk': 1,")
410 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
411 (should (= (python-indent-calculate-indentation) 16))
412 (python-tests-look-at "'name': 'first',")
413 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
414 (should (= (python-indent-calculate-indentation) 16))
415 (python-tests-look-at "},")
416 (should (eq (car (python-indent-context))
417 :inside-paren-at-closing-nested-paren
))
418 (should (= (python-indent-calculate-indentation) 12))
419 (python-tests-look-at "{")
420 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
421 (should (= (python-indent-calculate-indentation) 12))
422 (python-tests-look-at "'pk': 2,")
423 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
424 (should (= (python-indent-calculate-indentation) 16))
425 (python-tests-look-at "'name': 'second',")
426 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
427 (should (= (python-indent-calculate-indentation) 16))
428 (python-tests-look-at "}")
429 (should (eq (car (python-indent-context))
430 :inside-paren-at-closing-nested-paren
))
431 (should (= (python-indent-calculate-indentation) 12))
432 (python-tests-look-at "]")
433 (should (eq (car (python-indent-context))
434 :inside-paren-at-closing-nested-paren
))
435 (should (= (python-indent-calculate-indentation) 8))
436 (python-tests-look-at "}")
437 (should (eq (car (python-indent-context))
438 :inside-paren-at-closing-nested-paren
))
439 (should (= (python-indent-calculate-indentation) 4))
440 (python-tests-look-at "}")
441 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren
))
442 (should (= (python-indent-calculate-indentation) 0))))
444 (ert-deftest python-indent-inside-paren-2
()
445 "Another more compact paren group style."
446 (python-tests-with-temp-buffer
457 (python-tests-look-at "data = {")
458 (should (eq (car (python-indent-context)) :after-line
))
459 (should (= (python-indent-calculate-indentation) 0))
460 (python-tests-look-at "'objlist': [")
461 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
462 (should (= (python-indent-calculate-indentation) 4))
463 (python-tests-look-at "{'pk': 1,")
464 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
465 (should (= (python-indent-calculate-indentation) 8))
466 (python-tests-look-at "'name': 'first'},")
467 (should (eq (car (python-indent-context)) :inside-paren
))
468 (should (= (python-indent-calculate-indentation) 9))
469 (python-tests-look-at "{'pk': 2,")
470 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
471 (should (= (python-indent-calculate-indentation) 8))
472 (python-tests-look-at "'name': 'second'}")
473 (should (eq (car (python-indent-context)) :inside-paren
))
474 (should (= (python-indent-calculate-indentation) 9))
475 (python-tests-look-at "]")
476 (should (eq (car (python-indent-context))
477 :inside-paren-at-closing-nested-paren
))
478 (should (= (python-indent-calculate-indentation) 4))
479 (python-tests-look-at "}}")
480 (should (eq (car (python-indent-context))
481 :inside-paren-at-closing-nested-paren
))
482 (should (= (python-indent-calculate-indentation) 0))
483 (python-tests-look-at "}")
484 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren
))
485 (should (= (python-indent-calculate-indentation) 0))))
487 (ert-deftest python-indent-inside-paren-3
()
488 "The simplest case possible."
489 (python-tests-with-temp-buffer
496 (python-tests-look-at "data = ('these',")
497 (should (eq (car (python-indent-context)) :after-line
))
498 (should (= (python-indent-calculate-indentation) 0))
500 (should (eq (car (python-indent-context)) :inside-paren
))
501 (should (= (python-indent-calculate-indentation) 8))
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 (ert-deftest python-indent-inside-paren-4
()
510 "Respect indentation of first column."
511 (python-tests-with-temp-buffer
513 data = [ [ 'these', 'are'],
516 (python-tests-look-at "data = [ [ 'these', 'are'],")
517 (should (eq (car (python-indent-context)) :after-line
))
518 (should (= (python-indent-calculate-indentation) 0))
520 (should (eq (car (python-indent-context)) :inside-paren
))
521 (should (= (python-indent-calculate-indentation) 9))))
523 (ert-deftest python-indent-inside-paren-5
()
524 "Test when :inside-paren initial parens are skipped in context start."
525 (python-tests-with-temp-buffer
527 while ((not some_condition) and
529 do_something_interesting(
532 (python-tests-look-at "while ((not some_condition) and")
533 (should (eq (car (python-indent-context)) :after-line
))
534 (should (= (python-indent-calculate-indentation) 0))
536 (should (eq (car (python-indent-context)) :inside-paren
))
537 (should (= (python-indent-calculate-indentation) 7))
539 (should (eq (car (python-indent-context)) :after-block-start
))
540 (should (= (python-indent-calculate-indentation) 4))
542 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
543 (should (= (python-indent-calculate-indentation) 8))))
545 (ert-deftest python-indent-inside-paren-6
()
546 "This should be aligned.."
547 (python-tests-with-temp-buffer
549 CHOICES = (('some', 'choice'),
550 ('another', 'choice'),
553 (python-tests-look-at "CHOICES = (('some', 'choice'),")
554 (should (eq (car (python-indent-context)) :after-line
))
555 (should (= (python-indent-calculate-indentation) 0))
557 (should (eq (car (python-indent-context)) :inside-paren
))
558 (should (= (python-indent-calculate-indentation) 11))
560 (should (eq (car (python-indent-context)) :inside-paren
))
561 (should (= (python-indent-calculate-indentation) 11))))
563 (ert-deftest python-indent-after-block-1
()
564 "The most simple after-block case that shouldn't fail."
565 (python-tests-with-temp-buffer
567 def foo(a, b, c=True):
569 (should (eq (car (python-indent-context)) :no-indent
))
570 (should (= (python-indent-calculate-indentation) 0))
571 (goto-char (point-max))
572 (should (eq (car (python-indent-context)) :after-block-start
))
573 (should (= (python-indent-calculate-indentation) 4))))
575 (ert-deftest python-indent-after-block-2
()
576 "A weird (malformed) multiline block statement."
577 (python-tests-with-temp-buffer
583 (goto-char (point-max))
584 (should (eq (car (python-indent-context)) :after-block-start
))
585 (should (= (python-indent-calculate-indentation) 4))))
587 (ert-deftest python-indent-after-block-3
()
588 "A weird (malformed) sample, usually found in python shells."
589 (python-tests-with-temp-buffer
598 (python-tests-look-at "pass")
599 (should (eq (car (python-indent-context)) :after-block-start
))
600 (should (= (python-indent-calculate-indentation) 4))
601 (python-tests-look-at "something")
603 (should (eq (car (python-indent-context)) :after-line
))
604 (should (= (python-indent-calculate-indentation) 0))))
606 (ert-deftest python-indent-after-backslash-1
()
607 "The most common case."
608 (python-tests-with-temp-buffer
610 from foo.bar.baz import something, something_1 \\\\
611 something_2 something_3, \\\\
612 something_4, something_5
614 (python-tests-look-at "from foo.bar.baz import something, something_1")
615 (should (eq (car (python-indent-context)) :after-line
))
616 (should (= (python-indent-calculate-indentation) 0))
617 (python-tests-look-at "something_2 something_3,")
618 (should (eq (car (python-indent-context)) :after-backslash-first-line
))
619 (should (= (python-indent-calculate-indentation) 4))
620 (python-tests-look-at "something_4, something_5")
621 (should (eq (car (python-indent-context)) :after-backslash
))
622 (should (= (python-indent-calculate-indentation) 4))
623 (goto-char (point-max))
624 (should (eq (car (python-indent-context)) :after-line
))
625 (should (= (python-indent-calculate-indentation) 0))))
627 (ert-deftest python-indent-after-backslash-2
()
628 "A pretty extreme complicated case."
629 (python-tests-with-temp-buffer
631 objects = Thing.objects.all() \\\\
641 (python-tests-look-at "objects = Thing.objects.all()")
642 (should (eq (car (python-indent-context)) :after-line
))
643 (should (= (python-indent-calculate-indentation) 0))
644 (python-tests-look-at ".filter(")
645 (should (eq (car (python-indent-context))
646 :after-backslash-dotted-continuation
))
647 (should (= (python-indent-calculate-indentation) 23))
648 (python-tests-look-at "type='toy',")
649 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
650 (should (= (python-indent-calculate-indentation) 27))
651 (python-tests-look-at "status='bought'")
652 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
653 (should (= (python-indent-calculate-indentation) 27))
654 (python-tests-look-at ") \\\\")
655 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren
))
656 (should (= (python-indent-calculate-indentation) 23))
657 (python-tests-look-at ".aggregate(")
658 (should (eq (car (python-indent-context))
659 :after-backslash-dotted-continuation
))
660 (should (= (python-indent-calculate-indentation) 23))
661 (python-tests-look-at "Sum('amount')")
662 (should (eq (car (python-indent-context)) :inside-paren-newline-start
))
663 (should (= (python-indent-calculate-indentation) 27))
664 (python-tests-look-at ") \\\\")
665 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren
))
666 (should (= (python-indent-calculate-indentation) 23))
667 (python-tests-look-at ".values_list()")
668 (should (eq (car (python-indent-context))
669 :after-backslash-dotted-continuation
))
670 (should (= (python-indent-calculate-indentation) 23))
672 (should (eq (car (python-indent-context)) :after-line
))
673 (should (= (python-indent-calculate-indentation) 0))))
675 (ert-deftest python-indent-after-backslash-3
()
676 "Backslash continuation from block start."
677 (python-tests-with-temp-buffer
679 with open('/path/to/some/file/you/want/to/read') as file_1, \\\\
680 open('/path/to/some/file/being/written', 'w') as file_2:
681 file_2.write(file_1.read())
683 (python-tests-look-at
684 "with open('/path/to/some/file/you/want/to/read') as file_1, \\\\")
685 (should (eq (car (python-indent-context)) :after-line
))
686 (should (= (python-indent-calculate-indentation) 0))
687 (python-tests-look-at
688 "open('/path/to/some/file/being/written', 'w') as file_2")
689 (should (eq (car (python-indent-context))
690 :after-backslash-block-continuation
))
691 (should (= (python-indent-calculate-indentation) 5))
692 (python-tests-look-at "file_2.write(file_1.read())")
693 (should (eq (car (python-indent-context)) :after-block-start
))
694 (should (= (python-indent-calculate-indentation) 4))))
696 (ert-deftest python-indent-after-backslash-4
()
697 "Backslash continuation from assignment."
698 (python-tests-with-temp-buffer
700 super_awful_assignment = some_calculation() and \\\\
701 another_calculation() and \\\\
702 some_final_calculation()
704 (python-tests-look-at
705 "super_awful_assignment = some_calculation() and \\\\")
706 (should (eq (car (python-indent-context)) :after-line
))
707 (should (= (python-indent-calculate-indentation) 0))
708 (python-tests-look-at "another_calculation() and \\\\")
709 (should (eq (car (python-indent-context))
710 :after-backslash-assignment-continuation
))
711 (should (= (python-indent-calculate-indentation) 25))
712 (python-tests-look-at "some_final_calculation()")
713 (should (eq (car (python-indent-context)) :after-backslash
))
714 (should (= (python-indent-calculate-indentation) 25))))
716 (ert-deftest python-indent-after-backslash-5
()
717 "Dotted continuation bizarre example."
718 (python-tests-with-temp-buffer
720 def delete_all_things():
725 (python-tests-look-at "Thing \\\\")
726 (should (eq (car (python-indent-context)) :after-block-start
))
727 (should (= (python-indent-calculate-indentation) 4))
728 (python-tests-look-at ".objects.all() \\\\")
729 (should (eq (car (python-indent-context)) :after-backslash-first-line
))
730 (should (= (python-indent-calculate-indentation) 8))
731 (python-tests-look-at ".delete()")
732 (should (eq (car (python-indent-context))
733 :after-backslash-dotted-continuation
))
734 (should (= (python-indent-calculate-indentation) 16))))
736 (ert-deftest python-indent-block-enders-1
()
737 "Test de-indentation for pass keyword."
738 (python-tests-with-temp-buffer
751 (python-tests-look-at "3)")
753 (should (= (python-indent-calculate-indentation) 8))
754 (python-tests-look-at "pass")
756 (should (eq (car (python-indent-context)) :after-block-end
))
757 (should (= (python-indent-calculate-indentation) 8))))
759 (ert-deftest python-indent-block-enders-2
()
760 "Test de-indentation for return keyword."
761 (python-tests-with-temp-buffer
764 '''raise lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
766 eiusmod tempor incididunt ut labore et dolore magna aliqua.
769 \"return (1, 2, 3).\"
775 (python-tests-look-at "def")
776 (should (= (python-indent-calculate-indentation) 4))
777 (python-tests-look-at "if")
778 (should (= (python-indent-calculate-indentation) 8))
779 (python-tests-look-at "return")
780 (should (= (python-indent-calculate-indentation) 12))
781 (goto-char (point-max))
782 (should (eq (car (python-indent-context)) :after-block-end
))
783 (should (= (python-indent-calculate-indentation) 8))))
785 (ert-deftest python-indent-block-enders-3
()
786 "Test de-indentation for continue keyword."
787 (python-tests-with-temp-buffer
793 (python-tests-look-at "if")
794 (should (= (python-indent-calculate-indentation) 4))
795 (python-tests-look-at "continue")
796 (should (= (python-indent-calculate-indentation) 8))
798 (should (eq (car (python-indent-context)) :after-block-end
))
799 (should (= (python-indent-calculate-indentation) 4))))
801 (ert-deftest python-indent-block-enders-4
()
802 "Test de-indentation for break keyword."
803 (python-tests-with-temp-buffer
809 (python-tests-look-at "if")
810 (should (= (python-indent-calculate-indentation) 4))
811 (python-tests-look-at "break")
812 (should (= (python-indent-calculate-indentation) 8))
814 (should (eq (car (python-indent-context)) :after-block-end
))
815 (should (= (python-indent-calculate-indentation) 4))))
817 (ert-deftest python-indent-block-enders-5
()
818 "Test de-indentation for raise keyword."
819 (python-tests-with-temp-buffer
823 raise ValueError('Element cannot be None')
825 (python-tests-look-at "if")
826 (should (= (python-indent-calculate-indentation) 4))
827 (python-tests-look-at "raise")
828 (should (= (python-indent-calculate-indentation) 8))
830 (should (eq (car (python-indent-context)) :after-block-end
))
831 (should (= (python-indent-calculate-indentation) 4))))
833 (ert-deftest python-indent-dedenters-1
()
834 "Test de-indentation for the elif keyword."
835 (python-tests-with-temp-buffer
844 (python-tests-look-at "elif\n")
845 (should (eq (car (python-indent-context)) :at-dedenter-block-start
))
846 (should (= (python-indent-calculate-indentation) 0))
847 (should (= (python-indent-calculate-indentation t
) 0))))
849 (ert-deftest python-indent-dedenters-2
()
850 "Test de-indentation for the else keyword."
851 (python-tests-with-temp-buffer
857 msg = 'Error saving to disk'
859 logger.exception(msg)
862 logger.exception('Unhandled exception')
867 (python-tests-look-at "else\n")
868 (should (eq (car (python-indent-context)) :at-dedenter-block-start
))
869 (should (= (python-indent-calculate-indentation) 8))
870 (python-indent-line t
)
871 (should (= (python-indent-calculate-indentation t
) 4))
872 (python-indent-line t
)
873 (should (= (python-indent-calculate-indentation t
) 0))
874 (python-indent-line t
)
875 (should (= (python-indent-calculate-indentation t
) 8))))
877 (ert-deftest python-indent-dedenters-3
()
878 "Test de-indentation for the except keyword."
879 (python-tests-with-temp-buffer
886 (python-tests-look-at "except\n")
887 (should (eq (car (python-indent-context)) :at-dedenter-block-start
))
888 (should (= (python-indent-calculate-indentation) 4))
889 (python-indent-line t
)
890 (should (= (python-indent-calculate-indentation t
) 4))))
892 (ert-deftest python-indent-dedenters-4
()
893 "Test de-indentation for the finally keyword."
894 (python-tests-with-temp-buffer
901 (python-tests-look-at "finally\n")
902 (should (eq (car (python-indent-context)) :at-dedenter-block-start
))
903 (should (= (python-indent-calculate-indentation) 4))
904 (python-indent-line t
)
905 (should (= (python-indent-calculate-indentation) 4))))
907 (ert-deftest python-indent-dedenters-5
()
908 "Test invalid levels are skipped in a complex example."
909 (python-tests-with-temp-buffer
915 msg = 'Error saving to disk'
917 logger.exception(msg)
923 (python-tests-look-at "else\n")
924 (should (eq (car (python-indent-context)) :at-dedenter-block-start
))
925 (should (= (python-indent-calculate-indentation) 8))
926 (should (= (python-indent-calculate-indentation t
) 0))
927 (python-indent-line t
)
928 (should (= (python-indent-calculate-indentation t
) 8))))
930 (ert-deftest python-indent-dedenters-6
()
931 "Test indentation is zero when no opening block for dedenter."
932 (python-tests-with-temp-buffer
939 (python-tests-look-at "else\n")
940 (should (eq (car (python-indent-context)) :at-dedenter-block-start
))
941 (should (= (python-indent-calculate-indentation) 0))
942 (should (= (python-indent-calculate-indentation t
) 0))))
944 (ert-deftest python-indent-dedenters-7
()
945 "Test indentation case from Bug#15163."
946 (python-tests-with-temp-buffer
955 (python-tests-look-at "else:" 2)
956 (should (eq (car (python-indent-context)) :at-dedenter-block-start
))
957 (should (= (python-indent-calculate-indentation) 0))
958 (should (= (python-indent-calculate-indentation t
) 0))))
960 (ert-deftest python-indent-dedenters-8
()
961 "Test indentation for Bug#18432."
962 (python-tests-with-temp-buffer
970 (python-tests-look-at "elif (a == 3 or")
971 (should (eq (car (python-indent-context)) :at-dedenter-block-start
))
972 (should (= (python-indent-calculate-indentation) 0))
973 (should (= (python-indent-calculate-indentation t
) 0))
974 (python-tests-look-at "a == 4):\n")
975 (should (eq (car (python-indent-context)) :inside-paren
))
976 (should (= (python-indent-calculate-indentation) 6))
978 (should (= (python-indent-calculate-indentation t
) 4))
979 (python-indent-line t
)
980 (should (= (python-indent-calculate-indentation t
) 0))
981 (python-indent-line t
)
982 (should (= (python-indent-calculate-indentation t
) 6))))
984 (ert-deftest python-indent-inside-string-1
()
985 "Test indentation for strings."
986 (python-tests-with-temp-buffer
994 (python-tests-look-at "multiline = '''")
995 (should (eq (car (python-indent-context)) :after-line
))
996 (should (= (python-indent-calculate-indentation) 0))
997 (python-tests-look-at "bunch")
998 (should (eq (car (python-indent-context)) :inside-string
))
999 (should (= (python-indent-calculate-indentation) 0))
1000 (python-tests-look-at "of")
1001 (should (eq (car (python-indent-context)) :inside-string
))
1002 (should (= (python-indent-calculate-indentation) 0))
1003 (python-tests-look-at "lines")
1004 (should (eq (car (python-indent-context)) :inside-string
))
1005 (should (= (python-indent-calculate-indentation) 0))
1006 (python-tests-look-at "'''")
1007 (should (eq (car (python-indent-context)) :inside-string
))
1008 (should (= (python-indent-calculate-indentation) 0))))
1010 (ert-deftest python-indent-inside-string-2
()
1011 "Test indentation for docstrings."
1012 (python-tests-with-temp-buffer
1014 def fn(a, b, c=True):
1021 (python-tests-look-at "'''docstring")
1022 (should (eq (car (python-indent-context)) :after-block-start
))
1023 (should (= (python-indent-calculate-indentation) 4))
1024 (python-tests-look-at "bunch")
1025 (should (eq (car (python-indent-context)) :inside-docstring
))
1026 (should (= (python-indent-calculate-indentation) 4))
1027 (python-tests-look-at "of")
1028 (should (eq (car (python-indent-context)) :inside-docstring
))
1029 ;; Any indentation deeper than the base-indent must remain unmodified.
1030 (should (= (python-indent-calculate-indentation) 8))
1031 (python-tests-look-at "lines")
1032 (should (eq (car (python-indent-context)) :inside-docstring
))
1033 (should (= (python-indent-calculate-indentation) 4))
1034 (python-tests-look-at "'''")
1035 (should (eq (car (python-indent-context)) :inside-docstring
))
1036 (should (= (python-indent-calculate-indentation) 4))))
1038 (ert-deftest python-indent-inside-string-3
()
1039 "Test indentation for nested strings."
1040 (python-tests-with-temp-buffer
1042 def fn(a, b, c=True):
1049 (python-tests-look-at "some_var = '''")
1050 (should (eq (car (python-indent-context)) :after-block-start
))
1051 (should (= (python-indent-calculate-indentation) 4))
1052 (python-tests-look-at "bunch")
1053 (should (eq (car (python-indent-context)) :inside-string
))
1054 (should (= (python-indent-calculate-indentation) 4))
1055 (python-tests-look-at "of")
1056 (should (eq (car (python-indent-context)) :inside-string
))
1057 (should (= (python-indent-calculate-indentation) 4))
1058 (python-tests-look-at "lines")
1059 (should (eq (car (python-indent-context)) :inside-string
))
1060 (should (= (python-indent-calculate-indentation) 4))
1061 (python-tests-look-at "'''")
1062 (should (eq (car (python-indent-context)) :inside-string
))
1063 (should (= (python-indent-calculate-indentation) 4))))
1065 (ert-deftest python-indent-electric-colon-1
()
1066 "Test indentation case from Bug#18228."
1067 (python-tests-with-temp-buffer
1074 (python-tests-look-at "def b()")
1075 (goto-char (line-end-position))
1076 (python-tests-self-insert ":")
1077 (should (= (current-indentation) 0))))
1079 (ert-deftest python-indent-electric-colon-2
()
1080 "Test indentation case for dedenter."
1081 (python-tests-with-temp-buffer
1087 (python-tests-look-at "else")
1088 (goto-char (line-end-position))
1089 (python-tests-self-insert ":")
1090 (should (= (current-indentation) 0))))
1092 (ert-deftest python-indent-electric-colon-3
()
1093 "Test indentation case for multi-line dedenter."
1094 (python-tests-with-temp-buffer
1102 (python-tests-look-at "that)")
1103 (goto-char (line-end-position))
1104 (python-tests-self-insert ":")
1105 (python-tests-look-at "elif" -
1)
1106 (should (= (current-indentation) 0))
1107 (python-tests-look-at "and")
1108 (should (= (current-indentation) 6))
1109 (python-tests-look-at "that)")
1110 (should (= (current-indentation) 6))))
1112 (ert-deftest python-indent-region-1
()
1113 "Test indentation case from Bug#18843."
1121 (python-tests-with-temp-buffer
1123 (python-indent-region (point-min) (point-max))
1124 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1127 (ert-deftest python-indent-region-2
()
1128 "Test region indentation on comments."
1138 (python-tests-with-temp-buffer
1140 (python-indent-region (point-min) (point-max))
1141 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1144 (ert-deftest python-indent-region-3
()
1145 "Test region indentation on comments."
1162 (python-tests-with-temp-buffer
1164 (python-indent-region (point-min) (point-max))
1165 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1168 (ert-deftest python-indent-region-4
()
1169 "Test region indentation block starts, dedenters and enders."
1186 (python-tests-with-temp-buffer
1188 (python-indent-region (point-min) (point-max))
1189 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1192 (ert-deftest python-indent-region-5
()
1193 "Test region indentation for docstrings."
1203 this is an arbitrarily
1217 this is an arbitrarily
1222 (python-tests-with-temp-buffer
1224 (python-indent-region (point-min) (point-max))
1225 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1231 (ert-deftest python-nav-beginning-of-defun-1
()
1232 (python-tests-with-temp-buffer
1234 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1235 '''print decorated function call data to stdout.
1239 @decoratorFunctionWithArguments('arg1', 'arg2')
1240 def func(a, b, c=True):
1245 print 'Inside wwrap()'
1246 def wrapped_f(*args):
1247 print 'Inside wrapped_f()'
1248 print 'Decorator arguments:', arg1, arg2, arg3
1250 print 'After f(*args)'
1254 (python-tests-look-at "return wrap")
1255 (should (= (save-excursion
1256 (python-nav-beginning-of-defun)
1259 (python-tests-look-at "def wrapped_f(*args):" -
1)
1262 (python-tests-look-at "def wrapped_f(*args):" -
1)
1263 (should (= (save-excursion
1264 (python-nav-beginning-of-defun)
1267 (python-tests-look-at "def wwrap(f):" -
1)
1270 (python-tests-look-at "def wwrap(f):" -
1)
1271 (should (= (save-excursion
1272 (python-nav-beginning-of-defun)
1275 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)
1279 (ert-deftest python-nav-beginning-of-defun-2
()
1280 (python-tests-with-temp-buffer
1296 ;; Nested defuns, are handled with care.
1297 (python-tests-look-at "def c(self):")
1298 (should (= (save-excursion
1299 (python-nav-beginning-of-defun)
1302 (python-tests-look-at "def m(self):" -
1)
1305 ;; Defuns on same levels should be respected.
1306 (python-tests-look-at "def a():" -
1)
1307 (should (= (save-excursion
1308 (python-nav-beginning-of-defun)
1311 (python-tests-look-at "def b():" -
1)
1314 ;; Jump to a top level defun.
1315 (python-tests-look-at "def b():" -
1)
1316 (should (= (save-excursion
1317 (python-nav-beginning-of-defun)
1320 (python-tests-look-at "def m(self):" -
1)
1323 ;; Jump to a top level defun again.
1324 (python-tests-look-at "def m(self):" -
1)
1325 (should (= (save-excursion
1326 (python-nav-beginning-of-defun)
1329 (python-tests-look-at "class C(object):" -
1)
1333 (ert-deftest python-nav-end-of-defun-1
()
1334 (python-tests-with-temp-buffer
1350 (should (= (save-excursion
1351 (python-tests-look-at "class C(object):")
1352 (python-nav-end-of-defun)
1356 (should (= (save-excursion
1357 (python-tests-look-at "def m(self):")
1358 (python-nav-end-of-defun)
1361 (python-tests-look-at "def c(self):")
1364 (should (= (save-excursion
1365 (python-tests-look-at "def b():")
1366 (python-nav-end-of-defun)
1369 (python-tests-look-at "def b():")
1372 (should (= (save-excursion
1373 (python-tests-look-at "def c(self):")
1374 (python-nav-end-of-defun)
1379 (ert-deftest python-nav-end-of-defun-2
()
1380 (python-tests-with-temp-buffer
1382 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1383 '''print decorated function call data to stdout.
1387 @decoratorFunctionWithArguments('arg1', 'arg2')
1388 def func(a, b, c=True):
1393 print 'Inside wwrap()'
1394 def wrapped_f(*args):
1395 print 'Inside wrapped_f()'
1396 print 'Decorator arguments:', arg1, arg2, arg3
1398 print 'After f(*args)'
1402 (should (= (save-excursion
1403 (python-tests-look-at "def decoratorFunctionWithArguments")
1404 (python-nav-end-of-defun)
1408 (should (= (save-excursion
1409 (python-tests-look-at "@decoratorFunctionWithArguments")
1410 (python-nav-end-of-defun)
1414 (should (= (save-excursion
1415 (python-tests-look-at "def wwrap(f):")
1416 (python-nav-end-of-defun)
1419 (python-tests-look-at "return wwrap")
1420 (line-beginning-position))))
1421 (should (= (save-excursion
1422 (python-tests-look-at "def wrapped_f(*args):")
1423 (python-nav-end-of-defun)
1426 (python-tests-look-at "return wrapped_f")
1427 (line-beginning-position))))
1428 (should (= (save-excursion
1429 (python-tests-look-at "f(*args)")
1430 (python-nav-end-of-defun)
1433 (python-tests-look-at "return wrapped_f")
1434 (line-beginning-position))))))
1436 (ert-deftest python-nav-backward-defun-1
()
1437 (python-tests-with-temp-buffer
1439 class A(object): # A
1447 class B(object): # B
1449 class C(object): # C
1463 (goto-char (point-max))
1464 (should (= (save-excursion (python-nav-backward-defun))
1465 (python-tests-look-at " def c(self): # c" -
1)))
1466 (should (= (save-excursion (python-nav-backward-defun))
1467 (python-tests-look-at " def d(self): # d" -
1)))
1468 (should (= (save-excursion (python-nav-backward-defun))
1469 (python-tests-look-at " class C(object): # C" -
1)))
1470 (should (= (save-excursion (python-nav-backward-defun))
1471 (python-tests-look-at " class B(object): # B" -
1)))
1472 (should (= (save-excursion (python-nav-backward-defun))
1473 (python-tests-look-at " def b(self): # b" -
1)))
1474 (should (= (save-excursion (python-nav-backward-defun))
1475 (python-tests-look-at " def a(self): # a" -
1)))
1476 (should (= (save-excursion (python-nav-backward-defun))
1477 (python-tests-look-at "class A(object): # A" -
1)))
1478 (should (not (python-nav-backward-defun)))))
1480 (ert-deftest python-nav-backward-defun-2
()
1481 (python-tests-with-temp-buffer
1483 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1484 '''print decorated function call data to stdout.
1488 @decoratorFunctionWithArguments('arg1', 'arg2')
1489 def func(a, b, c=True):
1494 print 'Inside wwrap()'
1495 def wrapped_f(*args):
1496 print 'Inside wrapped_f()'
1497 print 'Decorator arguments:', arg1, arg2, arg3
1499 print 'After f(*args)'
1503 (goto-char (point-max))
1504 (should (= (save-excursion (python-nav-backward-defun))
1505 (python-tests-look-at " def wrapped_f(*args):" -
1)))
1506 (should (= (save-excursion (python-nav-backward-defun))
1507 (python-tests-look-at " def wwrap(f):" -
1)))
1508 (should (= (save-excursion (python-nav-backward-defun))
1509 (python-tests-look-at "def decoratorFunctionWithArguments(arg1, arg2, arg3):" -
1)))
1510 (should (not (python-nav-backward-defun)))))
1512 (ert-deftest python-nav-backward-defun-3
()
1513 (python-tests-with-temp-buffer
1529 (goto-char (point-min))
1530 (let ((point (python-tests-look-at "class A(object):")))
1531 (should (not (python-nav-backward-defun)))
1532 (should (= point
(point))))))
1534 (ert-deftest python-nav-forward-defun-1
()
1535 (python-tests-with-temp-buffer
1537 class A(object): # A
1545 class B(object): # B
1547 class C(object): # C
1561 (goto-char (point-min))
1562 (should (= (save-excursion (python-nav-forward-defun))
1563 (python-tests-look-at "(object): # A")))
1564 (should (= (save-excursion (python-nav-forward-defun))
1565 (python-tests-look-at "(self): # a")))
1566 (should (= (save-excursion (python-nav-forward-defun))
1567 (python-tests-look-at "(self): # b")))
1568 (should (= (save-excursion (python-nav-forward-defun))
1569 (python-tests-look-at "(object): # B")))
1570 (should (= (save-excursion (python-nav-forward-defun))
1571 (python-tests-look-at "(object): # C")))
1572 (should (= (save-excursion (python-nav-forward-defun))
1573 (python-tests-look-at "(self): # d")))
1574 (should (= (save-excursion (python-nav-forward-defun))
1575 (python-tests-look-at "(self): # c")))
1576 (should (not (python-nav-forward-defun)))))
1578 (ert-deftest python-nav-forward-defun-2
()
1579 (python-tests-with-temp-buffer
1581 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1582 '''print decorated function call data to stdout.
1586 @decoratorFunctionWithArguments('arg1', 'arg2')
1587 def func(a, b, c=True):
1592 print 'Inside wwrap()'
1593 def wrapped_f(*args):
1594 print 'Inside wrapped_f()'
1595 print 'Decorator arguments:', arg1, arg2, arg3
1597 print 'After f(*args)'
1601 (goto-char (point-min))
1602 (should (= (save-excursion (python-nav-forward-defun))
1603 (python-tests-look-at "(arg1, arg2, arg3):")))
1604 (should (= (save-excursion (python-nav-forward-defun))
1605 (python-tests-look-at "(f):")))
1606 (should (= (save-excursion (python-nav-forward-defun))
1607 (python-tests-look-at "(*args):")))
1608 (should (not (python-nav-forward-defun)))))
1610 (ert-deftest python-nav-forward-defun-3
()
1611 (python-tests-with-temp-buffer
1627 (goto-char (point-min))
1628 (let ((point (python-tests-look-at "(object):")))
1629 (should (not (python-nav-forward-defun)))
1630 (should (= point
(point))))))
1632 (ert-deftest python-nav-beginning-of-statement-1
()
1633 (python-tests-with-temp-buffer
1643 v3 = ('this is a string'
1648 # this is a comment, yo
1649 'continue previous line')
1655 (python-tests-look-at "v2 =")
1656 (python-util-forward-comment -
1)
1657 (should (= (save-excursion
1658 (python-nav-beginning-of-statement)
1660 (python-tests-look-at "v1 =" -
1 t
)))
1661 (python-tests-look-at "v3 =")
1662 (python-util-forward-comment -
1)
1663 (should (= (save-excursion
1664 (python-nav-beginning-of-statement)
1666 (python-tests-look-at "v2 =" -
1 t
)))
1667 (python-tests-look-at "v4 =")
1668 (python-util-forward-comment -
1)
1669 (should (= (save-excursion
1670 (python-nav-beginning-of-statement)
1672 (python-tests-look-at "v3 =" -
1 t
)))
1673 (goto-char (point-max))
1674 (python-util-forward-comment -
1)
1675 (should (= (save-excursion
1676 (python-nav-beginning-of-statement)
1678 (python-tests-look-at "v4 =" -
1 t
)))))
1680 (ert-deftest python-nav-end-of-statement-1
()
1681 (python-tests-with-temp-buffer
1691 v3 = ('this is a string'
1696 # this is a comment, yo
1697 'continue previous line')
1703 (python-tests-look-at "v1 =")
1704 (should (= (save-excursion
1705 (python-nav-end-of-statement)
1708 (python-tests-look-at "789")
1709 (line-end-position))))
1710 (python-tests-look-at "v2 =")
1711 (should (= (save-excursion
1712 (python-nav-end-of-statement)
1715 (python-tests-look-at "value4)")
1716 (line-end-position))))
1717 (python-tests-look-at "v3 =")
1718 (should (= (save-excursion
1719 (python-nav-end-of-statement)
1722 (python-tests-look-at
1723 "'continue previous line')")
1724 (line-end-position))))
1725 (python-tests-look-at "v4 =")
1726 (should (= (save-excursion
1727 (python-nav-end-of-statement)
1730 (goto-char (point-max))
1731 (python-util-forward-comment -
1)
1734 (ert-deftest python-nav-forward-statement-1
()
1735 (python-tests-with-temp-buffer
1745 v3 = ('this is a string'
1750 # this is a comment, yo
1751 'continue previous line')
1757 (python-tests-look-at "v1 =")
1758 (should (= (save-excursion
1759 (python-nav-forward-statement)
1761 (python-tests-look-at "v2 =")))
1762 (should (= (save-excursion
1763 (python-nav-forward-statement)
1765 (python-tests-look-at "v3 =")))
1766 (should (= (save-excursion
1767 (python-nav-forward-statement)
1769 (python-tests-look-at "v4 =")))
1770 (should (= (save-excursion
1771 (python-nav-forward-statement)
1775 (ert-deftest python-nav-backward-statement-1
()
1776 (python-tests-with-temp-buffer
1786 v3 = ('this is a string'
1791 # this is a comment, yo
1792 'continue previous line')
1798 (goto-char (point-max))
1799 (should (= (save-excursion
1800 (python-nav-backward-statement)
1802 (python-tests-look-at "v4 =" -
1)))
1803 (should (= (save-excursion
1804 (python-nav-backward-statement)
1806 (python-tests-look-at "v3 =" -
1)))
1807 (should (= (save-excursion
1808 (python-nav-backward-statement)
1810 (python-tests-look-at "v2 =" -
1)))
1811 (should (= (save-excursion
1812 (python-nav-backward-statement)
1814 (python-tests-look-at "v1 =" -
1)))))
1816 (ert-deftest python-nav-backward-statement-2
()
1817 :expected-result
:failed
1818 (python-tests-with-temp-buffer
1829 ;; FIXME: For some reason `python-nav-backward-statement' is moving
1830 ;; back two sentences when starting from 'value4)'.
1831 (goto-char (point-max))
1832 (python-util-forward-comment -
1)
1833 (should (= (save-excursion
1834 (python-nav-backward-statement)
1836 (python-tests-look-at "v2 =" -
1 t
)))))
1838 (ert-deftest python-nav-beginning-of-block-1
()
1839 (python-tests-with-temp-buffer
1841 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1842 '''print decorated function call data to stdout.
1846 @decoratorFunctionWithArguments('arg1', 'arg2')
1847 def func(a, b, c=True):
1852 print 'Inside wwrap()'
1853 def wrapped_f(*args):
1854 print 'Inside wrapped_f()'
1855 print 'Decorator arguments:', arg1, arg2, arg3
1857 print 'After f(*args)'
1861 (python-tests-look-at "return wwrap")
1862 (should (= (save-excursion
1863 (python-nav-beginning-of-block)
1865 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)))
1866 (python-tests-look-at "print 'Inside wwrap()'")
1867 (should (= (save-excursion
1868 (python-nav-beginning-of-block)
1870 (python-tests-look-at "def wwrap(f):" -
1)))
1871 (python-tests-look-at "print 'After f(*args)'")
1873 (should (= (save-excursion
1874 (python-nav-beginning-of-block)
1876 (python-tests-look-at "def wrapped_f(*args):" -
1)))
1877 (python-tests-look-at "return wrapped_f")
1878 (should (= (save-excursion
1879 (python-nav-beginning-of-block)
1881 (python-tests-look-at "def wwrap(f):" -
1)))))
1883 (ert-deftest python-nav-end-of-block-1
()
1884 (python-tests-with-temp-buffer
1886 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1887 '''print decorated function call data to stdout.
1891 @decoratorFunctionWithArguments('arg1', 'arg2')
1892 def func(a, b, c=True):
1897 print 'Inside wwrap()'
1898 def wrapped_f(*args):
1899 print 'Inside wrapped_f()'
1900 print 'Decorator arguments:', arg1, arg2, arg3
1902 print 'After f(*args)'
1906 (python-tests-look-at "def decoratorFunctionWithArguments")
1907 (should (= (save-excursion
1908 (python-nav-end-of-block)
1911 (goto-char (point-max))
1912 (python-util-forward-comment -
1)
1914 (python-tests-look-at "def wwrap(f):")
1915 (should (= (save-excursion
1916 (python-nav-end-of-block)
1919 (python-tests-look-at "return wrapped_f")
1920 (line-end-position))))
1922 (should (= (save-excursion
1923 (python-nav-end-of-block)
1926 (python-tests-look-at "return wrapped_f")
1927 (line-end-position))))
1928 (python-tests-look-at "f(*args)")
1929 (should (= (save-excursion
1930 (python-nav-end-of-block)
1933 (python-tests-look-at "print 'After f(*args)'")
1934 (line-end-position))))))
1936 (ert-deftest python-nav-forward-block-1
()
1937 "This also accounts as a test for `python-nav-backward-block'."
1938 (python-tests-with-temp-buffer
1940 if request.user.is_authenticated():
1944 profile = request.user.get_profile()
1945 except Profile.DoesNotExist:
1946 profile = Profile.objects.create(user=request.user)
1949 profile.recalculate_stats()
1951 profile.clear_stats()
1956 (should (= (save-excursion (python-nav-forward-block))
1957 (python-tests-look-at "if request.user.is_authenticated():")))
1958 (should (= (save-excursion (python-nav-forward-block))
1959 (python-tests-look-at "try:")))
1960 (should (= (save-excursion (python-nav-forward-block))
1961 (python-tests-look-at "except Profile.DoesNotExist:")))
1962 (should (= (save-excursion (python-nav-forward-block))
1963 (python-tests-look-at "else:")))
1964 (should (= (save-excursion (python-nav-forward-block))
1965 (python-tests-look-at "if profile.stats:")))
1966 (should (= (save-excursion (python-nav-forward-block))
1967 (python-tests-look-at "else:")))
1968 (should (= (save-excursion (python-nav-forward-block))
1969 (python-tests-look-at "finally:")))
1970 ;; When point is at the last block, leave it there and return nil
1971 (should (not (save-excursion (python-nav-forward-block))))
1972 ;; Move backwards, and even if the number of moves is less than the
1973 ;; provided argument return the point.
1974 (should (= (save-excursion (python-nav-forward-block -
10))
1975 (python-tests-look-at
1976 "if request.user.is_authenticated():" -
1)))))
1978 (ert-deftest python-nav-forward-sexp-1
()
1979 (python-tests-with-temp-buffer
1985 (python-tests-look-at "a()")
1986 (python-nav-forward-sexp)
1987 (should (looking-at "$"))
1988 (should (save-excursion
1990 (looking-at "a()")))
1991 (python-nav-forward-sexp)
1992 (should (looking-at "$"))
1993 (should (save-excursion
1995 (looking-at "b()")))
1996 (python-nav-forward-sexp)
1997 (should (looking-at "$"))
1998 (should (save-excursion
2000 (looking-at "c()")))
2001 ;; The default behavior when next to a paren should do what lisp
2002 ;; does and, otherwise `blink-matching-open' breaks.
2003 (python-nav-forward-sexp -
1)
2004 (should (looking-at "()"))
2005 (should (save-excursion
2007 (looking-at "c()")))
2009 ;; Skipping parens should jump to `bolp'
2010 (python-nav-forward-sexp -
1 nil t
)
2011 (should (looking-at "c()"))
2015 (python-nav-forward-sexp -
1)
2016 (should (looking-at "()"))
2017 (python-nav-forward-sexp -
1)
2018 (should (looking-at "b()"))
2020 (python-nav-forward-sexp -
1 nil t
)
2021 (should (looking-at "b()"))
2025 (python-nav-forward-sexp -
1)
2026 (should (looking-at "()"))
2027 (python-nav-forward-sexp -
1)
2028 (should (looking-at "a()"))
2030 (python-nav-forward-sexp -
1 nil t
)
2031 (should (looking-at "a()"))))
2033 (ert-deftest python-nav-forward-sexp-2
()
2034 (python-tests-with-temp-buffer
2043 (python-tests-look-at "aa =")
2044 (python-nav-forward-sexp)
2045 (should (looking-at " = bbb"))
2046 (python-nav-forward-sexp)
2047 (should (looking-at "$"))
2048 (should (save-excursion
2049 (back-to-indentation)
2050 (looking-at "aaa = bbb")))
2051 (python-nav-forward-sexp)
2052 (should (looking-at "$"))
2053 (should (save-excursion
2054 (back-to-indentation)
2055 (looking-at "ccc = ddd")))
2056 (python-nav-forward-sexp)
2057 (should (looking-at "$"))
2058 (should (save-excursion
2059 (back-to-indentation)
2060 (looking-at "eee = fff")))
2061 (python-nav-forward-sexp)
2062 (should (looking-at "$"))
2063 (should (save-excursion
2064 (back-to-indentation)
2065 (looking-at "return ggg")))
2066 (python-nav-forward-sexp -
1)
2067 (should (looking-at "def func():"))))
2069 (ert-deftest python-nav-forward-sexp-3
()
2070 (python-tests-with-temp-buffer
2072 from some_module import some_sub_module
2073 from another_module import another_sub_module
2075 def another_statement():
2078 (python-tests-look-at "some_module")
2079 (python-nav-forward-sexp)
2080 (should (looking-at " import"))
2081 (python-nav-forward-sexp)
2082 (should (looking-at " some_sub_module"))
2083 (python-nav-forward-sexp)
2084 (should (looking-at "$"))
2087 (back-to-indentation)
2089 "from some_module import some_sub_module")))
2090 (python-nav-forward-sexp)
2091 (should (looking-at "$"))
2094 (back-to-indentation)
2096 "from another_module import another_sub_module")))
2097 (python-nav-forward-sexp)
2098 (should (looking-at "$"))
2101 (back-to-indentation)
2104 (python-nav-forward-sexp -
1)
2105 (should (looking-at "def another_statement():"))
2106 (python-nav-forward-sexp -
1)
2107 (should (looking-at "from another_module import another_sub_module"))
2108 (python-nav-forward-sexp -
1)
2109 (should (looking-at "from some_module import some_sub_module"))))
2111 (ert-deftest python-nav-forward-sexp-safe-1
()
2112 (python-tests-with-temp-buffer
2114 profile = Profile.objects.create(user=request.user)
2117 (python-tests-look-at "profile =")
2118 (python-nav-forward-sexp-safe 1)
2119 (should (looking-at "$"))
2120 (beginning-of-line 1)
2121 (python-tests-look-at "user=request.user")
2122 (python-nav-forward-sexp-safe -
1)
2123 (should (looking-at "(user=request.user)"))
2124 (python-nav-forward-sexp-safe -
4)
2125 (should (looking-at "profile ="))
2126 (python-tests-look-at "user=request.user")
2127 (python-nav-forward-sexp-safe 3)
2128 (should (looking-at ")"))
2129 (python-nav-forward-sexp-safe 1)
2130 (should (looking-at "$"))
2131 (python-nav-forward-sexp-safe 1)
2132 (should (looking-at "$"))))
2134 (ert-deftest python-nav-up-list-1
()
2135 (python-tests-with-temp-buffer
2139 return [i for i in range(3)]
2141 (python-tests-look-at "3)]")
2142 (python-nav-up-list)
2143 (should (looking-at "]"))
2144 (python-nav-up-list)
2145 (should (looking-at "$"))))
2147 (ert-deftest python-nav-backward-up-list-1
()
2148 :expected-result
:failed
2149 (python-tests-with-temp-buffer
2153 return [i for i in range(3)]
2155 (python-tests-look-at "3)]")
2156 (python-nav-backward-up-list)
2157 (should (looking-at "(3)\\]"))
2158 (python-nav-backward-up-list)
2160 "\\[i for i in range(3)\\]"))
2161 ;; FIXME: Need to move to beginning-of-statement.
2162 (python-nav-backward-up-list)
2164 "return \\[i for i in range(3)\\]"))
2165 (python-nav-backward-up-list)
2166 (should (looking-at "if True:"))
2167 (python-nav-backward-up-list)
2168 (should (looking-at "def f():"))))
2170 (ert-deftest python-indent-dedent-line-backspace-1
()
2171 "Check de-indentation on first call. Bug#18319."
2172 (python-tests-with-temp-buffer
2178 (python-tests-look-at "if False:")
2179 (call-interactively #'python-indent-dedent-line-backspace
)
2180 (should (zerop (current-indentation)))
2181 ;; XXX: This should be a call to `undo' but it's triggering errors.
2183 (should (= (current-indentation) 4))
2184 (call-interactively #'python-indent-dedent-line-backspace
)
2185 (should (zerop (current-indentation)))))
2187 (ert-deftest python-indent-dedent-line-backspace-2
()
2188 "Check de-indentation with tabs. Bug#19730."
2189 (let ((tab-width 8))
2190 (python-tests-with-temp-buffer
2195 (python-tests-look-at "abcdefg")
2196 (goto-char (line-end-position))
2197 (call-interactively #'python-indent-dedent-line-backspace
)
2199 (string= (buffer-substring-no-properties
2200 (line-beginning-position) (line-end-position))
2203 (ert-deftest python-indent-dedent-line-backspace-3
()
2204 "Paranoid check of de-indentation with tabs. Bug#19730."
2205 (let ((tab-width 8))
2206 (python-tests-with-temp-buffer
2212 (python-tests-look-at "abcdefg")
2213 (goto-char (line-end-position))
2214 (call-interactively #'python-indent-dedent-line-backspace
)
2216 (string= (buffer-substring-no-properties
2217 (line-beginning-position) (line-end-position))
2219 (back-to-indentation)
2220 (call-interactively #'python-indent-dedent-line-backspace
)
2222 (string= (buffer-substring-no-properties
2223 (line-beginning-position) (line-end-position))
2225 (call-interactively #'python-indent-dedent-line-backspace
)
2227 (string= (buffer-substring-no-properties
2228 (line-beginning-position) (line-end-position))
2230 (call-interactively #'python-indent-dedent-line-backspace
)
2232 (string= (buffer-substring-no-properties
2233 (line-beginning-position) (line-end-position))
2237 ;;; Shell integration
2239 (defvar python-tests-shell-interpreter
"python")
2241 (ert-deftest python-shell-get-process-name-1
()
2242 "Check process name calculation sans `buffer-file-name'."
2243 (python-tests-with-temp-buffer
2245 (should (string= (python-shell-get-process-name nil
)
2246 python-shell-buffer-name
))
2247 (should (string= (python-shell-get-process-name t
)
2248 (format "%s[%s]" python-shell-buffer-name
(buffer-name))))))
2250 (ert-deftest python-shell-get-process-name-2
()
2251 "Check process name calculation with `buffer-file-name'."
2252 (python-tests-with-temp-file
2254 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
2255 ;; should be respected.
2256 (should (string= (python-shell-get-process-name nil
)
2257 python-shell-buffer-name
))
2259 (python-shell-get-process-name t
)
2260 (format "%s[%s]" python-shell-buffer-name
(buffer-name))))))
2262 (ert-deftest python-shell-internal-get-process-name-1
()
2263 "Check the internal process name is buffer-unique sans `buffer-file-name'."
2264 (python-tests-with-temp-buffer
2266 (should (string= (python-shell-internal-get-process-name)
2267 (format "%s[%s]" python-shell-internal-buffer-name
(buffer-name))))))
2269 (ert-deftest python-shell-internal-get-process-name-2
()
2270 "Check the internal process name is buffer-unique with `buffer-file-name'."
2271 (python-tests-with-temp-file
2273 (should (string= (python-shell-internal-get-process-name)
2274 (format "%s[%s]" python-shell-internal-buffer-name
(buffer-name))))))
2276 (ert-deftest python-shell-calculate-command-1
()
2277 "Check the command to execute is calculated correctly.
2278 Using `python-shell-interpreter' and
2279 `python-shell-interpreter-args'."
2280 (skip-unless (executable-find python-tests-shell-interpreter
))
2281 (let ((python-shell-interpreter (executable-find
2282 python-tests-shell-interpreter
))
2283 (python-shell-interpreter-args "-B"))
2286 python-shell-interpreter
2287 python-shell-interpreter-args
)
2288 (python-shell-calculate-command)))))
2290 (ert-deftest python-shell-calculate-process-environment-1
()
2291 "Test `python-shell-process-environment' modification."
2292 (let* ((python-shell-process-environment
2293 '("TESTVAR1=value1" "TESTVAR2=value2"))
2294 (process-environment
2295 (python-shell-calculate-process-environment)))
2296 (should (equal (getenv "TESTVAR1") "value1"))
2297 (should (equal (getenv "TESTVAR2") "value2"))))
2299 (ert-deftest python-shell-calculate-process-environment-2
()
2300 "Test `python-shell-extra-pythonpaths' modification."
2301 (let* ((process-environment process-environment
)
2302 (original-pythonpath (setenv "PYTHONPATH" "path3"))
2303 (paths '("path1" "path2"))
2304 (python-shell-extra-pythonpaths paths
)
2305 (process-environment
2306 (python-shell-calculate-process-environment)))
2307 (should (equal (getenv "PYTHONPATH")
2309 (mapconcat 'identity paths path-separator
)
2310 path-separator original-pythonpath
)))))
2312 (ert-deftest python-shell-calculate-process-environment-3
()
2313 "Test `python-shell-virtualenv-root' modification."
2314 (let* ((original-path (or (getenv "PATH") ""))
2315 (python-shell-virtualenv-root
2316 (directory-file-name user-emacs-directory
))
2317 (process-environment
2318 (python-shell-calculate-process-environment)))
2319 (should (not (getenv "PYTHONHOME")))
2320 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-root
))
2321 (should (equal (getenv "PATH")
2322 (format "%s/bin%s%s"
2323 python-shell-virtualenv-root
2324 path-separator original-path
)))))
2326 (ert-deftest python-shell-calculate-process-environment-4
()
2327 "Test `python-shell-unbuffered' modification."
2328 (setenv "PYTHONUNBUFFERED")
2329 (let* ((process-environment
2330 (python-shell-calculate-process-environment)))
2332 (should python-shell-unbuffered
)
2333 (should (string= (getenv "PYTHONUNBUFFERED") "1"))))
2335 (ert-deftest python-shell-calculate-process-environment-5
()
2336 (setenv "PYTHONUNBUFFERED")
2337 "Test `python-shell-unbuffered' modification."
2338 (let* ((python-shell-unbuffered nil
)
2339 (process-environment
2340 (python-shell-calculate-process-environment)))
2341 (should (not (getenv "PYTHONUNBUFFERED")))))
2343 (ert-deftest python-shell-calculate-exec-path-1
()
2344 "Test `python-shell-exec-path' modification."
2345 (let* ((original-exec-path exec-path
)
2346 (python-shell-exec-path '("path1" "path2"))
2347 (exec-path (python-shell-calculate-exec-path)))
2350 (append python-shell-exec-path
2351 original-exec-path
)))))
2353 (ert-deftest python-shell-calculate-exec-path-2
()
2354 "Test `python-shell-exec-path' modification."
2355 (let* ((original-exec-path exec-path
)
2356 (python-shell-virtualenv-root
2357 (directory-file-name (expand-file-name user-emacs-directory
)))
2358 (exec-path (python-shell-calculate-exec-path)))
2362 (format "%s/bin" python-shell-virtualenv-root
)
2363 original-exec-path
))))))
2365 (ert-deftest python-shell-make-comint-1
()
2366 "Check comint creation for global shell buffer."
2367 (skip-unless (executable-find python-tests-shell-interpreter
))
2368 ;; The interpreter can get killed too quickly to allow it to clean
2369 ;; up the tempfiles that the default python-shell-setup-codes create,
2370 ;; so it leaves tempfiles behind, which is a minor irritation.
2371 (let* ((python-shell-setup-codes nil
)
2372 (python-shell-interpreter
2373 (executable-find python-tests-shell-interpreter
))
2374 (proc-name (python-shell-get-process-name nil
))
2376 (python-tests-with-temp-buffer
2377 "" (python-shell-make-comint
2378 (python-shell-calculate-command) proc-name
)))
2379 (process (get-buffer-process shell-buffer
)))
2382 (set-process-query-on-exit-flag process nil
)
2383 (should (process-live-p process
))
2384 (with-current-buffer shell-buffer
2385 (should (eq major-mode
'inferior-python-mode
))
2386 (should (string= (buffer-name) (format "*%s*" proc-name
)))))
2387 (kill-buffer shell-buffer
))))
2389 (ert-deftest python-shell-make-comint-2
()
2390 "Check comint creation for internal shell buffer."
2391 (skip-unless (executable-find python-tests-shell-interpreter
))
2392 (let* ((python-shell-setup-codes nil
)
2393 (python-shell-interpreter
2394 (executable-find python-tests-shell-interpreter
))
2395 (proc-name (python-shell-internal-get-process-name))
2397 (python-tests-with-temp-buffer
2398 "" (python-shell-make-comint
2399 (python-shell-calculate-command) proc-name nil t
)))
2400 (process (get-buffer-process shell-buffer
)))
2403 (set-process-query-on-exit-flag process nil
)
2404 (should (process-live-p process
))
2405 (with-current-buffer shell-buffer
2406 (should (eq major-mode
'inferior-python-mode
))
2407 (should (string= (buffer-name) (format " *%s*" proc-name
)))))
2408 (kill-buffer shell-buffer
))))
2410 (ert-deftest python-shell-make-comint-3
()
2411 "Check comint creation with overridden python interpreter and args.
2412 The command passed to `python-shell-make-comint' as argument must
2413 locally override global values set in `python-shell-interpreter'
2414 and `python-shell-interpreter-args' in the new shell buffer."
2415 (skip-unless (executable-find python-tests-shell-interpreter
))
2416 (let* ((python-shell-setup-codes nil
)
2417 (python-shell-interpreter "interpreter")
2418 (python-shell-interpreter-args "--some-args")
2419 (proc-name (python-shell-get-process-name nil
))
2420 (interpreter-override
2421 (concat (executable-find python-tests-shell-interpreter
) " " "-i"))
2423 (python-tests-with-temp-buffer
2424 "" (python-shell-make-comint interpreter-override proc-name nil
)))
2425 (process (get-buffer-process shell-buffer
)))
2428 (set-process-query-on-exit-flag process nil
)
2429 (should (process-live-p process
))
2430 (with-current-buffer shell-buffer
2431 (should (eq major-mode
'inferior-python-mode
))
2432 (should (file-equal-p
2433 python-shell-interpreter
2434 (executable-find python-tests-shell-interpreter
)))
2435 (should (string= python-shell-interpreter-args
"-i"))))
2436 (kill-buffer shell-buffer
))))
2438 (ert-deftest python-shell-make-comint-4
()
2439 "Check shell calculated prompts regexps are set."
2440 (skip-unless (executable-find python-tests-shell-interpreter
))
2441 (let* ((process-environment process-environment
)
2442 (python-shell-setup-codes nil
)
2443 (python-shell-interpreter
2444 (executable-find python-tests-shell-interpreter
))
2445 (python-shell-interpreter-args "-i")
2446 (python-shell--prompt-calculated-input-regexp nil
)
2447 (python-shell--prompt-calculated-output-regexp nil
)
2448 (python-shell-prompt-detect-enabled t
)
2449 (python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2450 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2451 (python-shell-prompt-regexp "in")
2452 (python-shell-prompt-block-regexp "block")
2453 (python-shell-prompt-pdb-regexp "pdf")
2454 (python-shell-prompt-output-regexp "output")
2455 (startup-code (concat "import sys\n"
2456 "sys.ps1 = 'py> '\n"
2457 "sys.ps2 = '..> '\n"
2458 "sys.ps3 = 'out '\n"))
2459 (startup-file (python-shell--save-temp-file startup-code
))
2460 (proc-name (python-shell-get-process-name nil
))
2463 (setenv "PYTHONSTARTUP" startup-file
)
2464 (python-tests-with-temp-buffer
2465 "" (python-shell-make-comint
2466 (python-shell-calculate-command) proc-name nil
))))
2467 (process (get-buffer-process shell-buffer
)))
2470 (set-process-query-on-exit-flag process nil
)
2471 (should (process-live-p process
))
2472 (with-current-buffer shell-buffer
2473 (should (eq major-mode
'inferior-python-mode
))
2475 python-shell--prompt-calculated-input-regexp
2476 (concat "^\\(extralargeinputprompt\\|\\.\\.> \\|"
2477 "block\\|py> \\|pdf\\|sml\\|in\\)")))
2479 python-shell--prompt-calculated-output-regexp
2480 "^\\(extralargeoutputprompt\\|output\\|out \\|sml\\)"))))
2481 (delete-file startup-file
)
2482 (kill-buffer shell-buffer
))))
2484 (ert-deftest python-shell-get-process-1
()
2485 "Check dedicated shell process preference over global."
2486 (skip-unless (executable-find python-tests-shell-interpreter
))
2487 (python-tests-with-temp-file
2489 (let* ((python-shell-setup-codes nil
)
2490 (python-shell-interpreter
2491 (executable-find python-tests-shell-interpreter
))
2492 (global-proc-name (python-shell-get-process-name nil
))
2493 (dedicated-proc-name (python-shell-get-process-name t
))
2494 (global-shell-buffer
2495 (python-shell-make-comint
2496 (python-shell-calculate-command) global-proc-name
))
2497 (dedicated-shell-buffer
2498 (python-shell-make-comint
2499 (python-shell-calculate-command) dedicated-proc-name
))
2500 (global-process (get-buffer-process global-shell-buffer
))
2501 (dedicated-process (get-buffer-process dedicated-shell-buffer
)))
2504 (set-process-query-on-exit-flag global-process nil
)
2505 (set-process-query-on-exit-flag dedicated-process nil
)
2506 ;; Prefer dedicated if global also exists.
2507 (should (equal (python-shell-get-process) dedicated-process
))
2508 (kill-buffer dedicated-shell-buffer
)
2509 ;; If there's only global, use it.
2510 (should (equal (python-shell-get-process) global-process
))
2511 (kill-buffer global-shell-buffer
)
2512 ;; No buffer available.
2513 (should (not (python-shell-get-process))))
2514 (ignore-errors (kill-buffer global-shell-buffer
))
2515 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
2517 (ert-deftest python-shell-internal-get-or-create-process-1
()
2518 "Check internal shell process creation fallback."
2519 (skip-unless (executable-find python-tests-shell-interpreter
))
2520 (python-tests-with-temp-file
2522 (should (not (process-live-p (python-shell-internal-get-process-name))))
2523 (let* ((python-shell-interpreter
2524 (executable-find python-tests-shell-interpreter
))
2525 (internal-process-name (python-shell-internal-get-process-name))
2526 (internal-process (python-shell-internal-get-or-create-process))
2527 (internal-shell-buffer (process-buffer internal-process
)))
2530 (set-process-query-on-exit-flag internal-process nil
)
2531 (should (equal (process-name internal-process
)
2532 internal-process-name
))
2533 (should (equal internal-process
2534 (python-shell-internal-get-or-create-process)))
2535 ;; Assert the internal process is not a user process
2536 (should (not (python-shell-get-process)))
2537 (kill-buffer internal-shell-buffer
))
2538 (ignore-errors (kill-buffer internal-shell-buffer
))))))
2540 (ert-deftest python-shell-prompt-detect-1
()
2541 "Check prompt autodetection."
2542 (skip-unless (executable-find python-tests-shell-interpreter
))
2543 (let ((process-environment process-environment
))
2544 ;; Ensure no startup file is enabled
2545 (setenv "PYTHONSTARTUP" "")
2546 (should python-shell-prompt-detect-enabled
)
2547 (should (equal (python-shell-prompt-detect) '(">>> " "... " "")))))
2549 (ert-deftest python-shell-prompt-detect-2
()
2550 "Check prompt autodetection with startup file. Bug#17370."
2551 (skip-unless (executable-find python-tests-shell-interpreter
))
2552 (let* ((process-environment process-environment
)
2553 (startup-code (concat "import sys\n"
2554 "sys.ps1 = 'py> '\n"
2555 "sys.ps2 = '..> '\n"
2556 "sys.ps3 = 'out '\n"))
2557 (startup-file (python-shell--save-temp-file startup-code
)))
2560 ;; Ensure startup file is enabled
2561 (setenv "PYTHONSTARTUP" startup-file
)
2562 (should python-shell-prompt-detect-enabled
)
2563 (should (equal (python-shell-prompt-detect) '("py> " "..> " "out "))))
2564 (ignore-errors (delete-file startup-file
)))))
2566 (ert-deftest python-shell-prompt-detect-3
()
2567 "Check prompts are not autodetected when feature is disabled."
2568 (skip-unless (executable-find python-tests-shell-interpreter
))
2569 (let ((process-environment process-environment
)
2570 (python-shell-prompt-detect-enabled nil
))
2571 ;; Ensure no startup file is enabled
2572 (should (not python-shell-prompt-detect-enabled
))
2573 (should (not (python-shell-prompt-detect)))))
2575 (ert-deftest python-shell-prompt-detect-4
()
2576 "Check warning is shown when detection fails."
2577 (skip-unless (executable-find python-tests-shell-interpreter
))
2578 (let* ((process-environment process-environment
)
2579 ;; Trigger failure by removing prompts in the startup file
2580 (startup-code (concat "import sys\n"
2584 (startup-file (python-shell--save-temp-file startup-code
)))
2587 (kill-buffer (get-buffer-create "*Warnings*"))
2588 (should (not (get-buffer "*Warnings*")))
2589 (setenv "PYTHONSTARTUP" startup-file
)
2590 (should python-shell-prompt-detect-failure-warning
)
2591 (should python-shell-prompt-detect-enabled
)
2592 (should (not (python-shell-prompt-detect)))
2593 (should (get-buffer "*Warnings*")))
2594 (ignore-errors (delete-file startup-file
)))))
2596 (ert-deftest python-shell-prompt-detect-5
()
2597 "Check disabled warnings are not shown when detection fails."
2598 (skip-unless (executable-find python-tests-shell-interpreter
))
2599 (let* ((process-environment process-environment
)
2600 (startup-code (concat "import sys\n"
2604 (startup-file (python-shell--save-temp-file startup-code
))
2605 (python-shell-prompt-detect-failure-warning nil
))
2608 (kill-buffer (get-buffer-create "*Warnings*"))
2609 (should (not (get-buffer "*Warnings*")))
2610 (setenv "PYTHONSTARTUP" startup-file
)
2611 (should (not python-shell-prompt-detect-failure-warning
))
2612 (should python-shell-prompt-detect-enabled
)
2613 (should (not (python-shell-prompt-detect)))
2614 (should (not (get-buffer "*Warnings*"))))
2615 (ignore-errors (delete-file startup-file
)))))
2617 (ert-deftest python-shell-prompt-detect-6
()
2618 "Warnings are not shown when detection is disabled."
2619 (skip-unless (executable-find python-tests-shell-interpreter
))
2620 (let* ((process-environment process-environment
)
2621 (startup-code (concat "import sys\n"
2625 (startup-file (python-shell--save-temp-file startup-code
))
2626 (python-shell-prompt-detect-failure-warning t
)
2627 (python-shell-prompt-detect-enabled nil
))
2630 (kill-buffer (get-buffer-create "*Warnings*"))
2631 (should (not (get-buffer "*Warnings*")))
2632 (setenv "PYTHONSTARTUP" startup-file
)
2633 (should python-shell-prompt-detect-failure-warning
)
2634 (should (not python-shell-prompt-detect-enabled
))
2635 (should (not (python-shell-prompt-detect)))
2636 (should (not (get-buffer "*Warnings*"))))
2637 (ignore-errors (delete-file startup-file
)))))
2639 (ert-deftest python-shell-prompt-validate-regexps-1
()
2640 "Check `python-shell-prompt-input-regexps' are validated."
2641 (let* ((python-shell-prompt-input-regexps '("\\("))
2642 (error-data (should-error (python-shell-prompt-validate-regexps)
2643 :type
'user-error
)))
2645 (string= (cadr error-data
)
2646 "Invalid regexp \\( in `python-shell-prompt-input-regexps'"))))
2648 (ert-deftest python-shell-prompt-validate-regexps-2
()
2649 "Check `python-shell-prompt-output-regexps' are validated."
2650 (let* ((python-shell-prompt-output-regexps '("\\("))
2651 (error-data (should-error (python-shell-prompt-validate-regexps)
2652 :type
'user-error
)))
2654 (string= (cadr error-data
)
2655 "Invalid regexp \\( in `python-shell-prompt-output-regexps'"))))
2657 (ert-deftest python-shell-prompt-validate-regexps-3
()
2658 "Check `python-shell-prompt-regexp' is validated."
2659 (let* ((python-shell-prompt-regexp "\\(")
2660 (error-data (should-error (python-shell-prompt-validate-regexps)
2661 :type
'user-error
)))
2663 (string= (cadr error-data
)
2664 "Invalid regexp \\( in `python-shell-prompt-regexp'"))))
2666 (ert-deftest python-shell-prompt-validate-regexps-4
()
2667 "Check `python-shell-prompt-block-regexp' is validated."
2668 (let* ((python-shell-prompt-block-regexp "\\(")
2669 (error-data (should-error (python-shell-prompt-validate-regexps)
2670 :type
'user-error
)))
2672 (string= (cadr error-data
)
2673 "Invalid regexp \\( in `python-shell-prompt-block-regexp'"))))
2675 (ert-deftest python-shell-prompt-validate-regexps-5
()
2676 "Check `python-shell-prompt-pdb-regexp' is validated."
2677 (let* ((python-shell-prompt-pdb-regexp "\\(")
2678 (error-data (should-error (python-shell-prompt-validate-regexps)
2679 :type
'user-error
)))
2681 (string= (cadr error-data
)
2682 "Invalid regexp \\( in `python-shell-prompt-pdb-regexp'"))))
2684 (ert-deftest python-shell-prompt-validate-regexps-6
()
2685 "Check `python-shell-prompt-output-regexp' is validated."
2686 (let* ((python-shell-prompt-output-regexp "\\(")
2687 (error-data (should-error (python-shell-prompt-validate-regexps)
2688 :type
'user-error
)))
2690 (string= (cadr error-data
)
2691 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2693 (ert-deftest python-shell-prompt-validate-regexps-7
()
2694 "Check default regexps are valid."
2695 ;; should not signal error
2696 (python-shell-prompt-validate-regexps))
2698 (ert-deftest python-shell-prompt-set-calculated-regexps-1
()
2699 "Check regexps are validated."
2700 (let* ((python-shell-prompt-output-regexp '("\\("))
2701 (python-shell--prompt-calculated-input-regexp nil
)
2702 (python-shell--prompt-calculated-output-regexp nil
)
2703 (python-shell-prompt-detect-enabled nil
)
2704 (error-data (should-error (python-shell-prompt-set-calculated-regexps)
2705 :type
'user-error
)))
2707 (string= (cadr error-data
)
2708 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2710 (ert-deftest python-shell-prompt-set-calculated-regexps-2
()
2711 "Check `python-shell-prompt-input-regexps' are set."
2712 (let* ((python-shell-prompt-input-regexps '("my" "prompt"))
2713 (python-shell-prompt-output-regexps '(""))
2714 (python-shell-prompt-regexp "")
2715 (python-shell-prompt-block-regexp "")
2716 (python-shell-prompt-pdb-regexp "")
2717 (python-shell-prompt-output-regexp "")
2718 (python-shell--prompt-calculated-input-regexp nil
)
2719 (python-shell--prompt-calculated-output-regexp nil
)
2720 (python-shell-prompt-detect-enabled nil
))
2721 (python-shell-prompt-set-calculated-regexps)
2722 (should (string= python-shell--prompt-calculated-input-regexp
2723 "^\\(prompt\\|my\\|\\)"))))
2725 (ert-deftest python-shell-prompt-set-calculated-regexps-3
()
2726 "Check `python-shell-prompt-output-regexps' are set."
2727 (let* ((python-shell-prompt-input-regexps '(""))
2728 (python-shell-prompt-output-regexps '("my" "prompt"))
2729 (python-shell-prompt-regexp "")
2730 (python-shell-prompt-block-regexp "")
2731 (python-shell-prompt-pdb-regexp "")
2732 (python-shell-prompt-output-regexp "")
2733 (python-shell--prompt-calculated-input-regexp nil
)
2734 (python-shell--prompt-calculated-output-regexp nil
)
2735 (python-shell-prompt-detect-enabled nil
))
2736 (python-shell-prompt-set-calculated-regexps)
2737 (should (string= python-shell--prompt-calculated-output-regexp
2738 "^\\(prompt\\|my\\|\\)"))))
2740 (ert-deftest python-shell-prompt-set-calculated-regexps-4
()
2741 "Check user defined prompts are set."
2742 (let* ((python-shell-prompt-input-regexps '(""))
2743 (python-shell-prompt-output-regexps '(""))
2744 (python-shell-prompt-regexp "prompt")
2745 (python-shell-prompt-block-regexp "block")
2746 (python-shell-prompt-pdb-regexp "pdb")
2747 (python-shell-prompt-output-regexp "output")
2748 (python-shell--prompt-calculated-input-regexp nil
)
2749 (python-shell--prompt-calculated-output-regexp nil
)
2750 (python-shell-prompt-detect-enabled nil
))
2751 (python-shell-prompt-set-calculated-regexps)
2752 (should (string= python-shell--prompt-calculated-input-regexp
2753 "^\\(prompt\\|block\\|pdb\\|\\)"))
2754 (should (string= python-shell--prompt-calculated-output-regexp
2755 "^\\(output\\|\\)"))))
2757 (ert-deftest python-shell-prompt-set-calculated-regexps-5
()
2758 "Check order of regexps (larger first)."
2759 (let* ((python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2760 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2761 (python-shell-prompt-regexp "in")
2762 (python-shell-prompt-block-regexp "block")
2763 (python-shell-prompt-pdb-regexp "pdf")
2764 (python-shell-prompt-output-regexp "output")
2765 (python-shell--prompt-calculated-input-regexp nil
)
2766 (python-shell--prompt-calculated-output-regexp nil
)
2767 (python-shell-prompt-detect-enabled nil
))
2768 (python-shell-prompt-set-calculated-regexps)
2769 (should (string= python-shell--prompt-calculated-input-regexp
2770 "^\\(extralargeinputprompt\\|block\\|pdf\\|sml\\|in\\)"))
2771 (should (string= python-shell--prompt-calculated-output-regexp
2772 "^\\(extralargeoutputprompt\\|output\\|sml\\)"))))
2774 (ert-deftest python-shell-prompt-set-calculated-regexps-6
()
2775 "Check detected prompts are included `regexp-quote'd."
2776 (skip-unless (executable-find python-tests-shell-interpreter
))
2777 (let* ((python-shell-prompt-input-regexps '(""))
2778 (python-shell-prompt-output-regexps '(""))
2779 (python-shell-prompt-regexp "")
2780 (python-shell-prompt-block-regexp "")
2781 (python-shell-prompt-pdb-regexp "")
2782 (python-shell-prompt-output-regexp "")
2783 (python-shell--prompt-calculated-input-regexp nil
)
2784 (python-shell--prompt-calculated-output-regexp nil
)
2785 (python-shell-prompt-detect-enabled t
)
2786 (process-environment process-environment
)
2787 (startup-code (concat "import sys\n"
2788 "sys.ps1 = 'p.> '\n"
2789 "sys.ps2 = '..> '\n"
2790 "sys.ps3 = 'o.t '\n"))
2791 (startup-file (python-shell--save-temp-file startup-code
)))
2794 (setenv "PYTHONSTARTUP" startup-file
)
2795 (python-shell-prompt-set-calculated-regexps)
2796 (should (string= python-shell--prompt-calculated-input-regexp
2797 "^\\(\\.\\.> \\|p\\.> \\|\\)"))
2798 (should (string= python-shell--prompt-calculated-output-regexp
2799 "^\\(o\\.t \\|\\)")))
2800 (ignore-errors (delete-file startup-file
)))))
2802 (ert-deftest python-shell-buffer-substring-1
()
2803 "Selecting a substring of the whole buffer must match its contents."
2804 (python-tests-with-temp-buffer
2806 class Foo(models.Model):
2810 class Bar(models.Model):
2813 (should (string= (buffer-string)
2814 (python-shell-buffer-substring (point-min) (point-max))))))
2816 (ert-deftest python-shell-buffer-substring-2
()
2817 "Main block should be removed if NOMAIN is non-nil."
2818 (python-tests-with-temp-buffer
2820 class Foo(models.Model):
2823 class Bar(models.Model):
2826 if __name__ == \"__main__\":
2830 (should (string= (python-shell-buffer-substring (point-min) (point-max) t
)
2832 class Foo(models.Model):
2835 class Bar(models.Model):
2843 (ert-deftest python-shell-buffer-substring-3
()
2844 "Main block should be removed if NOMAIN is non-nil."
2845 (python-tests-with-temp-buffer
2847 class Foo(models.Model):
2850 if __name__ == \"__main__\":
2854 class Bar(models.Model):
2857 (should (string= (python-shell-buffer-substring (point-min) (point-max) t
)
2859 class Foo(models.Model):
2866 class Bar(models.Model):
2870 (ert-deftest python-shell-buffer-substring-4
()
2871 "Coding cookie should be added for substrings."
2872 (python-tests-with-temp-buffer
2875 class Foo(models.Model):
2878 if __name__ == \"__main__\":
2882 class Bar(models.Model):
2885 (should (string= (python-shell-buffer-substring
2886 (python-tests-look-at "class Foo(models.Model):")
2887 (progn (python-nav-forward-sexp) (point)))
2888 "# -*- coding: latin-1 -*-
2890 class Foo(models.Model):
2893 (ert-deftest python-shell-buffer-substring-5
()
2894 "The proper amount of blank lines is added for a substring."
2895 (python-tests-with-temp-buffer
2898 class Foo(models.Model):
2901 if __name__ == \"__main__\":
2905 class Bar(models.Model):
2908 (should (string= (python-shell-buffer-substring
2909 (python-tests-look-at "class Bar(models.Model):")
2910 (progn (python-nav-forward-sexp) (point)))
2911 "# -*- coding: latin-1 -*-
2920 class Bar(models.Model):
2923 (ert-deftest python-shell-buffer-substring-6
()
2924 "Handle substring with coding cookie in the second line."
2925 (python-tests-with-temp-buffer
2929 class Foo(models.Model):
2932 if __name__ == \"__main__\":
2936 class Bar(models.Model):
2939 (should (string= (python-shell-buffer-substring
2940 (python-tests-look-at "# coding: latin-1")
2941 (python-tests-look-at "if __name__ == \"__main__\":"))
2942 "# -*- coding: latin-1 -*-
2945 class Foo(models.Model):
2950 (ert-deftest python-shell-buffer-substring-7
()
2951 "Ensure first coding cookie gets precedence."
2952 (python-tests-with-temp-buffer
2956 class Foo(models.Model):
2959 if __name__ == \"__main__\":
2963 class Bar(models.Model):
2966 (should (string= (python-shell-buffer-substring
2967 (python-tests-look-at "# coding: latin-1")
2968 (python-tests-look-at "if __name__ == \"__main__\":"))
2969 "# -*- coding: utf-8 -*-
2972 class Foo(models.Model):
2977 (ert-deftest python-shell-buffer-substring-8
()
2978 "Ensure first coding cookie gets precedence when sending whole buffer."
2979 (python-tests-with-temp-buffer
2983 class Foo(models.Model):
2986 (should (string= (python-shell-buffer-substring (point-min) (point-max))
2990 class Foo(models.Model):
2994 (ert-deftest python-shell-buffer-substring-9
()
2995 "Check substring starting from `point-min'."
2996 (python-tests-with-temp-buffer
2999 class Foo(models.Model):
3002 class Bar(models.Model):
3005 (should (string= (python-shell-buffer-substring
3007 (python-tests-look-at "class Bar(models.Model):"))
3010 class Foo(models.Model):
3016 ;;; Shell completion
3018 (ert-deftest python-shell-completion-native-interpreter-disabled-p-1
()
3019 (let* ((python-shell-completion-native-disabled-interpreters (list "pypy"))
3020 (python-shell-interpreter "/some/path/to/bin/pypy"))
3021 (should (python-shell-completion-native-interpreter-disabled-p))))
3026 ;;; PDB Track integration
3029 ;;; Symbol completion
3046 (ert-deftest python-eldoc--get-symbol-at-point-1
()
3047 "Test paren handling."
3048 (python-tests-with-temp-buffer
3051 map(codecs.open('somefile'
3053 (python-tests-look-at "ap(xx")
3054 (should (string= (python-eldoc--get-symbol-at-point) "map"))
3055 (goto-char (line-end-position))
3056 (should (string= (python-eldoc--get-symbol-at-point) "map"))
3057 (python-tests-look-at "('somefile'")
3058 (should (string= (python-eldoc--get-symbol-at-point) "map"))
3059 (goto-char (line-end-position))
3060 (should (string= (python-eldoc--get-symbol-at-point) "codecs.open"))))
3062 (ert-deftest python-eldoc--get-symbol-at-point-2
()
3063 "Ensure self is replaced with the class name."
3064 (python-tests-with-temp-buffer
3068 def some_method(self, n):
3072 return self.some_method(1234)
3075 (python-tests-look-at "self.some_method")
3076 (should (string= (python-eldoc--get-symbol-at-point)
3077 "TheClass.some_method"))
3078 (python-tests-look-at "1234)")
3079 (should (string= (python-eldoc--get-symbol-at-point)
3080 "TheClass.some_method"))))
3082 (ert-deftest python-eldoc--get-symbol-at-point-3
()
3083 "Ensure symbol is found when point is at end of buffer."
3084 (python-tests-with-temp-buffer
3089 (goto-char (point-max))
3090 (should (string= (python-eldoc--get-symbol-at-point)
3093 (ert-deftest python-eldoc--get-symbol-at-point-4
()
3094 "Ensure symbol is found when point is at whitespace."
3095 (python-tests-with-temp-buffer
3097 some_symbol some_other_symbol
3099 (python-tests-look-at " some_other_symbol")
3100 (should (string= (python-eldoc--get-symbol-at-point)
3106 (ert-deftest python-imenu-create-index-1
()
3107 (python-tests-with-temp-buffer
3109 class Foo(models.Model):
3113 class Bar(models.Model):
3117 def decorator(arg1, arg2, arg3):
3118 '''print decorated function call data to stdout.
3122 @decorator('arg1', 'arg2')
3123 def func(a, b, c=True):
3129 def wrapped_f(*args):
3131 print ('Decorator arguments:', arg1, arg2, arg3)
3133 print ('called f(*args)')
3151 (goto-char (point-max))
3154 (cons "Foo (class)" (copy-marker 2))
3155 (cons "Bar (class)" (copy-marker 38))
3158 (cons "*function definition*" (copy-marker 74))
3161 (cons "*function definition*" (copy-marker 254))
3162 (cons "wrapped_f (def)" (copy-marker 294))))
3165 (cons "*class definition*" (copy-marker 519))
3166 (cons "a (def)" (copy-marker 539))
3167 (cons "b (def)" (copy-marker 570))
3170 (cons "*class definition*" (copy-marker 601))
3171 (cons "c (def)" (copy-marker 626)))))
3172 (python-imenu-create-index)))))
3174 (ert-deftest python-imenu-create-index-2
()
3175 (python-tests-with-temp-buffer
3185 (goto-char (point-max))
3190 (cons "*class definition*" (copy-marker 2))
3193 (cons "*function definition*" (copy-marker 21))
3194 (cons "foo1 (def)" (copy-marker 40)))
3195 (cons "foobar (def)" (copy-marker 78))))
3196 (python-imenu-create-index)))))
3198 (ert-deftest python-imenu-create-index-3
()
3199 (python-tests-with-temp-buffer
3208 (goto-char (point-max))
3213 (cons "*class definition*" (copy-marker 2))
3216 (cons "*function definition*" (copy-marker 21))
3217 (cons "foo1 (def)" (copy-marker 40))
3218 (cons "foo2 (def)" (copy-marker 77)))))
3219 (python-imenu-create-index)))))
3221 (ert-deftest python-imenu-create-index-4
()
3222 (python-tests-with-temp-buffer
3235 (goto-char (point-max))
3240 (cons "*class definition*" (copy-marker 2))
3243 (cons "*class definition*" (copy-marker 21))
3244 (cons "__init__ (def)" (copy-marker 44))
3245 (cons "__str__ (def)" (copy-marker 90)))
3246 (cons "__init__ (def)" (copy-marker 135))))
3247 (python-imenu-create-index)))))
3249 (ert-deftest python-imenu-create-flat-index-1
()
3250 (python-tests-with-temp-buffer
3252 class Foo(models.Model):
3256 class Bar(models.Model):
3260 def decorator(arg1, arg2, arg3):
3261 '''print decorated function call data to stdout.
3265 @decorator('arg1', 'arg2')
3266 def func(a, b, c=True):
3272 def wrapped_f(*args):
3274 print ('Decorator arguments:', arg1, arg2, arg3)
3276 print ('called f(*args)')
3294 (goto-char (point-max))
3296 (list (cons "Foo" (copy-marker 2))
3297 (cons "Bar" (copy-marker 38))
3298 (cons "decorator" (copy-marker 74))
3299 (cons "decorator.wrap" (copy-marker 254))
3300 (cons "decorator.wrap.wrapped_f" (copy-marker 294))
3301 (cons "Baz" (copy-marker 519))
3302 (cons "Baz.a" (copy-marker 539))
3303 (cons "Baz.b" (copy-marker 570))
3304 (cons "Baz.Frob" (copy-marker 601))
3305 (cons "Baz.Frob.c" (copy-marker 626)))
3306 (python-imenu-create-flat-index)))))
3308 (ert-deftest python-imenu-create-flat-index-2
()
3309 (python-tests-with-temp-buffer
3322 (goto-char (point-max))
3325 (cons "Foo" (copy-marker 2))
3326 (cons "Foo.Bar" (copy-marker 21))
3327 (cons "Foo.Bar.__init__" (copy-marker 44))
3328 (cons "Foo.Bar.__str__" (copy-marker 90))
3329 (cons "Foo.__init__" (copy-marker 135)))
3330 (python-imenu-create-flat-index)))))
3335 (ert-deftest python-info-current-defun-1
()
3336 (python-tests-with-temp-buffer
3341 (should (string= "foo" (python-info-current-defun)))
3342 (should (string= "def foo" (python-info-current-defun t)))
3344 (should (not (python-info-current-defun)))
3345 (indent-for-tab-command)
3346 (should (string= "foo" (python-info-current-defun)))
3347 (should (string= "def foo" (python-info-current-defun t)))))
3349 (ert-deftest python-info-current-defun-2
()
3350 (python-tests-with-temp-buffer
3356 return [i for i in range(3)]
3370 (should (string= "C" (python-info-current-defun)))
3371 (should (string= "class C" (python-info-current-defun t)))
3372 (python-tests-look-at "return [i for ")
3373 (should (string= "C.m" (python-info-current-defun)))
3374 (should (string= "def C.m" (python-info-current-defun t)))
3375 (python-tests-look-at "def b():")
3376 (should (string= "C.m.b" (python-info-current-defun)))
3377 (should (string= "def C.m.b" (python-info-current-defun t)))
3379 (indent-for-tab-command)
3380 (python-indent-dedent-line-backspace 1)
3381 (should (string= "C.m" (python-info-current-defun)))
3382 (should (string= "def C.m" (python-info-current-defun t)))
3383 (python-tests-look-at "def c(self):")
3385 (indent-for-tab-command)
3386 (should (string= "C.m.a" (python-info-current-defun)))
3387 (should (string= "def C.m.a" (python-info-current-defun t)))
3388 (python-indent-dedent-line-backspace 1)
3389 (should (string= "C.m" (python-info-current-defun)))
3390 (should (string= "def C.m" (python-info-current-defun t)))
3391 (python-indent-dedent-line-backspace 1)
3392 (should (string= "C" (python-info-current-defun)))
3393 (should (string= "class C" (python-info-current-defun t)))
3394 (python-tests-look-at "def c(self):")
3395 (should (string= "C.c" (python-info-current-defun)))
3396 (should (string= "def C.c" (python-info-current-defun t)))
3397 (python-tests-look-at "do_c()")
3398 (should (string= "C.c" (python-info-current-defun)))
3399 (should (string= "def C.c" (python-info-current-defun t)))))
3401 (ert-deftest python-info-current-defun-3
()
3402 (python-tests-with-temp-buffer
3404 def decoratorFunctionWithArguments(arg1, arg2, arg3):
3405 '''print decorated function call data to stdout.
3409 @decoratorFunctionWithArguments('arg1', 'arg2')
3410 def func(a, b, c=True):
3415 print 'Inside wwrap()'
3416 def wrapped_f(*args):
3417 print 'Inside wrapped_f()'
3418 print 'Decorator arguments:', arg1, arg2, arg3
3420 print 'After f(*args)'
3424 (python-tests-look-at "def wwrap(f):")
3426 (should (not (python-info-current-defun)))
3427 (indent-for-tab-command 1)
3428 (should (string= (python-info-current-defun)
3429 "decoratorFunctionWithArguments"))
3430 (should (string= (python-info-current-defun t)
3431 "def decoratorFunctionWithArguments"))
3432 (python-tests-look-at "def wrapped_f(*args):")
3433 (should (string= (python-info-current-defun)
3434 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
3435 (should (string= (python-info-current-defun t)
3436 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
3437 (python-tests-look-at "return wrapped_f")
3438 (should (string= (python-info-current-defun)
3439 "decoratorFunctionWithArguments.wwrap"))
3440 (should (string= (python-info-current-defun t)
3441 "def decoratorFunctionWithArguments.wwrap"))
3443 (python-tests-look-at "return wwrap")
3444 (should (string= (python-info-current-defun)
3445 "decoratorFunctionWithArguments"))
3446 (should (string= (python-info-current-defun t)
3447 "def decoratorFunctionWithArguments"))))
3449 (ert-deftest python-info-current-symbol-1
()
3450 (python-tests-with-temp-buffer
3460 (python-tests-look-at "self.c()")
3461 (should (string= "self.c" (python-info-current-symbol)))
3462 (should (string= "C.c" (python-info-current-symbol t
)))))
3464 (ert-deftest python-info-current-symbol-2
()
3465 (python-tests-with-temp-buffer
3477 (python-tests-look-at "self.c()")
3478 (should (string= "self.c" (python-info-current-symbol)))
3479 (should (string= "C.M.c" (python-info-current-symbol t
)))))
3481 (ert-deftest python-info-current-symbol-3
()
3482 "Keywords should not be considered symbols."
3483 :expected-result
:failed
3484 (python-tests-with-temp-buffer
3489 ;; FIXME: keywords are not symbols.
3490 (python-tests-look-at "class C")
3491 (should (not (python-info-current-symbol)))
3492 (should (not (python-info-current-symbol t
)))
3493 (python-tests-look-at "C(object)")
3494 (should (string= "C" (python-info-current-symbol)))
3495 (should (string= "class C" (python-info-current-symbol t
)))))
3497 (ert-deftest python-info-statement-starts-block-p-1
()
3498 (python-tests-with-temp-buffer
3500 def long_function_name(
3501 var_one, var_two, var_three,
3505 (python-tests-look-at "def long_function_name")
3506 (should (python-info-statement-starts-block-p))
3507 (python-tests-look-at "print (var_one)")
3508 (python-util-forward-comment -
1)
3509 (should (python-info-statement-starts-block-p))))
3511 (ert-deftest python-info-statement-starts-block-p-2
()
3512 (python-tests-with-temp-buffer
3514 if width == 0 and height == 0 and \\\\
3515 color == 'red' and emphasis == 'strong' or \\\\
3517 raise ValueError('sorry, you lose')
3519 (python-tests-look-at "if width == 0 and")
3520 (should (python-info-statement-starts-block-p))
3521 (python-tests-look-at "raise ValueError(")
3522 (python-util-forward-comment -
1)
3523 (should (python-info-statement-starts-block-p))))
3525 (ert-deftest python-info-statement-ends-block-p-1
()
3526 (python-tests-with-temp-buffer
3528 def long_function_name(
3529 var_one, var_two, var_three,
3533 (python-tests-look-at "print (var_one)")
3534 (should (python-info-statement-ends-block-p))))
3536 (ert-deftest python-info-statement-ends-block-p-2
()
3537 (python-tests-with-temp-buffer
3539 if width == 0 and height == 0 and \\\\
3540 color == 'red' and emphasis == 'strong' or \\\\
3547 (python-tests-look-at "raise ValueError(")
3548 (should (python-info-statement-ends-block-p))))
3550 (ert-deftest python-info-beginning-of-statement-p-1
()
3551 (python-tests-with-temp-buffer
3553 def long_function_name(
3554 var_one, var_two, var_three,
3558 (python-tests-look-at "def long_function_name")
3559 (should (python-info-beginning-of-statement-p))
3561 (should (not (python-info-beginning-of-statement-p)))
3562 (python-tests-look-at "print (var_one)")
3563 (should (python-info-beginning-of-statement-p))
3564 (goto-char (line-beginning-position))
3565 (should (not (python-info-beginning-of-statement-p)))))
3567 (ert-deftest python-info-beginning-of-statement-p-2
()
3568 (python-tests-with-temp-buffer
3570 if width == 0 and height == 0 and \\\\
3571 color == 'red' and emphasis == 'strong' or \\\\
3578 (python-tests-look-at "if width == 0 and")
3579 (should (python-info-beginning-of-statement-p))
3581 (should (not (python-info-beginning-of-statement-p)))
3582 (python-tests-look-at "raise ValueError(")
3583 (should (python-info-beginning-of-statement-p))
3584 (goto-char (line-beginning-position))
3585 (should (not (python-info-beginning-of-statement-p)))))
3587 (ert-deftest python-info-end-of-statement-p-1
()
3588 (python-tests-with-temp-buffer
3590 def long_function_name(
3591 var_one, var_two, var_three,
3595 (python-tests-look-at "def long_function_name")
3596 (should (not (python-info-end-of-statement-p)))
3598 (should (not (python-info-end-of-statement-p)))
3599 (python-tests-look-at "print (var_one)")
3600 (python-util-forward-comment -
1)
3601 (should (python-info-end-of-statement-p))
3602 (python-tests-look-at "print (var_one)")
3603 (should (not (python-info-end-of-statement-p)))
3605 (should (python-info-end-of-statement-p))))
3607 (ert-deftest python-info-end-of-statement-p-2
()
3608 (python-tests-with-temp-buffer
3610 if width == 0 and height == 0 and \\\\
3611 color == 'red' and emphasis == 'strong' or \\\\
3618 (python-tests-look-at "if width == 0 and")
3619 (should (not (python-info-end-of-statement-p)))
3621 (should (not (python-info-end-of-statement-p)))
3622 (python-tests-look-at "raise ValueError(")
3623 (python-util-forward-comment -
1)
3624 (should (python-info-end-of-statement-p))
3625 (python-tests-look-at "raise ValueError(")
3626 (should (not (python-info-end-of-statement-p)))
3628 (should (not (python-info-end-of-statement-p)))
3629 (goto-char (point-max))
3630 (python-util-forward-comment -
1)
3631 (should (python-info-end-of-statement-p))))
3633 (ert-deftest python-info-beginning-of-block-p-1
()
3634 (python-tests-with-temp-buffer
3636 def long_function_name(
3637 var_one, var_two, var_three,
3641 (python-tests-look-at "def long_function_name")
3642 (should (python-info-beginning-of-block-p))
3643 (python-tests-look-at "var_one, var_two, var_three,")
3644 (should (not (python-info-beginning-of-block-p)))
3645 (python-tests-look-at "print (var_one)")
3646 (should (not (python-info-beginning-of-block-p)))))
3648 (ert-deftest python-info-beginning-of-block-p-2
()
3649 (python-tests-with-temp-buffer
3651 if width == 0 and height == 0 and \\\\
3652 color == 'red' and emphasis == 'strong' or \\\\
3659 (python-tests-look-at "if width == 0 and")
3660 (should (python-info-beginning-of-block-p))
3661 (python-tests-look-at "color == 'red' and emphasis")
3662 (should (not (python-info-beginning-of-block-p)))
3663 (python-tests-look-at "raise ValueError(")
3664 (should (not (python-info-beginning-of-block-p)))))
3666 (ert-deftest python-info-end-of-block-p-1
()
3667 (python-tests-with-temp-buffer
3669 def long_function_name(
3670 var_one, var_two, var_three,
3674 (python-tests-look-at "def long_function_name")
3675 (should (not (python-info-end-of-block-p)))
3676 (python-tests-look-at "var_one, var_two, var_three,")
3677 (should (not (python-info-end-of-block-p)))
3678 (python-tests-look-at "var_four):")
3680 (should (not (python-info-end-of-block-p)))
3681 (python-tests-look-at "print (var_one)")
3682 (should (not (python-info-end-of-block-p)))
3684 (should (python-info-end-of-block-p))))
3686 (ert-deftest python-info-end-of-block-p-2
()
3687 (python-tests-with-temp-buffer
3689 if width == 0 and height == 0 and \\\\
3690 color == 'red' and emphasis == 'strong' or \\\\
3697 (python-tests-look-at "if width == 0 and")
3698 (should (not (python-info-end-of-block-p)))
3699 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3700 (should (not (python-info-end-of-block-p)))
3701 (python-tests-look-at "highlight > 100:")
3703 (should (not (python-info-end-of-block-p)))
3704 (python-tests-look-at "raise ValueError(")
3705 (should (not (python-info-end-of-block-p)))
3707 (should (not (python-info-end-of-block-p)))
3708 (goto-char (point-max))
3709 (python-util-forward-comment -
1)
3710 (should (python-info-end-of-block-p))))
3712 (ert-deftest python-info-dedenter-opening-block-position-1
()
3713 (python-tests-with-temp-buffer
3715 if request.user.is_authenticated():
3717 profile = request.user.get_profile()
3718 except Profile.DoesNotExist:
3719 profile = Profile.objects.create(user=request.user)
3722 profile.recalculate_stats()
3724 profile.clear_stats()
3729 (python-tests-look-at "try:")
3730 (should (not (python-info-dedenter-opening-block-position)))
3731 (python-tests-look-at "except Profile.DoesNotExist:")
3732 (should (= (python-tests-look-at "try:" -
1 t
)
3733 (python-info-dedenter-opening-block-position)))
3734 (python-tests-look-at "else:")
3735 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -
1 t
)
3736 (python-info-dedenter-opening-block-position)))
3737 (python-tests-look-at "if profile.stats:")
3738 (should (not (python-info-dedenter-opening-block-position)))
3739 (python-tests-look-at "else:")
3740 (should (= (python-tests-look-at "if profile.stats:" -
1 t
)
3741 (python-info-dedenter-opening-block-position)))
3742 (python-tests-look-at "finally:")
3743 (should (= (python-tests-look-at "else:" -
2 t
)
3744 (python-info-dedenter-opening-block-position)))))
3746 (ert-deftest python-info-dedenter-opening-block-position-2
()
3747 (python-tests-with-temp-buffer
3749 if request.user.is_authenticated():
3750 profile = Profile.objects.get_or_create(user=request.user)
3752 profile.recalculate_stats()
3759 (python-tests-look-at "'else': 'do it'")
3760 (should (not (python-info-dedenter-opening-block-position)))
3761 (python-tests-look-at "'else'")
3762 (should (not (python-info-dedenter-opening-block-position)))))
3764 (ert-deftest python-info-dedenter-opening-block-position-3
()
3765 (python-tests-with-temp-buffer
3771 msg = 'Error saving to disk'
3773 logger.exception(msg)
3776 logger.exception('Unhandled exception')
3781 (python-tests-look-at "try:")
3782 (should (not (python-info-dedenter-opening-block-position)))
3784 (python-tests-look-at "except IOError:")
3785 (should (= (python-tests-look-at "try:" -
1 t
)
3786 (python-info-dedenter-opening-block-position)))
3788 (python-tests-look-at "except Exception:")
3789 (should (= (python-tests-look-at "except IOError:" -
1 t
)
3790 (python-info-dedenter-opening-block-position)))
3792 (python-tests-look-at "if hide_details:")
3793 (should (not (python-info-dedenter-opening-block-position)))
3795 ;; check indentation modifies the detected opening block
3796 (python-tests-look-at "else")
3797 (should (= (python-tests-look-at "if hide_details:" -
1 t
)
3798 (python-info-dedenter-opening-block-position)))
3801 (should (= (python-tests-look-at "if hide_details:" -
1 t
)
3802 (python-info-dedenter-opening-block-position)))
3805 (should (= (python-tests-look-at "except Exception:" -
1 t
)
3806 (python-info-dedenter-opening-block-position)))
3809 (should (= (python-tests-look-at "if save:" -
1 t
)
3810 (python-info-dedenter-opening-block-position)))))
3812 (ert-deftest python-info-dedenter-opening-block-positions-1
()
3813 (python-tests-with-temp-buffer
3819 msg = 'Error saving to disk'
3821 logger.exception(msg)
3824 logger.exception('Unhandled exception')
3829 (python-tests-look-at "try:")
3830 (should (not (python-info-dedenter-opening-block-positions)))
3832 (python-tests-look-at "except IOError:")
3835 (python-tests-look-at "try:" -
1 t
))
3836 (python-info-dedenter-opening-block-positions)))
3838 (python-tests-look-at "except Exception:")
3841 (python-tests-look-at "except IOError:" -
1 t
))
3842 (python-info-dedenter-opening-block-positions)))
3844 (python-tests-look-at "if hide_details:")
3845 (should (not (python-info-dedenter-opening-block-positions)))
3847 ;; check indentation does not modify the detected opening blocks
3848 (python-tests-look-at "else")
3851 (python-tests-look-at "if hide_details:" -
1 t
)
3852 (python-tests-look-at "except Exception:" -
1 t
)
3853 (python-tests-look-at "if save:" -
1 t
))
3854 (python-info-dedenter-opening-block-positions)))
3859 (python-tests-look-at "if hide_details:" -
1 t
)
3860 (python-tests-look-at "except Exception:" -
1 t
)
3861 (python-tests-look-at "if save:" -
1 t
))
3862 (python-info-dedenter-opening-block-positions)))
3867 (python-tests-look-at "if hide_details:" -
1 t
)
3868 (python-tests-look-at "except Exception:" -
1 t
)
3869 (python-tests-look-at "if save:" -
1 t
))
3870 (python-info-dedenter-opening-block-positions)))
3875 (python-tests-look-at "if hide_details:" -
1 t
)
3876 (python-tests-look-at "except Exception:" -
1 t
)
3877 (python-tests-look-at "if save:" -
1 t
))
3878 (python-info-dedenter-opening-block-positions)))))
3880 (ert-deftest python-info-dedenter-opening-block-positions-2
()
3881 "Test detection of opening blocks for elif."
3882 (python-tests-with-temp-buffer
3891 (python-tests-look-at "elif var3:")
3894 (python-tests-look-at "if var2:" -
1 t
)
3895 (python-tests-look-at "if var:" -
1 t
))
3896 (python-info-dedenter-opening-block-positions)))
3898 (python-tests-look-at "elif\n")
3901 (python-tests-look-at "elif var3:" -
1 t
)
3902 (python-tests-look-at "if var:" -
1 t
))
3903 (python-info-dedenter-opening-block-positions)))))
3905 (ert-deftest python-info-dedenter-opening-block-positions-3
()
3906 "Test detection of opening blocks for else."
3907 (python-tests-with-temp-buffer
3929 (python-tests-look-at "else\n")
3932 (python-tests-look-at "elif var3:" -
1 t
)
3933 (python-tests-look-at "if var:" -
1 t
)
3934 (python-tests-look-at "except:" -
1 t
))
3935 (python-info-dedenter-opening-block-positions)))
3937 (python-tests-look-at "else\n")
3940 (python-tests-look-at "while var5:" -
1 t
)
3941 (python-tests-look-at "if var4:" -
1 t
))
3942 (python-info-dedenter-opening-block-positions)))
3944 (python-tests-look-at "else\n")
3947 (python-tests-look-at "if value > 0:" -
1 t
)
3948 (python-tests-look-at "for value in var6:" -
1 t
)
3949 (python-tests-look-at "if var4:" -
1 t
))
3950 (python-info-dedenter-opening-block-positions)))))
3952 (ert-deftest python-info-dedenter-opening-block-positions-4
()
3953 "Test detection of opening blocks for except."
3954 (python-tests-with-temp-buffer
3962 (python-tests-look-at "except ValueError:")
3964 (equal (list (python-tests-look-at "try:" -
1 t
))
3965 (python-info-dedenter-opening-block-positions)))
3967 (python-tests-look-at "except\n")
3969 (equal (list (python-tests-look-at "except ValueError:" -
1 t
))
3970 (python-info-dedenter-opening-block-positions)))))
3972 (ert-deftest python-info-dedenter-opening-block-positions-5
()
3973 "Test detection of opening blocks for finally."
3974 (python-tests-with-temp-buffer
3983 logger.exception('something went wrong')
3987 something_else_else()
3989 logger.exception('something else went wrong')
3994 (python-tests-look-at "finally\n")
3996 (equal (list (python-tests-look-at "try:" -
1 t
))
3997 (python-info-dedenter-opening-block-positions)))
3999 (python-tests-look-at "finally\n")
4001 (equal (list (python-tests-look-at "except:" -
1 t
))
4002 (python-info-dedenter-opening-block-positions)))
4004 (python-tests-look-at "finally\n")
4006 (equal (list (python-tests-look-at "else:" -
1 t
))
4007 (python-info-dedenter-opening-block-positions)))))
4009 (ert-deftest python-info-dedenter-opening-block-message-1
()
4010 "Test dedenters inside strings are ignored."
4011 (python-tests-with-temp-buffer
4016 logger.exception('something went wrong')
4019 (python-tests-look-at "except\n")
4020 (should (not (python-info-dedenter-opening-block-message)))))
4022 (ert-deftest python-info-dedenter-opening-block-message-2
()
4023 "Test except keyword."
4024 (python-tests-with-temp-buffer
4029 logger.exception('something went wrong')
4031 (python-tests-look-at "except:")
4034 (substring-no-properties
4035 (python-info-dedenter-opening-block-message))))
4039 (substring-no-properties
4040 (python-info-dedenter-opening-block-message))))))
4042 (ert-deftest python-info-dedenter-opening-block-message-3
()
4043 "Test else keyword."
4044 (python-tests-with-temp-buffer
4049 logger.exception('something went wrong')
4051 logger.debug('all good')
4053 (python-tests-look-at "else:")
4056 (substring-no-properties
4057 (python-info-dedenter-opening-block-message))))
4061 (substring-no-properties
4062 (python-info-dedenter-opening-block-message))))))
4064 (ert-deftest python-info-dedenter-opening-block-message-4
()
4065 "Test finally keyword."
4066 (python-tests-with-temp-buffer
4071 logger.exception('something went wrong')
4073 logger.debug('all good')
4077 (python-tests-look-at "finally:")
4080 (substring-no-properties
4081 (python-info-dedenter-opening-block-message))))
4085 (substring-no-properties
4086 (python-info-dedenter-opening-block-message))))))
4088 (ert-deftest python-info-dedenter-opening-block-message-5
()
4089 "Test elif keyword."
4090 (python-tests-with-temp-buffer
4096 (python-tests-look-at "elif b:")
4099 (substring-no-properties
4100 (python-info-dedenter-opening-block-message))))
4104 (substring-no-properties
4105 (python-info-dedenter-opening-block-message))))))
4108 (ert-deftest python-info-dedenter-statement-p-1
()
4109 "Test dedenters inside strings are ignored."
4110 (python-tests-with-temp-buffer
4115 logger.exception('something went wrong')
4118 (python-tests-look-at "except\n")
4119 (should (not (python-info-dedenter-statement-p)))))
4121 (ert-deftest python-info-dedenter-statement-p-2
()
4122 "Test except keyword."
4123 (python-tests-with-temp-buffer
4128 logger.exception('something went wrong')
4130 (python-tests-look-at "except:")
4131 (should (= (point) (python-info-dedenter-statement-p)))
4133 (should (= (save-excursion
4134 (back-to-indentation)
4136 (python-info-dedenter-statement-p)))))
4138 (ert-deftest python-info-dedenter-statement-p-3
()
4139 "Test else keyword."
4140 (python-tests-with-temp-buffer
4145 logger.exception('something went wrong')
4147 logger.debug('all good')
4149 (python-tests-look-at "else:")
4150 (should (= (point) (python-info-dedenter-statement-p)))
4152 (should (= (save-excursion
4153 (back-to-indentation)
4155 (python-info-dedenter-statement-p)))))
4157 (ert-deftest python-info-dedenter-statement-p-4
()
4158 "Test finally keyword."
4159 (python-tests-with-temp-buffer
4164 logger.exception('something went wrong')
4166 logger.debug('all good')
4170 (python-tests-look-at "finally:")
4171 (should (= (point) (python-info-dedenter-statement-p)))
4173 (should (= (save-excursion
4174 (back-to-indentation)
4176 (python-info-dedenter-statement-p)))))
4178 (ert-deftest python-info-dedenter-statement-p-5
()
4179 "Test elif keyword."
4180 (python-tests-with-temp-buffer
4186 (python-tests-look-at "elif b:")
4187 (should (= (point) (python-info-dedenter-statement-p)))
4189 (should (= (save-excursion
4190 (back-to-indentation)
4192 (python-info-dedenter-statement-p)))))
4194 (ert-deftest python-info-line-ends-backslash-p-1
()
4195 (python-tests-with-temp-buffer
4197 objects = Thing.objects.all() \\\\
4207 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
4208 (should (python-info-line-ends-backslash-p 3))
4209 (should (python-info-line-ends-backslash-p 4))
4210 (should (python-info-line-ends-backslash-p 5))
4211 (should (python-info-line-ends-backslash-p 6)) ; ) \...
4212 (should (python-info-line-ends-backslash-p 7))
4213 (should (python-info-line-ends-backslash-p 8))
4214 (should (python-info-line-ends-backslash-p 9))
4215 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
4217 (ert-deftest python-info-beginning-of-backslash-1
()
4218 (python-tests-with-temp-buffer
4220 objects = Thing.objects.all() \\\\
4231 (second (python-tests-look-at ".filter("))
4232 (third (python-tests-look-at ".aggregate(")))
4233 (should (= first
(python-info-beginning-of-backslash 2)))
4234 (should (= second
(python-info-beginning-of-backslash 3)))
4235 (should (= second
(python-info-beginning-of-backslash 4)))
4236 (should (= second
(python-info-beginning-of-backslash 5)))
4237 (should (= second
(python-info-beginning-of-backslash 6)))
4238 (should (= third
(python-info-beginning-of-backslash 7)))
4239 (should (= third
(python-info-beginning-of-backslash 8)))
4240 (should (= third
(python-info-beginning-of-backslash 9)))
4241 (should (not (python-info-beginning-of-backslash 10))))))
4243 (ert-deftest python-info-continuation-line-p-1
()
4244 (python-tests-with-temp-buffer
4246 if width == 0 and height == 0 and \\\\
4247 color == 'red' and emphasis == 'strong' or \\\\
4254 (python-tests-look-at "if width == 0 and height == 0 and")
4255 (should (not (python-info-continuation-line-p)))
4256 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
4257 (should (python-info-continuation-line-p))
4258 (python-tests-look-at "highlight > 100:")
4259 (should (python-info-continuation-line-p))
4260 (python-tests-look-at "raise ValueError(")
4261 (should (not (python-info-continuation-line-p)))
4262 (python-tests-look-at "'sorry, you lose'")
4263 (should (python-info-continuation-line-p))
4265 (should (python-info-continuation-line-p))
4266 (python-tests-look-at ")")
4267 (should (python-info-continuation-line-p))
4269 (should (not (python-info-continuation-line-p)))))
4271 (ert-deftest python-info-block-continuation-line-p-1
()
4272 (python-tests-with-temp-buffer
4274 if width == 0 and height == 0 and \\\\
4275 color == 'red' and emphasis == 'strong' or \\\\
4282 (python-tests-look-at "if width == 0 and")
4283 (should (not (python-info-block-continuation-line-p)))
4284 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
4285 (should (= (python-info-block-continuation-line-p)
4286 (python-tests-look-at "if width == 0 and" -
1 t
)))
4287 (python-tests-look-at "highlight > 100:")
4288 (should (not (python-info-block-continuation-line-p)))))
4290 (ert-deftest python-info-block-continuation-line-p-2
()
4291 (python-tests-with-temp-buffer
4298 (python-tests-look-at "def foo(a,")
4299 (should (not (python-info-block-continuation-line-p)))
4300 (python-tests-look-at "b,")
4301 (should (= (python-info-block-continuation-line-p)
4302 (python-tests-look-at "def foo(a," -
1 t
)))
4303 (python-tests-look-at "c):")
4304 (should (not (python-info-block-continuation-line-p)))))
4306 (ert-deftest python-info-assignment-statement-p-1
()
4307 (python-tests-with-temp-buffer
4309 data = foo(), bar() \\\\
4313 (python-tests-look-at "data = foo(), bar()")
4314 (should (python-info-assignment-statement-p))
4315 (should (python-info-assignment-statement-p t
))
4316 (python-tests-look-at "baz(), 4")
4317 (should (python-info-assignment-statement-p))
4318 (should (not (python-info-assignment-statement-p t
)))
4319 (python-tests-look-at "5, 6")
4320 (should (python-info-assignment-statement-p))
4321 (should (not (python-info-assignment-statement-p t
)))))
4323 (ert-deftest python-info-assignment-statement-p-2
()
4324 (python-tests-with-temp-buffer
4326 data = (foo(), bar()
4330 (python-tests-look-at "data = (foo(), bar()")
4331 (should (python-info-assignment-statement-p))
4332 (should (python-info-assignment-statement-p t
))
4333 (python-tests-look-at "baz(), 4")
4334 (should (python-info-assignment-statement-p))
4335 (should (not (python-info-assignment-statement-p t
)))
4336 (python-tests-look-at "5, 6)")
4337 (should (python-info-assignment-statement-p))
4338 (should (not (python-info-assignment-statement-p t
)))))
4340 (ert-deftest python-info-assignment-statement-p-3
()
4341 (python-tests-with-temp-buffer
4345 (python-tests-look-at "data '=' 42")
4346 (should (not (python-info-assignment-statement-p)))
4347 (should (not (python-info-assignment-statement-p t
)))))
4349 (ert-deftest python-info-assignment-continuation-line-p-1
()
4350 (python-tests-with-temp-buffer
4352 data = foo(), bar() \\\\
4356 (python-tests-look-at "data = foo(), bar()")
4357 (should (not (python-info-assignment-continuation-line-p)))
4358 (python-tests-look-at "baz(), 4")
4359 (should (= (python-info-assignment-continuation-line-p)
4360 (python-tests-look-at "foo()," -
1 t
)))
4361 (python-tests-look-at "5, 6")
4362 (should (not (python-info-assignment-continuation-line-p)))))
4364 (ert-deftest python-info-assignment-continuation-line-p-2
()
4365 (python-tests-with-temp-buffer
4367 data = (foo(), bar()
4371 (python-tests-look-at "data = (foo(), bar()")
4372 (should (not (python-info-assignment-continuation-line-p)))
4373 (python-tests-look-at "baz(), 4")
4374 (should (= (python-info-assignment-continuation-line-p)
4375 (python-tests-look-at "(foo()," -
1 t
)))
4376 (python-tests-look-at "5, 6)")
4377 (should (not (python-info-assignment-continuation-line-p)))))
4379 (ert-deftest python-info-looking-at-beginning-of-defun-1
()
4380 (python-tests-with-temp-buffer
4382 def decorat0r(deff):
4393 (python-tests-look-at "def decorat0r(deff):")
4394 (should (python-info-looking-at-beginning-of-defun))
4395 (python-tests-look-at "def foo(arg):")
4396 (should (not (python-info-looking-at-beginning-of-defun)))
4397 (python-tests-look-at "def wrap():")
4398 (should (python-info-looking-at-beginning-of-defun))
4399 (python-tests-look-at "deff()")
4400 (should (not (python-info-looking-at-beginning-of-defun)))))
4402 (ert-deftest python-info-current-line-comment-p-1
()
4403 (python-tests-with-temp-buffer
4406 foo = True # another comment
4410 print ('bar') # print bar
4412 (python-tests-look-at "# this is a comment")
4413 (should (python-info-current-line-comment-p))
4414 (python-tests-look-at "foo = True # another comment")
4415 (should (not (python-info-current-line-comment-p)))
4416 (python-tests-look-at "'#this is a string'")
4417 (should (not (python-info-current-line-comment-p)))
4418 (python-tests-look-at "# more comments")
4419 (should (python-info-current-line-comment-p))
4420 (python-tests-look-at "print ('bar') # print bar")
4421 (should (not (python-info-current-line-comment-p)))))
4423 (ert-deftest python-info-current-line-empty-p
()
4424 (python-tests-with-temp-buffer
4428 foo = True # another comment
4430 (should (python-info-current-line-empty-p))
4431 (python-tests-look-at "# this is a comment")
4432 (should (not (python-info-current-line-empty-p)))
4434 (should (python-info-current-line-empty-p))))
4436 (ert-deftest python-info-docstring-p-1
()
4437 "Test module docstring detection."
4438 (python-tests-with-temp-buffer
4439 "# -*- coding: utf-8 -*-
4443 Module Docstring Django style.
4445 u'''Additional module docstring.'''
4446 '''Not a module docstring.'''
4448 (python-tests-look-at "Module Docstring Django style.")
4449 (should (python-info-docstring-p))
4450 (python-tests-look-at "u'''Additional module docstring.'''")
4451 (should (python-info-docstring-p))
4452 (python-tests-look-at "'''Not a module docstring.'''")
4453 (should (not (python-info-docstring-p)))))
4455 (ert-deftest python-info-docstring-p-2
()
4456 "Test variable docstring detection."
4457 (python-tests-with-temp-buffer
4460 U'''Variable docstring.'''
4461 '''Additional variable docstring.'''
4462 '''Not a variable docstring.'''
4464 (python-tests-look-at "Variable docstring.")
4465 (should (python-info-docstring-p))
4466 (python-tests-look-at "u'''Additional variable docstring.'''")
4467 (should (python-info-docstring-p))
4468 (python-tests-look-at "'''Not a variable docstring.'''")
4469 (should (not (python-info-docstring-p)))))
4471 (ert-deftest python-info-docstring-p-3
()
4472 "Test function docstring detection."
4473 (python-tests-with-temp-buffer
4481 R'''Additional function docstring.'''
4482 '''Not a function docstring.'''
4485 (python-tests-look-at "Function docstring.")
4486 (should (python-info-docstring-p))
4487 (python-tests-look-at "R'''Additional function docstring.'''")
4488 (should (python-info-docstring-p))
4489 (python-tests-look-at "'''Not a function docstring.'''")
4490 (should (not (python-info-docstring-p)))))
4492 (ert-deftest python-info-docstring-p-4
()
4493 "Test class docstring detection."
4494 (python-tests-with-temp-buffer
4503 Additional class docstring.
4505 '''Not a class docstring.'''
4508 (python-tests-look-at "Class docstring.")
4509 (should (python-info-docstring-p))
4510 (python-tests-look-at "uR'''") ;; Additional class docstring
4511 (should (python-info-docstring-p))
4512 (python-tests-look-at "'''Not a class docstring.'''")
4513 (should (not (python-info-docstring-p)))))
4515 (ert-deftest python-info-docstring-p-5
()
4516 "Test class attribute docstring detection."
4517 (python-tests-with-temp-buffer
4522 Class attribute docstring.
4528 Additional class attribute docstring.
4530 '''Not a class attribute docstring.'''
4533 (python-tests-look-at "Class attribute docstring.")
4534 (should (python-info-docstring-p))
4535 (python-tests-look-at "UR'''") ;; Additional class attr docstring
4536 (should (python-info-docstring-p))
4537 (python-tests-look-at "'''Not a class attribute docstring.'''")
4538 (should (not (python-info-docstring-p)))))
4540 (ert-deftest python-info-docstring-p-6
()
4541 "Test class method docstring detection."
4542 (python-tests-with-temp-buffer
4546 def __init__(self, a, b):
4551 '''Method docstring.
4555 '''Additional method docstring.'''
4556 '''Not a method docstring.'''
4557 return self.a + self.b
4559 (python-tests-look-at "Method docstring.")
4560 (should (python-info-docstring-p))
4561 (python-tests-look-at "'''Additional method docstring.'''")
4562 (should (python-info-docstring-p))
4563 (python-tests-look-at "'''Not a method docstring.'''")
4564 (should (not (python-info-docstring-p)))))
4566 (ert-deftest python-info-encoding-from-cookie-1
()
4567 "Should detect it on first line."
4568 (python-tests-with-temp-buffer
4571 foo = True # another comment
4573 (should (eq (python-info-encoding-from-cookie) 'latin-1
))))
4575 (ert-deftest python-info-encoding-from-cookie-2
()
4576 "Should detect it on second line."
4577 (python-tests-with-temp-buffer
4581 foo = True # another comment
4583 (should (eq (python-info-encoding-from-cookie) 'latin-1
))))
4585 (ert-deftest python-info-encoding-from-cookie-3
()
4586 "Should not be detected on third line (and following ones)."
4587 (python-tests-with-temp-buffer
4591 foo = True # another comment
4593 (should (not (python-info-encoding-from-cookie)))))
4595 (ert-deftest python-info-encoding-from-cookie-4
()
4596 "Should detect Emacs style."
4597 (python-tests-with-temp-buffer
4598 "# -*- coding: latin-1 -*-
4600 foo = True # another comment"
4601 (should (eq (python-info-encoding-from-cookie) 'latin-1
))))
4603 (ert-deftest python-info-encoding-from-cookie-5
()
4604 "Should detect Vim style."
4605 (python-tests-with-temp-buffer
4606 "# vim: set fileencoding=latin-1 :
4608 foo = True # another comment"
4609 (should (eq (python-info-encoding-from-cookie) 'latin-1
))))
4611 (ert-deftest python-info-encoding-from-cookie-6
()
4612 "First cookie wins."
4613 (python-tests-with-temp-buffer
4614 "# -*- coding: iso-8859-1 -*-
4615 # vim: set fileencoding=latin-1 :
4617 foo = True # another comment"
4618 (should (eq (python-info-encoding-from-cookie) 'iso-8859-1
))))
4620 (ert-deftest python-info-encoding-from-cookie-7
()
4621 "First cookie wins."
4622 (python-tests-with-temp-buffer
4623 "# vim: set fileencoding=latin-1 :
4624 # -*- coding: iso-8859-1 -*-
4626 foo = True # another comment"
4627 (should (eq (python-info-encoding-from-cookie) 'latin-1
))))
4629 (ert-deftest python-info-encoding-1
()
4630 "Should return the detected encoding from cookie."
4631 (python-tests-with-temp-buffer
4632 "# vim: set fileencoding=latin-1 :
4634 foo = True # another comment"
4635 (should (eq (python-info-encoding) 'latin-1
))))
4637 (ert-deftest python-info-encoding-2
()
4638 "Should default to utf-8."
4639 (python-tests-with-temp-buffer
4640 "# No encoding for you
4642 foo = True # another comment"
4643 (should (eq (python-info-encoding) 'utf-8
))))
4646 ;;; Utility functions
4648 (ert-deftest python-util-goto-line-1
()
4649 (python-tests-with-temp-buffer
4654 pass" (make-string 20 ?
\n))
4655 (python-util-goto-line 10)
4656 (should (= (line-number-at-pos) 10))
4657 (python-util-goto-line 20)
4658 (should (= (line-number-at-pos) 20))))
4660 (ert-deftest python-util-clone-local-variables-1
()
4661 (let ((buffer (generate-new-buffer
4662 "python-util-clone-local-variables-1"))
4664 '((python-fill-docstring-style . django
)
4665 (python-shell-interpreter .
"python")
4666 (python-shell-interpreter-args .
"manage.py shell")
4667 (python-shell-prompt-regexp .
"In \\[[0-9]+\\]: ")
4668 (python-shell-prompt-output-regexp .
"Out\\[[0-9]+\\]: ")
4669 (python-shell-extra-pythonpaths "/home/user/pylib/")
4670 (python-shell-completion-setup-code
4671 .
"from IPython.core.completerlib import module_completion")
4672 (python-shell-completion-string-code
4673 .
"';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
4674 (python-shell-virtualenv-root
4675 .
"/home/user/.virtualenvs/project"))))
4676 (with-current-buffer buffer
4677 (kill-all-local-variables)
4678 (dolist (ccons varcons
)
4679 (set (make-local-variable (car ccons
)) (cdr ccons
))))
4680 (python-tests-with-temp-buffer
4682 (python-util-clone-local-variables buffer
)
4683 (dolist (ccons varcons
)
4685 (equal (symbol-value (car ccons
)) (cdr ccons
)))))
4686 (kill-buffer buffer
)))
4688 (ert-deftest python-util-strip-string-1
()
4689 (should (string= (python-util-strip-string "\t\r\n str") "str"))
4690 (should (string= (python-util-strip-string "str \n\r") "str"))
4691 (should (string= (python-util-strip-string "\t\r\n str \n\r ") "str"))
4693 (string= (python-util-strip-string "\n str \nin \tg \n\r") "str \nin \tg"))
4694 (should (string= (python-util-strip-string "\n \t \n\r ") ""))
4695 (should (string= (python-util-strip-string "") "")))
4697 (ert-deftest python-util-forward-comment-1
()
4698 (python-tests-with-temp-buffer
4702 # bad indented comment
4703 # more comments" (make-string 9999 ?
\n))
4704 (python-util-forward-comment 1)
4705 (should (= (point) (point-max)))
4706 (python-util-forward-comment -
1)
4707 (should (= (point) (point-min)))))
4709 (ert-deftest python-util-valid-regexp-p-1
()
4710 (should (python-util-valid-regexp-p ""))
4711 (should (python-util-valid-regexp-p python-shell-prompt-regexp
))
4712 (should (not (python-util-valid-regexp-p "\\("))))
4717 (ert-deftest python-parens-electric-indent-1
()
4718 (let ((eim electric-indent-mode
))
4721 (python-tests-with-temp-buffer
4723 from django.conf.urls import patterns, include, url
4725 from django.contrib import admin
4727 from myapp import views
4730 urlpatterns = patterns('',
4731 url(r'^$', views.index
4734 (electric-indent-mode 1)
4735 (python-tests-look-at "views.index")
4738 ;; Inserting commas within the same line should leave
4739 ;; indentation unchanged.
4740 (python-tests-self-insert ",")
4741 (should (= (current-indentation) 4))
4743 ;; As well as any other input happening within the same
4745 (python-tests-self-insert " name='index')")
4746 (should (= (current-indentation) 4))
4748 ;; But a comma outside it, should trigger indentation.
4749 (python-tests-self-insert ",")
4750 (should (= (current-indentation) 23))
4752 ;; Newline indents to the first argument column
4753 (python-tests-self-insert "\n")
4754 (should (= (current-indentation) 23))
4756 ;; All this input must not change indentation
4758 (python-tests-self-insert "url(r'^/login$', views.login)")
4759 (should (= (current-indentation) 4))
4761 ;; But this comma does
4762 (python-tests-self-insert ",")
4763 (should (= (current-indentation) 23))))
4764 (or eim
(electric-indent-mode -
1)))))
4766 (ert-deftest python-triple-quote-pairing
()
4767 (let ((epm electric-pair-mode
))
4770 (python-tests-with-temp-buffer
4772 (or epm
(electric-pair-mode 1))
4773 (goto-char (1- (point-max)))
4774 (python-tests-self-insert ?
\")
4775 (should (string= (buffer-string)
4777 (should (= (point) 4)))
4778 (python-tests-with-temp-buffer
4780 (python-tests-self-insert (list ?
\" ?
\" ?
\"))
4781 (should (string= (buffer-string)
4783 (should (= (point) 4)))
4784 (python-tests-with-temp-buffer
4786 (goto-char (1- (point-max)))
4787 (python-tests-self-insert ?
\")
4788 (should (= (point) (1- (point-max))))
4789 (should (string= (buffer-string)
4791 (or epm
(electric-pair-mode -
1)))))
4794 ;;; Hideshow support
4796 (ert-deftest python-hideshow-hide-levels-1
()
4797 "Should hide all methods when called after class start."
4798 (let ((enabled hs-minor-mode
))
4801 (python-tests-with-temp-buffer
4805 def __init__(self, arg, kwarg=1):
4809 def filter(self, nums):
4811 return item in [self.arg, self.kwarg]
4812 return filter(fn, nums)
4815 return '%s-%s' % (self.arg, self.kwarg)
4818 (python-tests-look-at "class SomeClass:")
4823 (python-tests-visible-string)
4827 def __init__(self, arg, kwarg=1):
4828 def filter(self, nums):
4829 def __str__(self):"))))
4830 (or enabled
(hs-minor-mode -
1)))))
4832 (ert-deftest python-hideshow-hide-levels-2
()
4833 "Should hide nested methods and parens at end of defun."
4834 (let ((enabled hs-minor-mode
))
4837 (python-tests-with-temp-buffer
4841 def __init__(self, arg, kwarg=1):
4845 def filter(self, nums):
4847 return item in [self.arg, self.kwarg]
4848 return filter(fn, nums)
4851 return '%s-%s' % (self.arg, self.kwarg)
4854 (python-tests-look-at "def fn(item):")
4858 (python-tests-visible-string)
4862 def __init__(self, arg, kwarg=1):
4866 def filter(self, nums):
4868 return filter(fn, nums)
4871 return '%s-%s' % (self.arg, self.kwarg)
4873 (or enabled
(hs-minor-mode -
1)))))
4877 (provide 'python-tests
)
4881 ;; indent-tabs-mode: nil
4884 ;;; python-tests.el ends here