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 (defmacro python-tests-with-temp-buffer (contents &rest body)
28 "Create a `python-mode' enabled temp buffer with CONTENTS.
29 BODY is code to be executed within the temp buffer. Point is
30 always located at the beginning of buffer."
31 (declare (indent 1) (debug t))
35 (goto-char (point-min))
38 (defmacro python-tests-with-temp-file (contents &rest body)
39 "Create a `python-mode' enabled file with CONTENTS.
40 BODY is code to be executed within the temp buffer. Point is
41 always located at the beginning of buffer."
42 (declare (indent 1) (debug t))
43 ;; temp-file never actually used for anything?
44 `(let* ((temp-file (make-temp-file "python-tests" nil ".py"))
45 (buffer (find-file-noselect temp-file)))
47 (with-current-buffer buffer
50 (goto-char (point-min))
52 (and buffer (kill-buffer buffer))
53 (delete-file temp-file))))
55 (defun python-tests-look-at (string &optional num restore-point)
56 "Move point at beginning of STRING in the current buffer.
57 Optional argument NUM defaults to 1 and is an integer indicating
58 how many occurrences must be found, when positive the search is
59 done forwards, otherwise backwards. When RESTORE-POINT is
60 non-nil the point is not moved but the position found is still
61 returned. When searching forward and point is already looking at
62 STRING, it is skipped so the next STRING occurrence is selected."
63 (let* ((num (or num 1))
64 (starting-point (point))
65 (string (regexp-quote string))
66 (search-fn (if (> num 0) #'re-search-forward #'re-search-backward))
67 (deinc-fn (if (> num 0) #'1- #'1+))
71 (while (not (= num 0))
74 ;; Moving forward and already looking at STRING, skip it.
75 (forward-char (length (match-string-no-properties 0))))
76 (and (not (funcall search-fn string nil t))
79 ;; `re-search-forward' leaves point at the end of the
80 ;; occurrence, move back so point is at the beginning
82 (forward-char (- (length (match-string-no-properties 0)))))
84 num (funcall deinc-fn num)
85 found-point (point))))
87 (and restore-point (goto-char starting-point)))))
89 (defun python-tests-self-insert (char-or-str)
90 "Call `self-insert-command' for chars in CHAR-OR-STR."
93 ((characterp char-or-str)
95 ((stringp char-or-str)
96 (string-to-list char-or-str))
98 (cl-remove-if #'characterp char-or-str))
100 (t (error "CHAR-OR-STR must be a char, string, or list of char")))))
103 (let ((last-command-event char))
104 (call-interactively 'self-insert-command)))
108 ;;; Tests for your tests, so you can test while you test.
110 (ert-deftest python-tests-look-at-1 ()
111 "Test forward movement."
112 (python-tests-with-temp-buffer
113 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
114 sed do eiusmod tempor incididunt ut labore et dolore magna
116 (let ((expected (save-excursion
118 (re-search-forward "et" nil t))
121 (should (= (python-tests-look-at "et" 3 t) expected))
122 ;; Even if NUM is bigger than found occurrences the point of last
123 ;; one should be returned.
124 (should (= (python-tests-look-at "et" 6 t) expected))
125 ;; If already looking at STRING, it should skip it.
126 (dotimes (i 2) (re-search-forward "et"))
128 (should (= (python-tests-look-at "et") expected)))))
130 (ert-deftest python-tests-look-at-2 ()
131 "Test backward movement."
132 (python-tests-with-temp-buffer
133 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
134 sed do eiusmod tempor incididunt ut labore et dolore magna
138 (re-search-forward "et" nil t)
142 (re-search-forward "et" nil t))
143 (should (= (python-tests-look-at "et" -3 t) expected))
144 (should (= (python-tests-look-at "et" -6 t) expected)))))
150 ;;; Python specialized rx
153 ;;; Font-lock and syntax
155 (ert-deftest python-syntax-after-python-backspace ()
156 ;; `python-indent-dedent-line-backspace' garbles syntax
157 :expected-result :failed
158 (python-tests-with-temp-buffer
160 (goto-char (point-max))
161 (python-indent-dedent-line-backspace 1)
162 (should (string= (buffer-string) "\"\""))
163 (should (null (nth 3 (syntax-ppss))))))
168 ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
170 (ert-deftest python-indent-pep8-1 ()
172 (python-tests-with-temp-buffer
173 "# Aligned with opening delimiter
174 foo = long_function_name(var_one, var_two,
177 (should (eq (car (python-indent-context)) :no-indent))
178 (should (= (python-indent-calculate-indentation) 0))
179 (python-tests-look-at "foo = long_function_name(var_one, var_two,")
180 (should (eq (car (python-indent-context)) :after-line))
181 (should (= (python-indent-calculate-indentation) 0))
182 (python-tests-look-at "var_three, var_four)")
183 (should (eq (car (python-indent-context)) :inside-paren))
184 (should (= (python-indent-calculate-indentation) 25))))
186 (ert-deftest python-indent-pep8-2 ()
188 (python-tests-with-temp-buffer
189 "# More indentation included to distinguish this from the rest.
190 def long_function_name(
191 var_one, var_two, var_three,
195 (should (eq (car (python-indent-context)) :no-indent))
196 (should (= (python-indent-calculate-indentation) 0))
197 (python-tests-look-at "def long_function_name(")
198 (should (eq (car (python-indent-context)) :after-line))
199 (should (= (python-indent-calculate-indentation) 0))
200 (python-tests-look-at "var_one, var_two, var_three,")
201 (should (eq (car (python-indent-context))
202 :inside-paren-newline-start-from-block))
203 (should (= (python-indent-calculate-indentation) 8))
204 (python-tests-look-at "var_four):")
205 (should (eq (car (python-indent-context))
206 :inside-paren-newline-start-from-block))
207 (should (= (python-indent-calculate-indentation) 8))
208 (python-tests-look-at "print (var_one)")
209 (should (eq (car (python-indent-context))
211 (should (= (python-indent-calculate-indentation) 4))))
213 (ert-deftest python-indent-pep8-3 ()
215 (python-tests-with-temp-buffer
216 "# Extra indentation is not necessary.
217 foo = long_function_name(
221 (should (eq (car (python-indent-context)) :no-indent))
222 (should (= (python-indent-calculate-indentation) 0))
223 (python-tests-look-at "foo = long_function_name(")
224 (should (eq (car (python-indent-context)) :after-line))
225 (should (= (python-indent-calculate-indentation) 0))
226 (python-tests-look-at "var_one, var_two,")
227 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
228 (should (= (python-indent-calculate-indentation) 4))
229 (python-tests-look-at "var_three, var_four)")
230 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
231 (should (= (python-indent-calculate-indentation) 4))))
233 (ert-deftest python-indent-base-case ()
234 "Check base case does not trigger errors."
235 (python-tests-with-temp-buffer
239 (goto-char (point-min))
240 (should (eq (car (python-indent-context)) :no-indent))
241 (should (= (python-indent-calculate-indentation) 0))
243 (should (eq (car (python-indent-context)) :after-line))
244 (should (= (python-indent-calculate-indentation) 0))
246 (should (eq (car (python-indent-context)) :after-line))
247 (should (= (python-indent-calculate-indentation) 0))))
249 (ert-deftest python-indent-after-comment-1 ()
250 "The most simple after-comment case that shouldn't fail."
251 (python-tests-with-temp-buffer
252 "# Contents will be modified to correct indentation
254 def _on_child_complete(self, child_future):
255 if self.in_terminal_state():
257 # We only complete when all our async children have entered a
258 # terminal state. At that point, if any child failed, we fail
259 # with the exception with which the first child failed.
261 (python-tests-look-at "# We only complete")
262 (should (eq (car (python-indent-context)) :after-block-end))
263 (should (= (python-indent-calculate-indentation) 8))
264 (python-tests-look-at "# terminal state")
265 (should (eq (car (python-indent-context)) :after-comment))
266 (should (= (python-indent-calculate-indentation) 8))
267 (python-tests-look-at "# with the exception")
268 (should (eq (car (python-indent-context)) :after-comment))
269 ;; This one indents relative to previous block, even given the fact
270 ;; that it was under-indented.
271 (should (= (python-indent-calculate-indentation) 4))
272 (python-tests-look-at "# terminal state" -1)
273 ;; It doesn't hurt to check again.
274 (should (eq (car (python-indent-context)) :after-comment))
276 (should (= (current-indentation) 8))
277 (python-tests-look-at "# with the exception")
278 (should (eq (car (python-indent-context)) :after-comment))
279 ;; Now everything should be lined up.
280 (should (= (python-indent-calculate-indentation) 8))))
282 (ert-deftest python-indent-after-comment-2 ()
283 "Test after-comment in weird cases."
284 (python-tests-with-temp-buffer
285 "# Contents will be modified to correct indentation
289 # This comment is badly indented just because.
290 # But we won't mess with the user in this line.
292 now_we_do_mess_cause_this_is_not_a_comment = 1
296 (python-tests-look-at "# I don't do much")
297 (should (eq (car (python-indent-context)) :after-block-start))
298 (should (= (python-indent-calculate-indentation) 4))
299 (python-tests-look-at "return arg")
300 ;; Comment here just gets ignored, this line is not a comment so
301 ;; the rules won't apply here.
302 (should (eq (car (python-indent-context)) :after-block-start))
303 (should (= (python-indent-calculate-indentation) 4))
304 (python-tests-look-at "# This comment is badly")
305 (should (eq (car (python-indent-context)) :after-block-end))
306 ;; The return keyword moves indentation backwards 4 spaces, but
307 ;; let's assume this comment was placed there because the user
308 ;; wanted to (manually adding spaces or whatever).
309 (should (= (python-indent-calculate-indentation) 0))
310 (python-tests-look-at "# but we won't mess")
311 (should (eq (car (python-indent-context)) :after-comment))
312 (should (= (python-indent-calculate-indentation) 4))
313 ;; Behave the same for blank lines: potentially a comment.
315 (should (eq (car (python-indent-context)) :after-comment))
316 (should (= (python-indent-calculate-indentation) 4))
317 (python-tests-look-at "now_we_do_mess")
318 ;; Here is where comment indentation starts to get ignored and
319 ;; where the user can't freely indent anymore.
320 (should (eq (car (python-indent-context)) :after-block-end))
321 (should (= (python-indent-calculate-indentation) 0))
322 (python-tests-look-at "# yeah, that.")
323 (should (eq (car (python-indent-context)) :after-line))
324 (should (= (python-indent-calculate-indentation) 0))))
326 (ert-deftest python-indent-inside-paren-1 ()
327 "The most simple inside-paren case that shouldn't fail."
328 (python-tests-with-temp-buffer
346 (python-tests-look-at "data = {")
347 (should (eq (car (python-indent-context)) :after-line))
348 (should (= (python-indent-calculate-indentation) 0))
349 (python-tests-look-at "'key':")
350 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
351 (should (= (python-indent-calculate-indentation) 4))
352 (python-tests-look-at "{")
353 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
354 (should (= (python-indent-calculate-indentation) 4))
355 (python-tests-look-at "'objlist': [")
356 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
357 (should (= (python-indent-calculate-indentation) 8))
358 (python-tests-look-at "{")
359 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
360 (should (= (python-indent-calculate-indentation) 12))
361 (python-tests-look-at "'pk': 1,")
362 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
363 (should (= (python-indent-calculate-indentation) 16))
364 (python-tests-look-at "'name': 'first',")
365 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
366 (should (= (python-indent-calculate-indentation) 16))
367 (python-tests-look-at "},")
368 (should (eq (car (python-indent-context))
369 :inside-paren-at-closing-nested-paren))
370 (should (= (python-indent-calculate-indentation) 12))
371 (python-tests-look-at "{")
372 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
373 (should (= (python-indent-calculate-indentation) 12))
374 (python-tests-look-at "'pk': 2,")
375 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
376 (should (= (python-indent-calculate-indentation) 16))
377 (python-tests-look-at "'name': 'second',")
378 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
379 (should (= (python-indent-calculate-indentation) 16))
380 (python-tests-look-at "}")
381 (should (eq (car (python-indent-context))
382 :inside-paren-at-closing-nested-paren))
383 (should (= (python-indent-calculate-indentation) 12))
384 (python-tests-look-at "]")
385 (should (eq (car (python-indent-context))
386 :inside-paren-at-closing-nested-paren))
387 (should (= (python-indent-calculate-indentation) 8))
388 (python-tests-look-at "}")
389 (should (eq (car (python-indent-context))
390 :inside-paren-at-closing-nested-paren))
391 (should (= (python-indent-calculate-indentation) 4))
392 (python-tests-look-at "}")
393 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
394 (should (= (python-indent-calculate-indentation) 0))))
396 (ert-deftest python-indent-inside-paren-2 ()
397 "Another more compact paren group style."
398 (python-tests-with-temp-buffer
409 (python-tests-look-at "data = {")
410 (should (eq (car (python-indent-context)) :after-line))
411 (should (= (python-indent-calculate-indentation) 0))
412 (python-tests-look-at "'objlist': [")
413 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
414 (should (= (python-indent-calculate-indentation) 4))
415 (python-tests-look-at "{'pk': 1,")
416 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
417 (should (= (python-indent-calculate-indentation) 8))
418 (python-tests-look-at "'name': 'first'},")
419 (should (eq (car (python-indent-context)) :inside-paren))
420 (should (= (python-indent-calculate-indentation) 9))
421 (python-tests-look-at "{'pk': 2,")
422 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
423 (should (= (python-indent-calculate-indentation) 8))
424 (python-tests-look-at "'name': 'second'}")
425 (should (eq (car (python-indent-context)) :inside-paren))
426 (should (= (python-indent-calculate-indentation) 9))
427 (python-tests-look-at "]")
428 (should (eq (car (python-indent-context))
429 :inside-paren-at-closing-nested-paren))
430 (should (= (python-indent-calculate-indentation) 4))
431 (python-tests-look-at "}}")
432 (should (eq (car (python-indent-context))
433 :inside-paren-at-closing-nested-paren))
434 (should (= (python-indent-calculate-indentation) 0))
435 (python-tests-look-at "}")
436 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
437 (should (= (python-indent-calculate-indentation) 0))))
439 (ert-deftest python-indent-inside-paren-3 ()
440 "The simplest case possible."
441 (python-tests-with-temp-buffer
448 (python-tests-look-at "data = ('these',")
449 (should (eq (car (python-indent-context)) :after-line))
450 (should (= (python-indent-calculate-indentation) 0))
452 (should (eq (car (python-indent-context)) :inside-paren))
453 (should (= (python-indent-calculate-indentation) 8))
455 (should (eq (car (python-indent-context)) :inside-paren))
456 (should (= (python-indent-calculate-indentation) 8))
458 (should (eq (car (python-indent-context)) :inside-paren))
459 (should (= (python-indent-calculate-indentation) 8))))
461 (ert-deftest python-indent-inside-paren-4 ()
462 "Respect indentation of first column."
463 (python-tests-with-temp-buffer
465 data = [ [ 'these', 'are'],
468 (python-tests-look-at "data = [ [ 'these', 'are'],")
469 (should (eq (car (python-indent-context)) :after-line))
470 (should (= (python-indent-calculate-indentation) 0))
472 (should (eq (car (python-indent-context)) :inside-paren))
473 (should (= (python-indent-calculate-indentation) 9))))
475 (ert-deftest python-indent-inside-paren-5 ()
476 "Test when :inside-paren initial parens are skipped in context start."
477 (python-tests-with-temp-buffer
479 while ((not some_condition) and
481 do_something_interesting(
484 (python-tests-look-at "while ((not some_condition) and")
485 (should (eq (car (python-indent-context)) :after-line))
486 (should (= (python-indent-calculate-indentation) 0))
488 (should (eq (car (python-indent-context)) :inside-paren))
489 (should (= (python-indent-calculate-indentation) 7))
491 (should (eq (car (python-indent-context)) :after-block-start))
492 (should (= (python-indent-calculate-indentation) 4))
494 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
495 (should (= (python-indent-calculate-indentation) 8))))
497 (ert-deftest python-indent-inside-paren-6 ()
498 "This should be aligned.."
499 (python-tests-with-temp-buffer
501 CHOICES = (('some', 'choice'),
502 ('another', 'choice'),
505 (python-tests-look-at "CHOICES = (('some', 'choice'),")
506 (should (eq (car (python-indent-context)) :after-line))
507 (should (= (python-indent-calculate-indentation) 0))
509 (should (eq (car (python-indent-context)) :inside-paren))
510 (should (= (python-indent-calculate-indentation) 11))
512 (should (eq (car (python-indent-context)) :inside-paren))
513 (should (= (python-indent-calculate-indentation) 11))))
515 (ert-deftest python-indent-after-block-1 ()
516 "The most simple after-block case that shouldn't fail."
517 (python-tests-with-temp-buffer
519 def foo(a, b, c=True):
521 (should (eq (car (python-indent-context)) :no-indent))
522 (should (= (python-indent-calculate-indentation) 0))
523 (goto-char (point-max))
524 (should (eq (car (python-indent-context)) :after-block-start))
525 (should (= (python-indent-calculate-indentation) 4))))
527 (ert-deftest python-indent-after-block-2 ()
528 "A weird (malformed) multiline block statement."
529 (python-tests-with-temp-buffer
535 (goto-char (point-max))
536 (should (eq (car (python-indent-context)) :after-block-start))
537 (should (= (python-indent-calculate-indentation) 4))))
539 (ert-deftest python-indent-after-block-3 ()
540 "A weird (malformed) sample, usually found in python shells."
541 (python-tests-with-temp-buffer
550 (python-tests-look-at "pass")
551 (should (eq (car (python-indent-context)) :after-block-start))
552 (should (= (python-indent-calculate-indentation) 4))
553 (python-tests-look-at "something")
555 (should (eq (car (python-indent-context)) :after-line))
556 (should (= (python-indent-calculate-indentation) 0))))
558 (ert-deftest python-indent-after-backslash-1 ()
559 "The most common case."
560 (python-tests-with-temp-buffer
562 from foo.bar.baz import something, something_1 \\\\
563 something_2 something_3, \\\\
564 something_4, something_5
566 (python-tests-look-at "from foo.bar.baz import something, something_1")
567 (should (eq (car (python-indent-context)) :after-line))
568 (should (= (python-indent-calculate-indentation) 0))
569 (python-tests-look-at "something_2 something_3,")
570 (should (eq (car (python-indent-context)) :after-backslash-first-line))
571 (should (= (python-indent-calculate-indentation) 4))
572 (python-tests-look-at "something_4, something_5")
573 (should (eq (car (python-indent-context)) :after-backslash))
574 (should (= (python-indent-calculate-indentation) 4))
575 (goto-char (point-max))
576 (should (eq (car (python-indent-context)) :after-line))
577 (should (= (python-indent-calculate-indentation) 0))))
579 (ert-deftest python-indent-after-backslash-2 ()
580 "A pretty extreme complicated case."
581 (python-tests-with-temp-buffer
583 objects = Thing.objects.all() \\\\
593 (python-tests-look-at "objects = Thing.objects.all()")
594 (should (eq (car (python-indent-context)) :after-line))
595 (should (= (python-indent-calculate-indentation) 0))
596 (python-tests-look-at ".filter(")
597 (should (eq (car (python-indent-context))
598 :after-backslash-dotted-continuation))
599 (should (= (python-indent-calculate-indentation) 23))
600 (python-tests-look-at "type='toy',")
601 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
602 (should (= (python-indent-calculate-indentation) 27))
603 (python-tests-look-at "status='bought'")
604 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
605 (should (= (python-indent-calculate-indentation) 27))
606 (python-tests-look-at ") \\\\")
607 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
608 (should (= (python-indent-calculate-indentation) 23))
609 (python-tests-look-at ".aggregate(")
610 (should (eq (car (python-indent-context))
611 :after-backslash-dotted-continuation))
612 (should (= (python-indent-calculate-indentation) 23))
613 (python-tests-look-at "Sum('amount')")
614 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
615 (should (= (python-indent-calculate-indentation) 27))
616 (python-tests-look-at ") \\\\")
617 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
618 (should (= (python-indent-calculate-indentation) 23))
619 (python-tests-look-at ".values_list()")
620 (should (eq (car (python-indent-context))
621 :after-backslash-dotted-continuation))
622 (should (= (python-indent-calculate-indentation) 23))
624 (should (eq (car (python-indent-context)) :after-line))
625 (should (= (python-indent-calculate-indentation) 0))))
627 (ert-deftest python-indent-after-backslash-3 ()
628 "Backslash continuation from block start."
629 (python-tests-with-temp-buffer
631 with open('/path/to/some/file/you/want/to/read') as file_1, \\\\
632 open('/path/to/some/file/being/written', 'w') as file_2:
633 file_2.write(file_1.read())
635 (python-tests-look-at
636 "with open('/path/to/some/file/you/want/to/read') as file_1, \\\\")
637 (should (eq (car (python-indent-context)) :after-line))
638 (should (= (python-indent-calculate-indentation) 0))
639 (python-tests-look-at
640 "open('/path/to/some/file/being/written', 'w') as file_2")
641 (should (eq (car (python-indent-context))
642 :after-backslash-block-continuation))
643 (should (= (python-indent-calculate-indentation) 5))
644 (python-tests-look-at "file_2.write(file_1.read())")
645 (should (eq (car (python-indent-context)) :after-block-start))
646 (should (= (python-indent-calculate-indentation) 4))))
648 (ert-deftest python-indent-after-backslash-4 ()
649 "Backslash continuation from assignment."
650 (python-tests-with-temp-buffer
652 super_awful_assignment = some_calculation() and \\\\
653 another_calculation() and \\\\
654 some_final_calculation()
656 (python-tests-look-at
657 "super_awful_assignment = some_calculation() and \\\\")
658 (should (eq (car (python-indent-context)) :after-line))
659 (should (= (python-indent-calculate-indentation) 0))
660 (python-tests-look-at "another_calculation() and \\\\")
661 (should (eq (car (python-indent-context))
662 :after-backslash-assignment-continuation))
663 (should (= (python-indent-calculate-indentation) 25))
664 (python-tests-look-at "some_final_calculation()")
665 (should (eq (car (python-indent-context)) :after-backslash))
666 (should (= (python-indent-calculate-indentation) 25))))
668 (ert-deftest python-indent-after-backslash-5 ()
669 "Dotted continuation bizarre example."
670 (python-tests-with-temp-buffer
672 def delete_all_things():
677 (python-tests-look-at "Thing \\\\")
678 (should (eq (car (python-indent-context)) :after-block-start))
679 (should (= (python-indent-calculate-indentation) 4))
680 (python-tests-look-at ".objects.all() \\\\")
681 (should (eq (car (python-indent-context)) :after-backslash-first-line))
682 (should (= (python-indent-calculate-indentation) 8))
683 (python-tests-look-at ".delete()")
684 (should (eq (car (python-indent-context))
685 :after-backslash-dotted-continuation))
686 (should (= (python-indent-calculate-indentation) 16))))
688 (ert-deftest python-indent-block-enders-1 ()
689 "Test de-indentation for pass keyword."
690 (python-tests-with-temp-buffer
703 (python-tests-look-at "3)")
705 (should (= (python-indent-calculate-indentation) 8))
706 (python-tests-look-at "pass")
708 (should (eq (car (python-indent-context)) :after-block-end))
709 (should (= (python-indent-calculate-indentation) 8))))
711 (ert-deftest python-indent-block-enders-2 ()
712 "Test de-indentation for return keyword."
713 (python-tests-with-temp-buffer
716 '''raise lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
718 eiusmod tempor incididunt ut labore et dolore magna aliqua.
721 \"return (1, 2, 3).\"
727 (python-tests-look-at "def")
728 (should (= (python-indent-calculate-indentation) 4))
729 (python-tests-look-at "if")
730 (should (= (python-indent-calculate-indentation) 8))
731 (python-tests-look-at "return")
732 (should (= (python-indent-calculate-indentation) 12))
733 (goto-char (point-max))
734 (should (eq (car (python-indent-context)) :after-block-end))
735 (should (= (python-indent-calculate-indentation) 8))))
737 (ert-deftest python-indent-block-enders-3 ()
738 "Test de-indentation for continue keyword."
739 (python-tests-with-temp-buffer
745 (python-tests-look-at "if")
746 (should (= (python-indent-calculate-indentation) 4))
747 (python-tests-look-at "continue")
748 (should (= (python-indent-calculate-indentation) 8))
750 (should (eq (car (python-indent-context)) :after-block-end))
751 (should (= (python-indent-calculate-indentation) 4))))
753 (ert-deftest python-indent-block-enders-4 ()
754 "Test de-indentation for break keyword."
755 (python-tests-with-temp-buffer
761 (python-tests-look-at "if")
762 (should (= (python-indent-calculate-indentation) 4))
763 (python-tests-look-at "break")
764 (should (= (python-indent-calculate-indentation) 8))
766 (should (eq (car (python-indent-context)) :after-block-end))
767 (should (= (python-indent-calculate-indentation) 4))))
769 (ert-deftest python-indent-block-enders-5 ()
770 "Test de-indentation for raise keyword."
771 (python-tests-with-temp-buffer
775 raise ValueError('Element cannot be None')
777 (python-tests-look-at "if")
778 (should (= (python-indent-calculate-indentation) 4))
779 (python-tests-look-at "raise")
780 (should (= (python-indent-calculate-indentation) 8))
782 (should (eq (car (python-indent-context)) :after-block-end))
783 (should (= (python-indent-calculate-indentation) 4))))
785 (ert-deftest python-indent-dedenters-1 ()
786 "Test de-indentation for the elif keyword."
787 (python-tests-with-temp-buffer
796 (python-tests-look-at "elif\n")
797 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
798 (should (= (python-indent-calculate-indentation) 0))
799 (should (= (python-indent-calculate-indentation t) 0))))
801 (ert-deftest python-indent-dedenters-2 ()
802 "Test de-indentation for the else keyword."
803 (python-tests-with-temp-buffer
809 msg = 'Error saving to disk'
811 logger.exception(msg)
814 logger.exception('Unhandled exception')
819 (python-tests-look-at "else\n")
820 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
821 (should (= (python-indent-calculate-indentation) 8))
822 (python-indent-line t)
823 (should (= (python-indent-calculate-indentation t) 4))
824 (python-indent-line t)
825 (should (= (python-indent-calculate-indentation t) 0))
826 (python-indent-line t)
827 (should (= (python-indent-calculate-indentation t) 8))))
829 (ert-deftest python-indent-dedenters-3 ()
830 "Test de-indentation for the except keyword."
831 (python-tests-with-temp-buffer
838 (python-tests-look-at "except\n")
839 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
840 (should (= (python-indent-calculate-indentation) 4))
841 (python-indent-line t)
842 (should (= (python-indent-calculate-indentation t) 4))))
844 (ert-deftest python-indent-dedenters-4 ()
845 "Test de-indentation for the finally keyword."
846 (python-tests-with-temp-buffer
853 (python-tests-look-at "finally\n")
854 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
855 (should (= (python-indent-calculate-indentation) 4))
856 (python-indent-line t)
857 (should (= (python-indent-calculate-indentation) 4))))
859 (ert-deftest python-indent-dedenters-5 ()
860 "Test invalid levels are skipped in a complex example."
861 (python-tests-with-temp-buffer
867 msg = 'Error saving to disk'
869 logger.exception(msg)
875 (python-tests-look-at "else\n")
876 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
877 (should (= (python-indent-calculate-indentation) 8))
878 (should (= (python-indent-calculate-indentation t) 0))
879 (python-indent-line t)
880 (should (= (python-indent-calculate-indentation t) 8))))
882 (ert-deftest python-indent-dedenters-6 ()
883 "Test indentation is zero when no opening block for dedenter."
884 (python-tests-with-temp-buffer
891 (python-tests-look-at "else\n")
892 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
893 (should (= (python-indent-calculate-indentation) 0))
894 (should (= (python-indent-calculate-indentation t) 0))))
896 (ert-deftest python-indent-dedenters-7 ()
897 "Test indentation case from Bug#15163."
898 (python-tests-with-temp-buffer
907 (python-tests-look-at "else:" 2)
908 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
909 (should (= (python-indent-calculate-indentation) 0))
910 (should (= (python-indent-calculate-indentation t) 0))))
912 (ert-deftest python-indent-dedenters-8 ()
913 "Test indentation for Bug#18432."
914 (python-tests-with-temp-buffer
922 (python-tests-look-at "elif (a == 3 or")
923 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
924 (should (= (python-indent-calculate-indentation) 0))
925 (should (= (python-indent-calculate-indentation t) 0))
926 (python-tests-look-at "a == 4):\n")
927 (should (eq (car (python-indent-context)) :inside-paren))
928 (should (= (python-indent-calculate-indentation) 6))
930 (should (= (python-indent-calculate-indentation t) 4))
931 (python-indent-line t)
932 (should (= (python-indent-calculate-indentation t) 0))
933 (python-indent-line t)
934 (should (= (python-indent-calculate-indentation t) 6))))
936 (ert-deftest python-indent-inside-string-1 ()
937 "Test indentation for strings."
938 (python-tests-with-temp-buffer
946 (python-tests-look-at "multiline = '''")
947 (should (eq (car (python-indent-context)) :after-line))
948 (should (= (python-indent-calculate-indentation) 0))
949 (python-tests-look-at "bunch")
950 (should (eq (car (python-indent-context)) :inside-string))
951 (should (= (python-indent-calculate-indentation) 0))
952 (python-tests-look-at "of")
953 (should (eq (car (python-indent-context)) :inside-string))
954 (should (= (python-indent-calculate-indentation) 0))
955 (python-tests-look-at "lines")
956 (should (eq (car (python-indent-context)) :inside-string))
957 (should (= (python-indent-calculate-indentation) 0))
958 (python-tests-look-at "'''")
959 (should (eq (car (python-indent-context)) :inside-string))
960 (should (= (python-indent-calculate-indentation) 0))))
962 (ert-deftest python-indent-inside-string-2 ()
963 "Test indentation for docstrings."
964 (python-tests-with-temp-buffer
966 def fn(a, b, c=True):
973 (python-tests-look-at "'''docstring")
974 (should (eq (car (python-indent-context)) :after-block-start))
975 (should (= (python-indent-calculate-indentation) 4))
976 (python-tests-look-at "bunch")
977 (should (eq (car (python-indent-context)) :inside-string))
978 (should (= (python-indent-calculate-indentation) 4))
979 (python-tests-look-at "of")
980 (should (eq (car (python-indent-context)) :inside-string))
981 (should (= (python-indent-calculate-indentation) 4))
982 (python-tests-look-at "lines")
983 (should (eq (car (python-indent-context)) :inside-string))
984 (should (= (python-indent-calculate-indentation) 4))
985 (python-tests-look-at "'''")
986 (should (eq (car (python-indent-context)) :inside-string))
987 (should (= (python-indent-calculate-indentation) 4))))
989 (ert-deftest python-indent-inside-string-3 ()
990 "Test indentation for nested strings."
991 (python-tests-with-temp-buffer
993 def fn(a, b, c=True):
1000 (python-tests-look-at "some_var = '''")
1001 (should (eq (car (python-indent-context)) :after-block-start))
1002 (should (= (python-indent-calculate-indentation) 4))
1003 (python-tests-look-at "bunch")
1004 (should (eq (car (python-indent-context)) :inside-string))
1005 (should (= (python-indent-calculate-indentation) 4))
1006 (python-tests-look-at "of")
1007 (should (eq (car (python-indent-context)) :inside-string))
1008 (should (= (python-indent-calculate-indentation) 4))
1009 (python-tests-look-at "lines")
1010 (should (eq (car (python-indent-context)) :inside-string))
1011 (should (= (python-indent-calculate-indentation) 4))
1012 (python-tests-look-at "'''")
1013 (should (eq (car (python-indent-context)) :inside-string))
1014 (should (= (python-indent-calculate-indentation) 4))))
1016 (ert-deftest python-indent-electric-colon-1 ()
1017 "Test indentation case from Bug#18228."
1018 (python-tests-with-temp-buffer
1025 (python-tests-look-at "def b()")
1026 (goto-char (line-end-position))
1027 (python-tests-self-insert ":")
1028 (should (= (current-indentation) 0))))
1030 (ert-deftest python-indent-electric-colon-2 ()
1031 "Test indentation case for dedenter."
1032 (python-tests-with-temp-buffer
1038 (python-tests-look-at "else")
1039 (goto-char (line-end-position))
1040 (python-tests-self-insert ":")
1041 (should (= (current-indentation) 0))))
1043 (ert-deftest python-indent-electric-colon-3 ()
1044 "Test indentation case for multi-line dedenter."
1045 (python-tests-with-temp-buffer
1053 (python-tests-look-at "that)")
1054 (goto-char (line-end-position))
1055 (python-tests-self-insert ":")
1056 (python-tests-look-at "elif" -1)
1057 (should (= (current-indentation) 0))
1058 (python-tests-look-at "and")
1059 (should (= (current-indentation) 6))
1060 (python-tests-look-at "that)")
1061 (should (= (current-indentation) 6))))
1063 (ert-deftest python-indent-region-1 ()
1064 "Test indentation case from Bug#18843."
1072 (python-tests-with-temp-buffer
1074 (python-indent-region (point-min) (point-max))
1075 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1078 (ert-deftest python-indent-region-2 ()
1079 "Test region indentation on comments."
1089 (python-tests-with-temp-buffer
1091 (python-indent-region (point-min) (point-max))
1092 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1095 (ert-deftest python-indent-region-3 ()
1096 "Test region indentation on comments."
1113 (python-tests-with-temp-buffer
1115 (python-indent-region (point-min) (point-max))
1116 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1119 (ert-deftest python-indent-region-4 ()
1120 "Test region indentation block starts, dedenters and enders."
1137 (python-tests-with-temp-buffer
1139 (python-indent-region (point-min) (point-max))
1140 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1143 (ert-deftest python-indent-region-5 ()
1144 "Test region indentation leaves strings untouched (start delimiter)."
1161 (python-tests-with-temp-buffer
1163 (python-indent-region (point-min) (point-max))
1164 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1170 (ert-deftest python-nav-beginning-of-defun-1 ()
1171 (python-tests-with-temp-buffer
1173 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1174 '''print decorated function call data to stdout.
1178 @decoratorFunctionWithArguments('arg1', 'arg2')
1179 def func(a, b, c=True):
1184 print 'Inside wwrap()'
1185 def wrapped_f(*args):
1186 print 'Inside wrapped_f()'
1187 print 'Decorator arguments:', arg1, arg2, arg3
1189 print 'After f(*args)'
1193 (python-tests-look-at "return wrap")
1194 (should (= (save-excursion
1195 (python-nav-beginning-of-defun)
1198 (python-tests-look-at "def wrapped_f(*args):" -1)
1201 (python-tests-look-at "def wrapped_f(*args):" -1)
1202 (should (= (save-excursion
1203 (python-nav-beginning-of-defun)
1206 (python-tests-look-at "def wwrap(f):" -1)
1209 (python-tests-look-at "def wwrap(f):" -1)
1210 (should (= (save-excursion
1211 (python-nav-beginning-of-defun)
1214 (python-tests-look-at "def decoratorFunctionWithArguments" -1)
1218 (ert-deftest python-nav-beginning-of-defun-2 ()
1219 (python-tests-with-temp-buffer
1235 ;; Nested defuns, are handled with care.
1236 (python-tests-look-at "def c(self):")
1237 (should (= (save-excursion
1238 (python-nav-beginning-of-defun)
1241 (python-tests-look-at "def m(self):" -1)
1244 ;; Defuns on same levels should be respected.
1245 (python-tests-look-at "def a():" -1)
1246 (should (= (save-excursion
1247 (python-nav-beginning-of-defun)
1250 (python-tests-look-at "def b():" -1)
1253 ;; Jump to a top level defun.
1254 (python-tests-look-at "def b():" -1)
1255 (should (= (save-excursion
1256 (python-nav-beginning-of-defun)
1259 (python-tests-look-at "def m(self):" -1)
1262 ;; Jump to a top level defun again.
1263 (python-tests-look-at "def m(self):" -1)
1264 (should (= (save-excursion
1265 (python-nav-beginning-of-defun)
1268 (python-tests-look-at "class C(object):" -1)
1272 (ert-deftest python-nav-end-of-defun-1 ()
1273 (python-tests-with-temp-buffer
1289 (should (= (save-excursion
1290 (python-tests-look-at "class C(object):")
1291 (python-nav-end-of-defun)
1295 (should (= (save-excursion
1296 (python-tests-look-at "def m(self):")
1297 (python-nav-end-of-defun)
1300 (python-tests-look-at "def c(self):")
1303 (should (= (save-excursion
1304 (python-tests-look-at "def b():")
1305 (python-nav-end-of-defun)
1308 (python-tests-look-at "def b():")
1311 (should (= (save-excursion
1312 (python-tests-look-at "def c(self):")
1313 (python-nav-end-of-defun)
1318 (ert-deftest python-nav-end-of-defun-2 ()
1319 (python-tests-with-temp-buffer
1321 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1322 '''print decorated function call data to stdout.
1326 @decoratorFunctionWithArguments('arg1', 'arg2')
1327 def func(a, b, c=True):
1332 print 'Inside wwrap()'
1333 def wrapped_f(*args):
1334 print 'Inside wrapped_f()'
1335 print 'Decorator arguments:', arg1, arg2, arg3
1337 print 'After f(*args)'
1341 (should (= (save-excursion
1342 (python-tests-look-at "def decoratorFunctionWithArguments")
1343 (python-nav-end-of-defun)
1347 (should (= (save-excursion
1348 (python-tests-look-at "@decoratorFunctionWithArguments")
1349 (python-nav-end-of-defun)
1353 (should (= (save-excursion
1354 (python-tests-look-at "def wwrap(f):")
1355 (python-nav-end-of-defun)
1358 (python-tests-look-at "return wwrap")
1359 (line-beginning-position))))
1360 (should (= (save-excursion
1361 (python-tests-look-at "def wrapped_f(*args):")
1362 (python-nav-end-of-defun)
1365 (python-tests-look-at "return wrapped_f")
1366 (line-beginning-position))))
1367 (should (= (save-excursion
1368 (python-tests-look-at "f(*args)")
1369 (python-nav-end-of-defun)
1372 (python-tests-look-at "return wrapped_f")
1373 (line-beginning-position))))))
1375 (ert-deftest python-nav-backward-defun-1 ()
1376 (python-tests-with-temp-buffer
1378 class A(object): # A
1386 class B(object): # B
1388 class C(object): # C
1402 (goto-char (point-max))
1403 (should (= (save-excursion (python-nav-backward-defun))
1404 (python-tests-look-at " def c(self): # c" -1)))
1405 (should (= (save-excursion (python-nav-backward-defun))
1406 (python-tests-look-at " def d(self): # d" -1)))
1407 (should (= (save-excursion (python-nav-backward-defun))
1408 (python-tests-look-at " class C(object): # C" -1)))
1409 (should (= (save-excursion (python-nav-backward-defun))
1410 (python-tests-look-at " class B(object): # B" -1)))
1411 (should (= (save-excursion (python-nav-backward-defun))
1412 (python-tests-look-at " def b(self): # b" -1)))
1413 (should (= (save-excursion (python-nav-backward-defun))
1414 (python-tests-look-at " def a(self): # a" -1)))
1415 (should (= (save-excursion (python-nav-backward-defun))
1416 (python-tests-look-at "class A(object): # A" -1)))
1417 (should (not (python-nav-backward-defun)))))
1419 (ert-deftest python-nav-backward-defun-2 ()
1420 (python-tests-with-temp-buffer
1422 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1423 '''print decorated function call data to stdout.
1427 @decoratorFunctionWithArguments('arg1', 'arg2')
1428 def func(a, b, c=True):
1433 print 'Inside wwrap()'
1434 def wrapped_f(*args):
1435 print 'Inside wrapped_f()'
1436 print 'Decorator arguments:', arg1, arg2, arg3
1438 print 'After f(*args)'
1442 (goto-char (point-max))
1443 (should (= (save-excursion (python-nav-backward-defun))
1444 (python-tests-look-at " def wrapped_f(*args):" -1)))
1445 (should (= (save-excursion (python-nav-backward-defun))
1446 (python-tests-look-at " def wwrap(f):" -1)))
1447 (should (= (save-excursion (python-nav-backward-defun))
1448 (python-tests-look-at "def decoratorFunctionWithArguments(arg1, arg2, arg3):" -1)))
1449 (should (not (python-nav-backward-defun)))))
1451 (ert-deftest python-nav-backward-defun-3 ()
1452 (python-tests-with-temp-buffer
1468 (goto-char (point-min))
1469 (let ((point (python-tests-look-at "class A(object):")))
1470 (should (not (python-nav-backward-defun)))
1471 (should (= point (point))))))
1473 (ert-deftest python-nav-forward-defun-1 ()
1474 (python-tests-with-temp-buffer
1476 class A(object): # A
1484 class B(object): # B
1486 class C(object): # C
1500 (goto-char (point-min))
1501 (should (= (save-excursion (python-nav-forward-defun))
1502 (python-tests-look-at "(object): # A")))
1503 (should (= (save-excursion (python-nav-forward-defun))
1504 (python-tests-look-at "(self): # a")))
1505 (should (= (save-excursion (python-nav-forward-defun))
1506 (python-tests-look-at "(self): # b")))
1507 (should (= (save-excursion (python-nav-forward-defun))
1508 (python-tests-look-at "(object): # B")))
1509 (should (= (save-excursion (python-nav-forward-defun))
1510 (python-tests-look-at "(object): # C")))
1511 (should (= (save-excursion (python-nav-forward-defun))
1512 (python-tests-look-at "(self): # d")))
1513 (should (= (save-excursion (python-nav-forward-defun))
1514 (python-tests-look-at "(self): # c")))
1515 (should (not (python-nav-forward-defun)))))
1517 (ert-deftest python-nav-forward-defun-2 ()
1518 (python-tests-with-temp-buffer
1520 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1521 '''print decorated function call data to stdout.
1525 @decoratorFunctionWithArguments('arg1', 'arg2')
1526 def func(a, b, c=True):
1531 print 'Inside wwrap()'
1532 def wrapped_f(*args):
1533 print 'Inside wrapped_f()'
1534 print 'Decorator arguments:', arg1, arg2, arg3
1536 print 'After f(*args)'
1540 (goto-char (point-min))
1541 (should (= (save-excursion (python-nav-forward-defun))
1542 (python-tests-look-at "(arg1, arg2, arg3):")))
1543 (should (= (save-excursion (python-nav-forward-defun))
1544 (python-tests-look-at "(f):")))
1545 (should (= (save-excursion (python-nav-forward-defun))
1546 (python-tests-look-at "(*args):")))
1547 (should (not (python-nav-forward-defun)))))
1549 (ert-deftest python-nav-forward-defun-3 ()
1550 (python-tests-with-temp-buffer
1566 (goto-char (point-min))
1567 (let ((point (python-tests-look-at "(object):")))
1568 (should (not (python-nav-forward-defun)))
1569 (should (= point (point))))))
1571 (ert-deftest python-nav-beginning-of-statement-1 ()
1572 (python-tests-with-temp-buffer
1582 v3 = ('this is a string'
1587 # this is a comment, yo
1588 'continue previous line')
1594 (python-tests-look-at "v2 =")
1595 (python-util-forward-comment -1)
1596 (should (= (save-excursion
1597 (python-nav-beginning-of-statement)
1599 (python-tests-look-at "v1 =" -1 t)))
1600 (python-tests-look-at "v3 =")
1601 (python-util-forward-comment -1)
1602 (should (= (save-excursion
1603 (python-nav-beginning-of-statement)
1605 (python-tests-look-at "v2 =" -1 t)))
1606 (python-tests-look-at "v4 =")
1607 (python-util-forward-comment -1)
1608 (should (= (save-excursion
1609 (python-nav-beginning-of-statement)
1611 (python-tests-look-at "v3 =" -1 t)))
1612 (goto-char (point-max))
1613 (python-util-forward-comment -1)
1614 (should (= (save-excursion
1615 (python-nav-beginning-of-statement)
1617 (python-tests-look-at "v4 =" -1 t)))))
1619 (ert-deftest python-nav-end-of-statement-1 ()
1620 (python-tests-with-temp-buffer
1630 v3 = ('this is a string'
1635 # this is a comment, yo
1636 'continue previous line')
1642 (python-tests-look-at "v1 =")
1643 (should (= (save-excursion
1644 (python-nav-end-of-statement)
1647 (python-tests-look-at "789")
1648 (line-end-position))))
1649 (python-tests-look-at "v2 =")
1650 (should (= (save-excursion
1651 (python-nav-end-of-statement)
1654 (python-tests-look-at "value4)")
1655 (line-end-position))))
1656 (python-tests-look-at "v3 =")
1657 (should (= (save-excursion
1658 (python-nav-end-of-statement)
1661 (python-tests-look-at
1662 "'continue previous line')")
1663 (line-end-position))))
1664 (python-tests-look-at "v4 =")
1665 (should (= (save-excursion
1666 (python-nav-end-of-statement)
1669 (goto-char (point-max))
1670 (python-util-forward-comment -1)
1673 (ert-deftest python-nav-forward-statement-1 ()
1674 (python-tests-with-temp-buffer
1684 v3 = ('this is a string'
1689 # this is a comment, yo
1690 'continue previous line')
1696 (python-tests-look-at "v1 =")
1697 (should (= (save-excursion
1698 (python-nav-forward-statement)
1700 (python-tests-look-at "v2 =")))
1701 (should (= (save-excursion
1702 (python-nav-forward-statement)
1704 (python-tests-look-at "v3 =")))
1705 (should (= (save-excursion
1706 (python-nav-forward-statement)
1708 (python-tests-look-at "v4 =")))
1709 (should (= (save-excursion
1710 (python-nav-forward-statement)
1714 (ert-deftest python-nav-backward-statement-1 ()
1715 (python-tests-with-temp-buffer
1725 v3 = ('this is a string'
1730 # this is a comment, yo
1731 'continue previous line')
1737 (goto-char (point-max))
1738 (should (= (save-excursion
1739 (python-nav-backward-statement)
1741 (python-tests-look-at "v4 =" -1)))
1742 (should (= (save-excursion
1743 (python-nav-backward-statement)
1745 (python-tests-look-at "v3 =" -1)))
1746 (should (= (save-excursion
1747 (python-nav-backward-statement)
1749 (python-tests-look-at "v2 =" -1)))
1750 (should (= (save-excursion
1751 (python-nav-backward-statement)
1753 (python-tests-look-at "v1 =" -1)))))
1755 (ert-deftest python-nav-backward-statement-2 ()
1756 :expected-result :failed
1757 (python-tests-with-temp-buffer
1768 ;; FIXME: For some reason `python-nav-backward-statement' is moving
1769 ;; back two sentences when starting from 'value4)'.
1770 (goto-char (point-max))
1771 (python-util-forward-comment -1)
1772 (should (= (save-excursion
1773 (python-nav-backward-statement)
1775 (python-tests-look-at "v2 =" -1 t)))))
1777 (ert-deftest python-nav-beginning-of-block-1 ()
1778 (python-tests-with-temp-buffer
1780 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1781 '''print decorated function call data to stdout.
1785 @decoratorFunctionWithArguments('arg1', 'arg2')
1786 def func(a, b, c=True):
1791 print 'Inside wwrap()'
1792 def wrapped_f(*args):
1793 print 'Inside wrapped_f()'
1794 print 'Decorator arguments:', arg1, arg2, arg3
1796 print 'After f(*args)'
1800 (python-tests-look-at "return wwrap")
1801 (should (= (save-excursion
1802 (python-nav-beginning-of-block)
1804 (python-tests-look-at "def decoratorFunctionWithArguments" -1)))
1805 (python-tests-look-at "print 'Inside wwrap()'")
1806 (should (= (save-excursion
1807 (python-nav-beginning-of-block)
1809 (python-tests-look-at "def wwrap(f):" -1)))
1810 (python-tests-look-at "print 'After f(*args)'")
1812 (should (= (save-excursion
1813 (python-nav-beginning-of-block)
1815 (python-tests-look-at "def wrapped_f(*args):" -1)))
1816 (python-tests-look-at "return wrapped_f")
1817 (should (= (save-excursion
1818 (python-nav-beginning-of-block)
1820 (python-tests-look-at "def wwrap(f):" -1)))))
1822 (ert-deftest python-nav-end-of-block-1 ()
1823 (python-tests-with-temp-buffer
1825 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1826 '''print decorated function call data to stdout.
1830 @decoratorFunctionWithArguments('arg1', 'arg2')
1831 def func(a, b, c=True):
1836 print 'Inside wwrap()'
1837 def wrapped_f(*args):
1838 print 'Inside wrapped_f()'
1839 print 'Decorator arguments:', arg1, arg2, arg3
1841 print 'After f(*args)'
1845 (python-tests-look-at "def decoratorFunctionWithArguments")
1846 (should (= (save-excursion
1847 (python-nav-end-of-block)
1850 (goto-char (point-max))
1851 (python-util-forward-comment -1)
1853 (python-tests-look-at "def wwrap(f):")
1854 (should (= (save-excursion
1855 (python-nav-end-of-block)
1858 (python-tests-look-at "return wrapped_f")
1859 (line-end-position))))
1861 (should (= (save-excursion
1862 (python-nav-end-of-block)
1865 (python-tests-look-at "return wrapped_f")
1866 (line-end-position))))
1867 (python-tests-look-at "f(*args)")
1868 (should (= (save-excursion
1869 (python-nav-end-of-block)
1872 (python-tests-look-at "print 'After f(*args)'")
1873 (line-end-position))))))
1875 (ert-deftest python-nav-forward-block-1 ()
1876 "This also accounts as a test for `python-nav-backward-block'."
1877 (python-tests-with-temp-buffer
1879 if request.user.is_authenticated():
1883 profile = request.user.get_profile()
1884 except Profile.DoesNotExist:
1885 profile = Profile.objects.create(user=request.user)
1888 profile.recalculate_stats()
1890 profile.clear_stats()
1895 (should (= (save-excursion (python-nav-forward-block))
1896 (python-tests-look-at "if request.user.is_authenticated():")))
1897 (should (= (save-excursion (python-nav-forward-block))
1898 (python-tests-look-at "try:")))
1899 (should (= (save-excursion (python-nav-forward-block))
1900 (python-tests-look-at "except Profile.DoesNotExist:")))
1901 (should (= (save-excursion (python-nav-forward-block))
1902 (python-tests-look-at "else:")))
1903 (should (= (save-excursion (python-nav-forward-block))
1904 (python-tests-look-at "if profile.stats:")))
1905 (should (= (save-excursion (python-nav-forward-block))
1906 (python-tests-look-at "else:")))
1907 (should (= (save-excursion (python-nav-forward-block))
1908 (python-tests-look-at "finally:")))
1909 ;; When point is at the last block, leave it there and return nil
1910 (should (not (save-excursion (python-nav-forward-block))))
1911 ;; Move backwards, and even if the number of moves is less than the
1912 ;; provided argument return the point.
1913 (should (= (save-excursion (python-nav-forward-block -10))
1914 (python-tests-look-at
1915 "if request.user.is_authenticated():" -1)))))
1917 (ert-deftest python-nav-forward-sexp-1 ()
1918 (python-tests-with-temp-buffer
1924 (python-tests-look-at "a()")
1925 (python-nav-forward-sexp)
1926 (should (looking-at "$"))
1927 (should (save-excursion
1929 (looking-at "a()")))
1930 (python-nav-forward-sexp)
1931 (should (looking-at "$"))
1932 (should (save-excursion
1934 (looking-at "b()")))
1935 (python-nav-forward-sexp)
1936 (should (looking-at "$"))
1937 (should (save-excursion
1939 (looking-at "c()")))
1940 ;; Movement next to a paren should do what lisp does and
1941 ;; unfortunately It can't change, because otherwise
1942 ;; `blink-matching-open' breaks.
1943 (python-nav-forward-sexp -1)
1944 (should (looking-at "()"))
1945 (should (save-excursion
1947 (looking-at "c()")))
1948 (python-nav-forward-sexp -1)
1949 (should (looking-at "c()"))
1950 (python-nav-forward-sexp -1)
1951 (should (looking-at "b()"))
1952 (python-nav-forward-sexp -1)
1953 (should (looking-at "a()"))))
1955 (ert-deftest python-nav-forward-sexp-2 ()
1956 (python-tests-with-temp-buffer
1965 (python-tests-look-at "aa =")
1966 (python-nav-forward-sexp)
1967 (should (looking-at " = bbb"))
1968 (python-nav-forward-sexp)
1969 (should (looking-at "$"))
1970 (should (save-excursion
1971 (back-to-indentation)
1972 (looking-at "aaa = bbb")))
1973 (python-nav-forward-sexp)
1974 (should (looking-at "$"))
1975 (should (save-excursion
1976 (back-to-indentation)
1977 (looking-at "ccc = ddd")))
1978 (python-nav-forward-sexp)
1979 (should (looking-at "$"))
1980 (should (save-excursion
1981 (back-to-indentation)
1982 (looking-at "eee = fff")))
1983 (python-nav-forward-sexp)
1984 (should (looking-at "$"))
1985 (should (save-excursion
1986 (back-to-indentation)
1987 (looking-at "return ggg")))
1988 (python-nav-forward-sexp -1)
1989 (should (looking-at "def func():"))))
1991 (ert-deftest python-nav-forward-sexp-3 ()
1992 (python-tests-with-temp-buffer
1994 from some_module import some_sub_module
1995 from another_module import another_sub_module
1997 def another_statement():
2000 (python-tests-look-at "some_module")
2001 (python-nav-forward-sexp)
2002 (should (looking-at " import"))
2003 (python-nav-forward-sexp)
2004 (should (looking-at " some_sub_module"))
2005 (python-nav-forward-sexp)
2006 (should (looking-at "$"))
2009 (back-to-indentation)
2011 "from some_module import some_sub_module")))
2012 (python-nav-forward-sexp)
2013 (should (looking-at "$"))
2016 (back-to-indentation)
2018 "from another_module import another_sub_module")))
2019 (python-nav-forward-sexp)
2020 (should (looking-at "$"))
2023 (back-to-indentation)
2026 (python-nav-forward-sexp -1)
2027 (should (looking-at "def another_statement():"))
2028 (python-nav-forward-sexp -1)
2029 (should (looking-at "from another_module import another_sub_module"))
2030 (python-nav-forward-sexp -1)
2031 (should (looking-at "from some_module import some_sub_module"))))
2033 (ert-deftest python-nav-forward-sexp-safe-1 ()
2034 (python-tests-with-temp-buffer
2036 profile = Profile.objects.create(user=request.user)
2039 (python-tests-look-at "profile =")
2040 (python-nav-forward-sexp-safe 1)
2041 (should (looking-at "$"))
2042 (beginning-of-line 1)
2043 (python-tests-look-at "user=request.user")
2044 (python-nav-forward-sexp-safe -1)
2045 (should (looking-at "(user=request.user)"))
2046 (python-nav-forward-sexp-safe -4)
2047 (should (looking-at "profile ="))
2048 (python-tests-look-at "user=request.user")
2049 (python-nav-forward-sexp-safe 3)
2050 (should (looking-at ")"))
2051 (python-nav-forward-sexp-safe 1)
2052 (should (looking-at "$"))
2053 (python-nav-forward-sexp-safe 1)
2054 (should (looking-at "$"))))
2056 (ert-deftest python-nav-up-list-1 ()
2057 (python-tests-with-temp-buffer
2061 return [i for i in range(3)]
2063 (python-tests-look-at "3)]")
2064 (python-nav-up-list)
2065 (should (looking-at "]"))
2066 (python-nav-up-list)
2067 (should (looking-at "$"))))
2069 (ert-deftest python-nav-backward-up-list-1 ()
2070 :expected-result :failed
2071 (python-tests-with-temp-buffer
2075 return [i for i in range(3)]
2077 (python-tests-look-at "3)]")
2078 (python-nav-backward-up-list)
2079 (should (looking-at "(3)\\]"))
2080 (python-nav-backward-up-list)
2082 "\\[i for i in range(3)\\]"))
2083 ;; FIXME: Need to move to beginning-of-statement.
2084 (python-nav-backward-up-list)
2086 "return \\[i for i in range(3)\\]"))
2087 (python-nav-backward-up-list)
2088 (should (looking-at "if True:"))
2089 (python-nav-backward-up-list)
2090 (should (looking-at "def f():"))))
2092 (ert-deftest python-indent-dedent-line-backspace-1 ()
2093 "Check de-indentation on first call. Bug#18319."
2094 (python-tests-with-temp-buffer
2100 (python-tests-look-at "if False:")
2101 (call-interactively #'python-indent-dedent-line-backspace)
2102 (should (zerop (current-indentation)))
2103 ;; XXX: This should be a call to `undo' but it's triggering errors.
2105 (should (= (current-indentation) 4))
2106 (call-interactively #'python-indent-dedent-line-backspace)
2107 (should (zerop (current-indentation)))))
2110 ;;; Shell integration
2112 (defvar python-tests-shell-interpreter "python")
2114 (ert-deftest python-shell-get-process-name-1 ()
2115 "Check process name calculation sans `buffer-file-name'."
2116 (python-tests-with-temp-buffer
2118 (should (string= (python-shell-get-process-name nil)
2119 python-shell-buffer-name))
2120 (should (string= (python-shell-get-process-name t)
2121 (format "%s[%s]" python-shell-buffer-name (buffer-name))))))
2123 (ert-deftest python-shell-get-process-name-2 ()
2124 "Check process name calculation with `buffer-file-name'."
2125 (python-tests-with-temp-file
2127 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
2128 ;; should be respected.
2129 (should (string= (python-shell-get-process-name nil)
2130 python-shell-buffer-name))
2132 (python-shell-get-process-name t)
2133 (format "%s[%s]" python-shell-buffer-name (buffer-name))))))
2135 (ert-deftest python-shell-internal-get-process-name-1 ()
2136 "Check the internal process name is buffer-unique sans `buffer-file-name'."
2137 (python-tests-with-temp-buffer
2139 (should (string= (python-shell-internal-get-process-name)
2140 (format "%s[%s]" python-shell-internal-buffer-name (buffer-name))))))
2142 (ert-deftest python-shell-internal-get-process-name-2 ()
2143 "Check the internal process name is buffer-unique with `buffer-file-name'."
2144 (python-tests-with-temp-file
2146 (should (string= (python-shell-internal-get-process-name)
2147 (format "%s[%s]" python-shell-internal-buffer-name (buffer-name))))))
2149 (ert-deftest python-shell-calculate-command-1 ()
2150 "Check the command to execute is calculated correctly.
2151 Using `python-shell-interpreter' and
2152 `python-shell-interpreter-args'."
2153 (skip-unless (executable-find python-tests-shell-interpreter))
2154 (let ((python-shell-interpreter (executable-find
2155 python-tests-shell-interpreter))
2156 (python-shell-interpreter-args "-B"))
2159 python-shell-interpreter
2160 python-shell-interpreter-args)
2161 (python-shell-calculate-command)))))
2163 (ert-deftest python-shell-calculate-process-environment-1 ()
2164 "Test `python-shell-process-environment' modification."
2165 (let* ((python-shell-process-environment
2166 '("TESTVAR1=value1" "TESTVAR2=value2"))
2167 (process-environment
2168 (python-shell-calculate-process-environment)))
2169 (should (equal (getenv "TESTVAR1") "value1"))
2170 (should (equal (getenv "TESTVAR2") "value2"))))
2172 (ert-deftest python-shell-calculate-process-environment-2 ()
2173 "Test `python-shell-extra-pythonpaths' modification."
2174 (let* ((process-environment process-environment)
2175 (original-pythonpath (setenv "PYTHONPATH" "path3"))
2176 (paths '("path1" "path2"))
2177 (python-shell-extra-pythonpaths paths)
2178 (process-environment
2179 (python-shell-calculate-process-environment)))
2180 (should (equal (getenv "PYTHONPATH")
2182 (mapconcat 'identity paths path-separator)
2183 path-separator original-pythonpath)))))
2185 (ert-deftest python-shell-calculate-process-environment-3 ()
2186 "Test `python-shell-virtualenv-root' modification."
2187 (let* ((original-path (or (getenv "PATH") ""))
2188 (python-shell-virtualenv-root
2189 (directory-file-name user-emacs-directory))
2190 (process-environment
2191 (python-shell-calculate-process-environment)))
2192 (should (not (getenv "PYTHONHOME")))
2193 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-root))
2194 (should (equal (getenv "PATH")
2195 (format "%s/bin%s%s"
2196 python-shell-virtualenv-root
2197 path-separator original-path)))))
2199 (ert-deftest python-shell-calculate-process-environment-4 ()
2200 "Test `python-shell-unbuffered' modification."
2201 (setenv "PYTHONUNBUFFERED")
2202 (let* ((process-environment
2203 (python-shell-calculate-process-environment)))
2205 (should python-shell-unbuffered)
2206 (should (string= (getenv "PYTHONUNBUFFERED") "1"))))
2208 (ert-deftest python-shell-calculate-process-environment-5 ()
2209 (setenv "PYTHONUNBUFFERED")
2210 "Test `python-shell-unbuffered' modification."
2211 (let* ((python-shell-unbuffered nil)
2212 (process-environment
2213 (python-shell-calculate-process-environment)))
2214 (should (not (getenv "PYTHONUNBUFFERED")))))
2216 (ert-deftest python-shell-calculate-exec-path-1 ()
2217 "Test `python-shell-exec-path' modification."
2218 (let* ((original-exec-path exec-path)
2219 (python-shell-exec-path '("path1" "path2"))
2220 (exec-path (python-shell-calculate-exec-path)))
2223 (append python-shell-exec-path
2224 original-exec-path)))))
2226 (ert-deftest python-shell-calculate-exec-path-2 ()
2227 "Test `python-shell-exec-path' modification."
2228 (let* ((original-exec-path exec-path)
2229 (python-shell-virtualenv-root
2230 (directory-file-name (expand-file-name user-emacs-directory)))
2231 (exec-path (python-shell-calculate-exec-path)))
2235 (format "%s/bin" python-shell-virtualenv-root)
2236 original-exec-path))))))
2238 (ert-deftest python-shell-make-comint-1 ()
2239 "Check comint creation for global shell buffer."
2240 (skip-unless (executable-find python-tests-shell-interpreter))
2241 ;; The interpreter can get killed too quickly to allow it to clean
2242 ;; up the tempfiles that the default python-shell-setup-codes create,
2243 ;; so it leaves tempfiles behind, which is a minor irritation.
2244 (let* ((python-shell-setup-codes nil)
2245 (python-shell-interpreter
2246 (executable-find python-tests-shell-interpreter))
2247 (proc-name (python-shell-get-process-name nil))
2249 (python-tests-with-temp-buffer
2250 "" (python-shell-make-comint
2251 (python-shell-calculate-command) proc-name)))
2252 (process (get-buffer-process shell-buffer)))
2255 (set-process-query-on-exit-flag process nil)
2256 (should (process-live-p process))
2257 (with-current-buffer shell-buffer
2258 (should (eq major-mode 'inferior-python-mode))
2259 (should (string= (buffer-name) (format "*%s*" proc-name)))))
2260 (kill-buffer shell-buffer))))
2262 (ert-deftest python-shell-make-comint-2 ()
2263 "Check comint creation for internal shell buffer."
2264 (skip-unless (executable-find python-tests-shell-interpreter))
2265 (let* ((python-shell-setup-codes nil)
2266 (python-shell-interpreter
2267 (executable-find python-tests-shell-interpreter))
2268 (proc-name (python-shell-internal-get-process-name))
2270 (python-tests-with-temp-buffer
2271 "" (python-shell-make-comint
2272 (python-shell-calculate-command) proc-name nil t)))
2273 (process (get-buffer-process shell-buffer)))
2276 (set-process-query-on-exit-flag process nil)
2277 (should (process-live-p process))
2278 (with-current-buffer shell-buffer
2279 (should (eq major-mode 'inferior-python-mode))
2280 (should (string= (buffer-name) (format " *%s*" proc-name)))))
2281 (kill-buffer shell-buffer))))
2283 (ert-deftest python-shell-make-comint-3 ()
2284 "Check comint creation with overridden python interpreter and args.
2285 The command passed to `python-shell-make-comint' as argument must
2286 locally override global values set in `python-shell-interpreter'
2287 and `python-shell-interpreter-args' in the new shell buffer."
2288 (skip-unless (executable-find python-tests-shell-interpreter))
2289 (let* ((python-shell-setup-codes nil)
2290 (python-shell-interpreter "interpreter")
2291 (python-shell-interpreter-args "--some-args")
2292 (proc-name (python-shell-get-process-name nil))
2293 (interpreter-override
2294 (concat (executable-find python-tests-shell-interpreter) " " "-i"))
2296 (python-tests-with-temp-buffer
2297 "" (python-shell-make-comint interpreter-override proc-name nil)))
2298 (process (get-buffer-process shell-buffer)))
2301 (set-process-query-on-exit-flag process nil)
2302 (should (process-live-p process))
2303 (with-current-buffer shell-buffer
2304 (should (eq major-mode 'inferior-python-mode))
2305 (should (file-equal-p
2306 python-shell-interpreter
2307 (executable-find python-tests-shell-interpreter)))
2308 (should (string= python-shell-interpreter-args "-i"))))
2309 (kill-buffer shell-buffer))))
2311 (ert-deftest python-shell-make-comint-4 ()
2312 "Check shell calculated prompts regexps are set."
2313 (skip-unless (executable-find python-tests-shell-interpreter))
2314 (let* ((process-environment process-environment)
2315 (python-shell-setup-codes nil)
2316 (python-shell-interpreter
2317 (executable-find python-tests-shell-interpreter))
2318 (python-shell-interpreter-args "-i")
2319 (python-shell--prompt-calculated-input-regexp nil)
2320 (python-shell--prompt-calculated-output-regexp nil)
2321 (python-shell-prompt-detect-enabled t)
2322 (python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2323 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2324 (python-shell-prompt-regexp "in")
2325 (python-shell-prompt-block-regexp "block")
2326 (python-shell-prompt-pdb-regexp "pdf")
2327 (python-shell-prompt-output-regexp "output")
2328 (startup-code (concat "import sys\n"
2329 "sys.ps1 = 'py> '\n"
2330 "sys.ps2 = '..> '\n"
2331 "sys.ps3 = 'out '\n"))
2332 (startup-file (python-shell--save-temp-file startup-code))
2333 (proc-name (python-shell-get-process-name nil))
2336 (setenv "PYTHONSTARTUP" startup-file)
2337 (python-tests-with-temp-buffer
2338 "" (python-shell-make-comint
2339 (python-shell-calculate-command) proc-name nil))))
2340 (process (get-buffer-process shell-buffer)))
2343 (set-process-query-on-exit-flag process nil)
2344 (should (process-live-p process))
2345 (with-current-buffer shell-buffer
2346 (should (eq major-mode 'inferior-python-mode))
2348 python-shell--prompt-calculated-input-regexp
2349 (concat "^\\(extralargeinputprompt\\|\\.\\.> \\|"
2350 "block\\|py> \\|pdf\\|sml\\|in\\)")))
2352 python-shell--prompt-calculated-output-regexp
2353 "^\\(extralargeoutputprompt\\|output\\|out \\|sml\\)"))))
2354 (delete-file startup-file)
2355 (kill-buffer shell-buffer))))
2357 (ert-deftest python-shell-get-process-1 ()
2358 "Check dedicated shell process preference over global."
2359 (skip-unless (executable-find python-tests-shell-interpreter))
2360 (python-tests-with-temp-file
2362 (let* ((python-shell-setup-codes nil)
2363 (python-shell-interpreter
2364 (executable-find python-tests-shell-interpreter))
2365 (global-proc-name (python-shell-get-process-name nil))
2366 (dedicated-proc-name (python-shell-get-process-name t))
2367 (global-shell-buffer
2368 (python-shell-make-comint
2369 (python-shell-calculate-command) global-proc-name))
2370 (dedicated-shell-buffer
2371 (python-shell-make-comint
2372 (python-shell-calculate-command) dedicated-proc-name))
2373 (global-process (get-buffer-process global-shell-buffer))
2374 (dedicated-process (get-buffer-process dedicated-shell-buffer)))
2377 (set-process-query-on-exit-flag global-process nil)
2378 (set-process-query-on-exit-flag dedicated-process nil)
2379 ;; Prefer dedicated if global also exists.
2380 (should (equal (python-shell-get-process) dedicated-process))
2381 (kill-buffer dedicated-shell-buffer)
2382 ;; If there's only global, use it.
2383 (should (equal (python-shell-get-process) global-process))
2384 (kill-buffer global-shell-buffer)
2385 ;; No buffer available.
2386 (should (not (python-shell-get-process))))
2387 (ignore-errors (kill-buffer global-shell-buffer))
2388 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
2390 (ert-deftest python-shell-internal-get-or-create-process-1 ()
2391 "Check internal shell process creation fallback."
2392 (skip-unless (executable-find python-tests-shell-interpreter))
2393 (python-tests-with-temp-file
2395 (should (not (process-live-p (python-shell-internal-get-process-name))))
2396 (let* ((python-shell-interpreter
2397 (executable-find python-tests-shell-interpreter))
2398 (internal-process-name (python-shell-internal-get-process-name))
2399 (internal-process (python-shell-internal-get-or-create-process))
2400 (internal-shell-buffer (process-buffer internal-process)))
2403 (set-process-query-on-exit-flag internal-process nil)
2404 (should (equal (process-name internal-process)
2405 internal-process-name))
2406 (should (equal internal-process
2407 (python-shell-internal-get-or-create-process)))
2408 ;; Assert the internal process is not a user process
2409 (should (not (python-shell-get-process)))
2410 (kill-buffer internal-shell-buffer))
2411 (ignore-errors (kill-buffer internal-shell-buffer))))))
2413 (ert-deftest python-shell-prompt-detect-1 ()
2414 "Check prompt autodetection."
2415 (skip-unless (executable-find python-tests-shell-interpreter))
2416 (let ((process-environment process-environment))
2417 ;; Ensure no startup file is enabled
2418 (setenv "PYTHONSTARTUP" "")
2419 (should python-shell-prompt-detect-enabled)
2420 (should (equal (python-shell-prompt-detect) '(">>> " "... " "")))))
2422 (ert-deftest python-shell-prompt-detect-2 ()
2423 "Check prompt autodetection with startup file. Bug#17370."
2424 (skip-unless (executable-find python-tests-shell-interpreter))
2425 (let* ((process-environment process-environment)
2426 (startup-code (concat "import sys\n"
2427 "sys.ps1 = 'py> '\n"
2428 "sys.ps2 = '..> '\n"
2429 "sys.ps3 = 'out '\n"))
2430 (startup-file (python-shell--save-temp-file startup-code)))
2433 ;; Ensure startup file is enabled
2434 (setenv "PYTHONSTARTUP" startup-file)
2435 (should python-shell-prompt-detect-enabled)
2436 (should (equal (python-shell-prompt-detect) '("py> " "..> " "out "))))
2437 (ignore-errors (delete-file startup-file)))))
2439 (ert-deftest python-shell-prompt-detect-3 ()
2440 "Check prompts are not autodetected when feature is disabled."
2441 (skip-unless (executable-find python-tests-shell-interpreter))
2442 (let ((process-environment process-environment)
2443 (python-shell-prompt-detect-enabled nil))
2444 ;; Ensure no startup file is enabled
2445 (should (not python-shell-prompt-detect-enabled))
2446 (should (not (python-shell-prompt-detect)))))
2448 (ert-deftest python-shell-prompt-detect-4 ()
2449 "Check warning is shown when detection fails."
2450 (skip-unless (executable-find python-tests-shell-interpreter))
2451 (let* ((process-environment process-environment)
2452 ;; Trigger failure by removing prompts in the startup file
2453 (startup-code (concat "import sys\n"
2457 (startup-file (python-shell--save-temp-file startup-code)))
2460 (kill-buffer (get-buffer-create "*Warnings*"))
2461 (should (not (get-buffer "*Warnings*")))
2462 (setenv "PYTHONSTARTUP" startup-file)
2463 (should python-shell-prompt-detect-failure-warning)
2464 (should python-shell-prompt-detect-enabled)
2465 (should (not (python-shell-prompt-detect)))
2466 (should (get-buffer "*Warnings*")))
2467 (ignore-errors (delete-file startup-file)))))
2469 (ert-deftest python-shell-prompt-detect-5 ()
2470 "Check disabled warnings are not shown when detection fails."
2471 (skip-unless (executable-find python-tests-shell-interpreter))
2472 (let* ((process-environment process-environment)
2473 (startup-code (concat "import sys\n"
2477 (startup-file (python-shell--save-temp-file startup-code))
2478 (python-shell-prompt-detect-failure-warning nil))
2481 (kill-buffer (get-buffer-create "*Warnings*"))
2482 (should (not (get-buffer "*Warnings*")))
2483 (setenv "PYTHONSTARTUP" startup-file)
2484 (should (not python-shell-prompt-detect-failure-warning))
2485 (should python-shell-prompt-detect-enabled)
2486 (should (not (python-shell-prompt-detect)))
2487 (should (not (get-buffer "*Warnings*"))))
2488 (ignore-errors (delete-file startup-file)))))
2490 (ert-deftest python-shell-prompt-detect-6 ()
2491 "Warnings are not shown when detection is disabled."
2492 (skip-unless (executable-find python-tests-shell-interpreter))
2493 (let* ((process-environment process-environment)
2494 (startup-code (concat "import sys\n"
2498 (startup-file (python-shell--save-temp-file startup-code))
2499 (python-shell-prompt-detect-failure-warning t)
2500 (python-shell-prompt-detect-enabled nil))
2503 (kill-buffer (get-buffer-create "*Warnings*"))
2504 (should (not (get-buffer "*Warnings*")))
2505 (setenv "PYTHONSTARTUP" startup-file)
2506 (should python-shell-prompt-detect-failure-warning)
2507 (should (not python-shell-prompt-detect-enabled))
2508 (should (not (python-shell-prompt-detect)))
2509 (should (not (get-buffer "*Warnings*"))))
2510 (ignore-errors (delete-file startup-file)))))
2512 (ert-deftest python-shell-prompt-validate-regexps-1 ()
2513 "Check `python-shell-prompt-input-regexps' are validated."
2514 (let* ((python-shell-prompt-input-regexps '("\\("))
2515 (error-data (should-error (python-shell-prompt-validate-regexps)
2516 :type 'user-error)))
2518 (string= (cadr error-data)
2519 "Invalid regexp \\( in `python-shell-prompt-input-regexps'"))))
2521 (ert-deftest python-shell-prompt-validate-regexps-2 ()
2522 "Check `python-shell-prompt-output-regexps' are validated."
2523 (let* ((python-shell-prompt-output-regexps '("\\("))
2524 (error-data (should-error (python-shell-prompt-validate-regexps)
2525 :type 'user-error)))
2527 (string= (cadr error-data)
2528 "Invalid regexp \\( in `python-shell-prompt-output-regexps'"))))
2530 (ert-deftest python-shell-prompt-validate-regexps-3 ()
2531 "Check `python-shell-prompt-regexp' is validated."
2532 (let* ((python-shell-prompt-regexp "\\(")
2533 (error-data (should-error (python-shell-prompt-validate-regexps)
2534 :type 'user-error)))
2536 (string= (cadr error-data)
2537 "Invalid regexp \\( in `python-shell-prompt-regexp'"))))
2539 (ert-deftest python-shell-prompt-validate-regexps-4 ()
2540 "Check `python-shell-prompt-block-regexp' is validated."
2541 (let* ((python-shell-prompt-block-regexp "\\(")
2542 (error-data (should-error (python-shell-prompt-validate-regexps)
2543 :type 'user-error)))
2545 (string= (cadr error-data)
2546 "Invalid regexp \\( in `python-shell-prompt-block-regexp'"))))
2548 (ert-deftest python-shell-prompt-validate-regexps-5 ()
2549 "Check `python-shell-prompt-pdb-regexp' is validated."
2550 (let* ((python-shell-prompt-pdb-regexp "\\(")
2551 (error-data (should-error (python-shell-prompt-validate-regexps)
2552 :type 'user-error)))
2554 (string= (cadr error-data)
2555 "Invalid regexp \\( in `python-shell-prompt-pdb-regexp'"))))
2557 (ert-deftest python-shell-prompt-validate-regexps-6 ()
2558 "Check `python-shell-prompt-output-regexp' is validated."
2559 (let* ((python-shell-prompt-output-regexp "\\(")
2560 (error-data (should-error (python-shell-prompt-validate-regexps)
2561 :type 'user-error)))
2563 (string= (cadr error-data)
2564 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2566 (ert-deftest python-shell-prompt-validate-regexps-7 ()
2567 "Check default regexps are valid."
2568 ;; should not signal error
2569 (python-shell-prompt-validate-regexps))
2571 (ert-deftest python-shell-prompt-set-calculated-regexps-1 ()
2572 "Check regexps are validated."
2573 (let* ((python-shell-prompt-output-regexp '("\\("))
2574 (python-shell--prompt-calculated-input-regexp nil)
2575 (python-shell--prompt-calculated-output-regexp nil)
2576 (python-shell-prompt-detect-enabled nil)
2577 (error-data (should-error (python-shell-prompt-set-calculated-regexps)
2578 :type 'user-error)))
2580 (string= (cadr error-data)
2581 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2583 (ert-deftest python-shell-prompt-set-calculated-regexps-2 ()
2584 "Check `python-shell-prompt-input-regexps' are set."
2585 (let* ((python-shell-prompt-input-regexps '("my" "prompt"))
2586 (python-shell-prompt-output-regexps '(""))
2587 (python-shell-prompt-regexp "")
2588 (python-shell-prompt-block-regexp "")
2589 (python-shell-prompt-pdb-regexp "")
2590 (python-shell-prompt-output-regexp "")
2591 (python-shell--prompt-calculated-input-regexp nil)
2592 (python-shell--prompt-calculated-output-regexp nil)
2593 (python-shell-prompt-detect-enabled nil))
2594 (python-shell-prompt-set-calculated-regexps)
2595 (should (string= python-shell--prompt-calculated-input-regexp
2596 "^\\(prompt\\|my\\|\\)"))))
2598 (ert-deftest python-shell-prompt-set-calculated-regexps-3 ()
2599 "Check `python-shell-prompt-output-regexps' are set."
2600 (let* ((python-shell-prompt-input-regexps '(""))
2601 (python-shell-prompt-output-regexps '("my" "prompt"))
2602 (python-shell-prompt-regexp "")
2603 (python-shell-prompt-block-regexp "")
2604 (python-shell-prompt-pdb-regexp "")
2605 (python-shell-prompt-output-regexp "")
2606 (python-shell--prompt-calculated-input-regexp nil)
2607 (python-shell--prompt-calculated-output-regexp nil)
2608 (python-shell-prompt-detect-enabled nil))
2609 (python-shell-prompt-set-calculated-regexps)
2610 (should (string= python-shell--prompt-calculated-output-regexp
2611 "^\\(prompt\\|my\\|\\)"))))
2613 (ert-deftest python-shell-prompt-set-calculated-regexps-4 ()
2614 "Check user defined prompts are set."
2615 (let* ((python-shell-prompt-input-regexps '(""))
2616 (python-shell-prompt-output-regexps '(""))
2617 (python-shell-prompt-regexp "prompt")
2618 (python-shell-prompt-block-regexp "block")
2619 (python-shell-prompt-pdb-regexp "pdb")
2620 (python-shell-prompt-output-regexp "output")
2621 (python-shell--prompt-calculated-input-regexp nil)
2622 (python-shell--prompt-calculated-output-regexp nil)
2623 (python-shell-prompt-detect-enabled nil))
2624 (python-shell-prompt-set-calculated-regexps)
2625 (should (string= python-shell--prompt-calculated-input-regexp
2626 "^\\(prompt\\|block\\|pdb\\|\\)"))
2627 (should (string= python-shell--prompt-calculated-output-regexp
2628 "^\\(output\\|\\)"))))
2630 (ert-deftest python-shell-prompt-set-calculated-regexps-5 ()
2631 "Check order of regexps (larger first)."
2632 (let* ((python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2633 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2634 (python-shell-prompt-regexp "in")
2635 (python-shell-prompt-block-regexp "block")
2636 (python-shell-prompt-pdb-regexp "pdf")
2637 (python-shell-prompt-output-regexp "output")
2638 (python-shell--prompt-calculated-input-regexp nil)
2639 (python-shell--prompt-calculated-output-regexp nil)
2640 (python-shell-prompt-detect-enabled nil))
2641 (python-shell-prompt-set-calculated-regexps)
2642 (should (string= python-shell--prompt-calculated-input-regexp
2643 "^\\(extralargeinputprompt\\|block\\|pdf\\|sml\\|in\\)"))
2644 (should (string= python-shell--prompt-calculated-output-regexp
2645 "^\\(extralargeoutputprompt\\|output\\|sml\\)"))))
2647 (ert-deftest python-shell-prompt-set-calculated-regexps-6 ()
2648 "Check detected prompts are included `regexp-quote'd."
2649 (skip-unless (executable-find python-tests-shell-interpreter))
2650 (let* ((python-shell-prompt-input-regexps '(""))
2651 (python-shell-prompt-output-regexps '(""))
2652 (python-shell-prompt-regexp "")
2653 (python-shell-prompt-block-regexp "")
2654 (python-shell-prompt-pdb-regexp "")
2655 (python-shell-prompt-output-regexp "")
2656 (python-shell--prompt-calculated-input-regexp nil)
2657 (python-shell--prompt-calculated-output-regexp nil)
2658 (python-shell-prompt-detect-enabled t)
2659 (process-environment process-environment)
2660 (startup-code (concat "import sys\n"
2661 "sys.ps1 = 'p.> '\n"
2662 "sys.ps2 = '..> '\n"
2663 "sys.ps3 = 'o.t '\n"))
2664 (startup-file (python-shell--save-temp-file startup-code)))
2667 (setenv "PYTHONSTARTUP" startup-file)
2668 (python-shell-prompt-set-calculated-regexps)
2669 (should (string= python-shell--prompt-calculated-input-regexp
2670 "^\\(\\.\\.> \\|p\\.> \\|\\)"))
2671 (should (string= python-shell--prompt-calculated-output-regexp
2672 "^\\(o\\.t \\|\\)")))
2673 (ignore-errors (delete-file startup-file)))))
2675 (ert-deftest python-shell-buffer-substring-1 ()
2676 "Selecting a substring of the whole buffer must match its contents."
2677 (python-tests-with-temp-buffer
2679 class Foo(models.Model):
2683 class Bar(models.Model):
2686 (should (string= (buffer-string)
2687 (python-shell-buffer-substring (point-min) (point-max))))))
2689 (ert-deftest python-shell-buffer-substring-2 ()
2690 "Main block should be removed if NOMAIN is non-nil."
2691 (python-tests-with-temp-buffer
2693 class Foo(models.Model):
2696 class Bar(models.Model):
2699 if __name__ == \"__main__\":
2703 (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
2705 class Foo(models.Model):
2708 class Bar(models.Model):
2716 (ert-deftest python-shell-buffer-substring-3 ()
2717 "Main block should be removed if NOMAIN is non-nil."
2718 (python-tests-with-temp-buffer
2720 class Foo(models.Model):
2723 if __name__ == \"__main__\":
2727 class Bar(models.Model):
2730 (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
2732 class Foo(models.Model):
2739 class Bar(models.Model):
2743 (ert-deftest python-shell-buffer-substring-4 ()
2744 "Coding cookie should be added for substrings."
2745 (python-tests-with-temp-buffer
2748 class Foo(models.Model):
2751 if __name__ == \"__main__\":
2755 class Bar(models.Model):
2758 (should (string= (python-shell-buffer-substring
2759 (python-tests-look-at "class Foo(models.Model):")
2760 (progn (python-nav-forward-sexp) (point)))
2761 "# -*- coding: latin-1 -*-
2763 class Foo(models.Model):
2766 (ert-deftest python-shell-buffer-substring-5 ()
2767 "The proper amount of blank lines is added for a substring."
2768 (python-tests-with-temp-buffer
2771 class Foo(models.Model):
2774 if __name__ == \"__main__\":
2778 class Bar(models.Model):
2781 (should (string= (python-shell-buffer-substring
2782 (python-tests-look-at "class Bar(models.Model):")
2783 (progn (python-nav-forward-sexp) (point)))
2784 "# -*- coding: latin-1 -*-
2793 class Bar(models.Model):
2796 (ert-deftest python-shell-buffer-substring-6 ()
2797 "Handle substring with coding cookie in the second line."
2798 (python-tests-with-temp-buffer
2802 class Foo(models.Model):
2805 if __name__ == \"__main__\":
2809 class Bar(models.Model):
2812 (should (string= (python-shell-buffer-substring
2813 (python-tests-look-at "# coding: latin-1")
2814 (python-tests-look-at "if __name__ == \"__main__\":"))
2815 "# -*- coding: latin-1 -*-
2818 class Foo(models.Model):
2823 (ert-deftest python-shell-buffer-substring-7 ()
2824 "Ensure first coding cookie gets precedence."
2825 (python-tests-with-temp-buffer
2829 class Foo(models.Model):
2832 if __name__ == \"__main__\":
2836 class Bar(models.Model):
2839 (should (string= (python-shell-buffer-substring
2840 (python-tests-look-at "# coding: latin-1")
2841 (python-tests-look-at "if __name__ == \"__main__\":"))
2842 "# -*- coding: utf-8 -*-
2845 class Foo(models.Model):
2850 (ert-deftest python-shell-buffer-substring-8 ()
2851 "Ensure first coding cookie gets precedence when sending whole buffer."
2852 (python-tests-with-temp-buffer
2856 class Foo(models.Model):
2859 (should (string= (python-shell-buffer-substring (point-min) (point-max))
2863 class Foo(models.Model):
2867 (ert-deftest python-shell-buffer-substring-9 ()
2868 "Check substring starting from `point-min'."
2869 (python-tests-with-temp-buffer
2872 class Foo(models.Model):
2875 class Bar(models.Model):
2878 (should (string= (python-shell-buffer-substring
2880 (python-tests-look-at "class Bar(models.Model):"))
2883 class Foo(models.Model):
2889 ;;; Shell completion
2891 (ert-deftest python-shell-completion-native-interpreter-disabled-p-1 ()
2892 (let* ((python-shell-completion-native-disabled-interpreters (list "pypy"))
2893 (python-shell-interpreter "/some/path/to/bin/pypy"))
2894 (should (python-shell-completion-native-interpreter-disabled-p))))
2899 ;;; PDB Track integration
2902 ;;; Symbol completion
2922 (ert-deftest python-imenu-create-index-1 ()
2923 (python-tests-with-temp-buffer
2925 class Foo(models.Model):
2929 class Bar(models.Model):
2933 def decorator(arg1, arg2, arg3):
2934 '''print decorated function call data to stdout.
2938 @decorator('arg1', 'arg2')
2939 def func(a, b, c=True):
2945 def wrapped_f(*args):
2947 print ('Decorator arguments:', arg1, arg2, arg3)
2949 print ('called f(*args)')
2967 (goto-char (point-max))
2970 (cons "Foo (class)" (copy-marker 2))
2971 (cons "Bar (class)" (copy-marker 38))
2974 (cons "*function definition*" (copy-marker 74))
2977 (cons "*function definition*" (copy-marker 254))
2978 (cons "wrapped_f (def)" (copy-marker 294))))
2981 (cons "*class definition*" (copy-marker 519))
2982 (cons "a (def)" (copy-marker 539))
2983 (cons "b (def)" (copy-marker 570))
2986 (cons "*class definition*" (copy-marker 601))
2987 (cons "c (def)" (copy-marker 626)))))
2988 (python-imenu-create-index)))))
2990 (ert-deftest python-imenu-create-index-2 ()
2991 (python-tests-with-temp-buffer
3001 (goto-char (point-max))
3006 (cons "*class definition*" (copy-marker 2))
3009 (cons "*function definition*" (copy-marker 21))
3010 (cons "foo1 (def)" (copy-marker 40)))
3011 (cons "foobar (def)" (copy-marker 78))))
3012 (python-imenu-create-index)))))
3014 (ert-deftest python-imenu-create-index-3 ()
3015 (python-tests-with-temp-buffer
3024 (goto-char (point-max))
3029 (cons "*class definition*" (copy-marker 2))
3032 (cons "*function definition*" (copy-marker 21))
3033 (cons "foo1 (def)" (copy-marker 40))
3034 (cons "foo2 (def)" (copy-marker 77)))))
3035 (python-imenu-create-index)))))
3037 (ert-deftest python-imenu-create-index-4 ()
3038 (python-tests-with-temp-buffer
3051 (goto-char (point-max))
3056 (cons "*class definition*" (copy-marker 2))
3059 (cons "*class definition*" (copy-marker 21))
3060 (cons "__init__ (def)" (copy-marker 44))
3061 (cons "__str__ (def)" (copy-marker 90)))
3062 (cons "__init__ (def)" (copy-marker 135))))
3063 (python-imenu-create-index)))))
3065 (ert-deftest python-imenu-create-flat-index-1 ()
3066 (python-tests-with-temp-buffer
3068 class Foo(models.Model):
3072 class Bar(models.Model):
3076 def decorator(arg1, arg2, arg3):
3077 '''print decorated function call data to stdout.
3081 @decorator('arg1', 'arg2')
3082 def func(a, b, c=True):
3088 def wrapped_f(*args):
3090 print ('Decorator arguments:', arg1, arg2, arg3)
3092 print ('called f(*args)')
3110 (goto-char (point-max))
3112 (list (cons "Foo" (copy-marker 2))
3113 (cons "Bar" (copy-marker 38))
3114 (cons "decorator" (copy-marker 74))
3115 (cons "decorator.wrap" (copy-marker 254))
3116 (cons "decorator.wrap.wrapped_f" (copy-marker 294))
3117 (cons "Baz" (copy-marker 519))
3118 (cons "Baz.a" (copy-marker 539))
3119 (cons "Baz.b" (copy-marker 570))
3120 (cons "Baz.Frob" (copy-marker 601))
3121 (cons "Baz.Frob.c" (copy-marker 626)))
3122 (python-imenu-create-flat-index)))))
3124 (ert-deftest python-imenu-create-flat-index-2 ()
3125 (python-tests-with-temp-buffer
3138 (goto-char (point-max))
3141 (cons "Foo" (copy-marker 2))
3142 (cons "Foo.Bar" (copy-marker 21))
3143 (cons "Foo.Bar.__init__" (copy-marker 44))
3144 (cons "Foo.Bar.__str__" (copy-marker 90))
3145 (cons "Foo.__init__" (copy-marker 135)))
3146 (python-imenu-create-flat-index)))))
3151 (ert-deftest python-info-current-defun-1 ()
3152 (python-tests-with-temp-buffer
3157 (should (string= "foo" (python-info-current-defun)))
3158 (should (string= "def foo" (python-info-current-defun t)))
3160 (should (not (python-info-current-defun)))
3161 (indent-for-tab-command)
3162 (should (string= "foo" (python-info-current-defun)))
3163 (should (string= "def foo" (python-info-current-defun t)))))
3165 (ert-deftest python-info-current-defun-2 ()
3166 (python-tests-with-temp-buffer
3172 return [i for i in range(3)]
3186 (should (string= "C" (python-info-current-defun)))
3187 (should (string= "class C" (python-info-current-defun t)))
3188 (python-tests-look-at "return [i for ")
3189 (should (string= "C.m" (python-info-current-defun)))
3190 (should (string= "def C.m" (python-info-current-defun t)))
3191 (python-tests-look-at "def b():")
3192 (should (string= "C.m.b" (python-info-current-defun)))
3193 (should (string= "def C.m.b" (python-info-current-defun t)))
3195 (indent-for-tab-command)
3196 (python-indent-dedent-line-backspace 1)
3197 (should (string= "C.m" (python-info-current-defun)))
3198 (should (string= "def C.m" (python-info-current-defun t)))
3199 (python-tests-look-at "def c(self):")
3201 (indent-for-tab-command)
3202 (should (string= "C.m.a" (python-info-current-defun)))
3203 (should (string= "def C.m.a" (python-info-current-defun t)))
3204 (python-indent-dedent-line-backspace 1)
3205 (should (string= "C.m" (python-info-current-defun)))
3206 (should (string= "def C.m" (python-info-current-defun t)))
3207 (python-indent-dedent-line-backspace 1)
3208 (should (string= "C" (python-info-current-defun)))
3209 (should (string= "class C" (python-info-current-defun t)))
3210 (python-tests-look-at "def c(self):")
3211 (should (string= "C.c" (python-info-current-defun)))
3212 (should (string= "def C.c" (python-info-current-defun t)))
3213 (python-tests-look-at "do_c()")
3214 (should (string= "C.c" (python-info-current-defun)))
3215 (should (string= "def C.c" (python-info-current-defun t)))))
3217 (ert-deftest python-info-current-defun-3 ()
3218 (python-tests-with-temp-buffer
3220 def decoratorFunctionWithArguments(arg1, arg2, arg3):
3221 '''print decorated function call data to stdout.
3225 @decoratorFunctionWithArguments('arg1', 'arg2')
3226 def func(a, b, c=True):
3231 print 'Inside wwrap()'
3232 def wrapped_f(*args):
3233 print 'Inside wrapped_f()'
3234 print 'Decorator arguments:', arg1, arg2, arg3
3236 print 'After f(*args)'
3240 (python-tests-look-at "def wwrap(f):")
3242 (should (not (python-info-current-defun)))
3243 (indent-for-tab-command 1)
3244 (should (string= (python-info-current-defun)
3245 "decoratorFunctionWithArguments"))
3246 (should (string= (python-info-current-defun t)
3247 "def decoratorFunctionWithArguments"))
3248 (python-tests-look-at "def wrapped_f(*args):")
3249 (should (string= (python-info-current-defun)
3250 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
3251 (should (string= (python-info-current-defun t)
3252 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
3253 (python-tests-look-at "return wrapped_f")
3254 (should (string= (python-info-current-defun)
3255 "decoratorFunctionWithArguments.wwrap"))
3256 (should (string= (python-info-current-defun t)
3257 "def decoratorFunctionWithArguments.wwrap"))
3259 (python-tests-look-at "return wwrap")
3260 (should (string= (python-info-current-defun)
3261 "decoratorFunctionWithArguments"))
3262 (should (string= (python-info-current-defun t)
3263 "def decoratorFunctionWithArguments"))))
3265 (ert-deftest python-info-current-symbol-1 ()
3266 (python-tests-with-temp-buffer
3276 (python-tests-look-at "self.c()")
3277 (should (string= "self.c" (python-info-current-symbol)))
3278 (should (string= "C.c" (python-info-current-symbol t)))))
3280 (ert-deftest python-info-current-symbol-2 ()
3281 (python-tests-with-temp-buffer
3293 (python-tests-look-at "self.c()")
3294 (should (string= "self.c" (python-info-current-symbol)))
3295 (should (string= "C.M.c" (python-info-current-symbol t)))))
3297 (ert-deftest python-info-current-symbol-3 ()
3298 "Keywords should not be considered symbols."
3299 :expected-result :failed
3300 (python-tests-with-temp-buffer
3305 ;; FIXME: keywords are not symbols.
3306 (python-tests-look-at "class C")
3307 (should (not (python-info-current-symbol)))
3308 (should (not (python-info-current-symbol t)))
3309 (python-tests-look-at "C(object)")
3310 (should (string= "C" (python-info-current-symbol)))
3311 (should (string= "class C" (python-info-current-symbol t)))))
3313 (ert-deftest python-info-statement-starts-block-p-1 ()
3314 (python-tests-with-temp-buffer
3316 def long_function_name(
3317 var_one, var_two, var_three,
3321 (python-tests-look-at "def long_function_name")
3322 (should (python-info-statement-starts-block-p))
3323 (python-tests-look-at "print (var_one)")
3324 (python-util-forward-comment -1)
3325 (should (python-info-statement-starts-block-p))))
3327 (ert-deftest python-info-statement-starts-block-p-2 ()
3328 (python-tests-with-temp-buffer
3330 if width == 0 and height == 0 and \\\\
3331 color == 'red' and emphasis == 'strong' or \\\\
3333 raise ValueError('sorry, you lose')
3335 (python-tests-look-at "if width == 0 and")
3336 (should (python-info-statement-starts-block-p))
3337 (python-tests-look-at "raise ValueError(")
3338 (python-util-forward-comment -1)
3339 (should (python-info-statement-starts-block-p))))
3341 (ert-deftest python-info-statement-ends-block-p-1 ()
3342 (python-tests-with-temp-buffer
3344 def long_function_name(
3345 var_one, var_two, var_three,
3349 (python-tests-look-at "print (var_one)")
3350 (should (python-info-statement-ends-block-p))))
3352 (ert-deftest python-info-statement-ends-block-p-2 ()
3353 (python-tests-with-temp-buffer
3355 if width == 0 and height == 0 and \\\\
3356 color == 'red' and emphasis == 'strong' or \\\\
3363 (python-tests-look-at "raise ValueError(")
3364 (should (python-info-statement-ends-block-p))))
3366 (ert-deftest python-info-beginning-of-statement-p-1 ()
3367 (python-tests-with-temp-buffer
3369 def long_function_name(
3370 var_one, var_two, var_three,
3374 (python-tests-look-at "def long_function_name")
3375 (should (python-info-beginning-of-statement-p))
3377 (should (not (python-info-beginning-of-statement-p)))
3378 (python-tests-look-at "print (var_one)")
3379 (should (python-info-beginning-of-statement-p))
3380 (goto-char (line-beginning-position))
3381 (should (not (python-info-beginning-of-statement-p)))))
3383 (ert-deftest python-info-beginning-of-statement-p-2 ()
3384 (python-tests-with-temp-buffer
3386 if width == 0 and height == 0 and \\\\
3387 color == 'red' and emphasis == 'strong' or \\\\
3394 (python-tests-look-at "if width == 0 and")
3395 (should (python-info-beginning-of-statement-p))
3397 (should (not (python-info-beginning-of-statement-p)))
3398 (python-tests-look-at "raise ValueError(")
3399 (should (python-info-beginning-of-statement-p))
3400 (goto-char (line-beginning-position))
3401 (should (not (python-info-beginning-of-statement-p)))))
3403 (ert-deftest python-info-end-of-statement-p-1 ()
3404 (python-tests-with-temp-buffer
3406 def long_function_name(
3407 var_one, var_two, var_three,
3411 (python-tests-look-at "def long_function_name")
3412 (should (not (python-info-end-of-statement-p)))
3414 (should (not (python-info-end-of-statement-p)))
3415 (python-tests-look-at "print (var_one)")
3416 (python-util-forward-comment -1)
3417 (should (python-info-end-of-statement-p))
3418 (python-tests-look-at "print (var_one)")
3419 (should (not (python-info-end-of-statement-p)))
3421 (should (python-info-end-of-statement-p))))
3423 (ert-deftest python-info-end-of-statement-p-2 ()
3424 (python-tests-with-temp-buffer
3426 if width == 0 and height == 0 and \\\\
3427 color == 'red' and emphasis == 'strong' or \\\\
3434 (python-tests-look-at "if width == 0 and")
3435 (should (not (python-info-end-of-statement-p)))
3437 (should (not (python-info-end-of-statement-p)))
3438 (python-tests-look-at "raise ValueError(")
3439 (python-util-forward-comment -1)
3440 (should (python-info-end-of-statement-p))
3441 (python-tests-look-at "raise ValueError(")
3442 (should (not (python-info-end-of-statement-p)))
3444 (should (not (python-info-end-of-statement-p)))
3445 (goto-char (point-max))
3446 (python-util-forward-comment -1)
3447 (should (python-info-end-of-statement-p))))
3449 (ert-deftest python-info-beginning-of-block-p-1 ()
3450 (python-tests-with-temp-buffer
3452 def long_function_name(
3453 var_one, var_two, var_three,
3457 (python-tests-look-at "def long_function_name")
3458 (should (python-info-beginning-of-block-p))
3459 (python-tests-look-at "var_one, var_two, var_three,")
3460 (should (not (python-info-beginning-of-block-p)))
3461 (python-tests-look-at "print (var_one)")
3462 (should (not (python-info-beginning-of-block-p)))))
3464 (ert-deftest python-info-beginning-of-block-p-2 ()
3465 (python-tests-with-temp-buffer
3467 if width == 0 and height == 0 and \\\\
3468 color == 'red' and emphasis == 'strong' or \\\\
3475 (python-tests-look-at "if width == 0 and")
3476 (should (python-info-beginning-of-block-p))
3477 (python-tests-look-at "color == 'red' and emphasis")
3478 (should (not (python-info-beginning-of-block-p)))
3479 (python-tests-look-at "raise ValueError(")
3480 (should (not (python-info-beginning-of-block-p)))))
3482 (ert-deftest python-info-end-of-block-p-1 ()
3483 (python-tests-with-temp-buffer
3485 def long_function_name(
3486 var_one, var_two, var_three,
3490 (python-tests-look-at "def long_function_name")
3491 (should (not (python-info-end-of-block-p)))
3492 (python-tests-look-at "var_one, var_two, var_three,")
3493 (should (not (python-info-end-of-block-p)))
3494 (python-tests-look-at "var_four):")
3496 (should (not (python-info-end-of-block-p)))
3497 (python-tests-look-at "print (var_one)")
3498 (should (not (python-info-end-of-block-p)))
3500 (should (python-info-end-of-block-p))))
3502 (ert-deftest python-info-end-of-block-p-2 ()
3503 (python-tests-with-temp-buffer
3505 if width == 0 and height == 0 and \\\\
3506 color == 'red' and emphasis == 'strong' or \\\\
3513 (python-tests-look-at "if width == 0 and")
3514 (should (not (python-info-end-of-block-p)))
3515 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3516 (should (not (python-info-end-of-block-p)))
3517 (python-tests-look-at "highlight > 100:")
3519 (should (not (python-info-end-of-block-p)))
3520 (python-tests-look-at "raise ValueError(")
3521 (should (not (python-info-end-of-block-p)))
3523 (should (not (python-info-end-of-block-p)))
3524 (goto-char (point-max))
3525 (python-util-forward-comment -1)
3526 (should (python-info-end-of-block-p))))
3528 (ert-deftest python-info-dedenter-opening-block-position-1 ()
3529 (python-tests-with-temp-buffer
3531 if request.user.is_authenticated():
3533 profile = request.user.get_profile()
3534 except Profile.DoesNotExist:
3535 profile = Profile.objects.create(user=request.user)
3538 profile.recalculate_stats()
3540 profile.clear_stats()
3545 (python-tests-look-at "try:")
3546 (should (not (python-info-dedenter-opening-block-position)))
3547 (python-tests-look-at "except Profile.DoesNotExist:")
3548 (should (= (python-tests-look-at "try:" -1 t)
3549 (python-info-dedenter-opening-block-position)))
3550 (python-tests-look-at "else:")
3551 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -1 t)
3552 (python-info-dedenter-opening-block-position)))
3553 (python-tests-look-at "if profile.stats:")
3554 (should (not (python-info-dedenter-opening-block-position)))
3555 (python-tests-look-at "else:")
3556 (should (= (python-tests-look-at "if profile.stats:" -1 t)
3557 (python-info-dedenter-opening-block-position)))
3558 (python-tests-look-at "finally:")
3559 (should (= (python-tests-look-at "else:" -2 t)
3560 (python-info-dedenter-opening-block-position)))))
3562 (ert-deftest python-info-dedenter-opening-block-position-2 ()
3563 (python-tests-with-temp-buffer
3565 if request.user.is_authenticated():
3566 profile = Profile.objects.get_or_create(user=request.user)
3568 profile.recalculate_stats()
3575 (python-tests-look-at "'else': 'do it'")
3576 (should (not (python-info-dedenter-opening-block-position)))
3577 (python-tests-look-at "'else'")
3578 (should (not (python-info-dedenter-opening-block-position)))))
3580 (ert-deftest python-info-dedenter-opening-block-position-3 ()
3581 (python-tests-with-temp-buffer
3587 msg = 'Error saving to disk'
3589 logger.exception(msg)
3592 logger.exception('Unhandled exception')
3597 (python-tests-look-at "try:")
3598 (should (not (python-info-dedenter-opening-block-position)))
3600 (python-tests-look-at "except IOError:")
3601 (should (= (python-tests-look-at "try:" -1 t)
3602 (python-info-dedenter-opening-block-position)))
3604 (python-tests-look-at "except Exception:")
3605 (should (= (python-tests-look-at "except IOError:" -1 t)
3606 (python-info-dedenter-opening-block-position)))
3608 (python-tests-look-at "if hide_details:")
3609 (should (not (python-info-dedenter-opening-block-position)))
3611 ;; check indentation modifies the detected opening block
3612 (python-tests-look-at "else")
3613 (should (= (python-tests-look-at "if hide_details:" -1 t)
3614 (python-info-dedenter-opening-block-position)))
3617 (should (= (python-tests-look-at "if hide_details:" -1 t)
3618 (python-info-dedenter-opening-block-position)))
3621 (should (= (python-tests-look-at "except Exception:" -1 t)
3622 (python-info-dedenter-opening-block-position)))
3625 (should (= (python-tests-look-at "if save:" -1 t)
3626 (python-info-dedenter-opening-block-position)))))
3628 (ert-deftest python-info-dedenter-opening-block-positions-1 ()
3629 (python-tests-with-temp-buffer
3635 msg = 'Error saving to disk'
3637 logger.exception(msg)
3640 logger.exception('Unhandled exception')
3645 (python-tests-look-at "try:")
3646 (should (not (python-info-dedenter-opening-block-positions)))
3648 (python-tests-look-at "except IOError:")
3651 (python-tests-look-at "try:" -1 t))
3652 (python-info-dedenter-opening-block-positions)))
3654 (python-tests-look-at "except Exception:")
3657 (python-tests-look-at "except IOError:" -1 t))
3658 (python-info-dedenter-opening-block-positions)))
3660 (python-tests-look-at "if hide_details:")
3661 (should (not (python-info-dedenter-opening-block-positions)))
3663 ;; check indentation does not modify the detected opening blocks
3664 (python-tests-look-at "else")
3667 (python-tests-look-at "if hide_details:" -1 t)
3668 (python-tests-look-at "except Exception:" -1 t)
3669 (python-tests-look-at "if save:" -1 t))
3670 (python-info-dedenter-opening-block-positions)))
3675 (python-tests-look-at "if hide_details:" -1 t)
3676 (python-tests-look-at "except Exception:" -1 t)
3677 (python-tests-look-at "if save:" -1 t))
3678 (python-info-dedenter-opening-block-positions)))
3683 (python-tests-look-at "if hide_details:" -1 t)
3684 (python-tests-look-at "except Exception:" -1 t)
3685 (python-tests-look-at "if save:" -1 t))
3686 (python-info-dedenter-opening-block-positions)))
3691 (python-tests-look-at "if hide_details:" -1 t)
3692 (python-tests-look-at "except Exception:" -1 t)
3693 (python-tests-look-at "if save:" -1 t))
3694 (python-info-dedenter-opening-block-positions)))))
3696 (ert-deftest python-info-dedenter-opening-block-positions-2 ()
3697 "Test detection of opening blocks for elif."
3698 (python-tests-with-temp-buffer
3707 (python-tests-look-at "elif var3:")
3710 (python-tests-look-at "if var2:" -1 t)
3711 (python-tests-look-at "if var:" -1 t))
3712 (python-info-dedenter-opening-block-positions)))
3714 (python-tests-look-at "elif\n")
3717 (python-tests-look-at "elif var3:" -1 t)
3718 (python-tests-look-at "if var:" -1 t))
3719 (python-info-dedenter-opening-block-positions)))))
3721 (ert-deftest python-info-dedenter-opening-block-positions-3 ()
3722 "Test detection of opening blocks for else."
3723 (python-tests-with-temp-buffer
3745 (python-tests-look-at "else\n")
3748 (python-tests-look-at "elif var3:" -1 t)
3749 (python-tests-look-at "if var:" -1 t)
3750 (python-tests-look-at "except:" -1 t))
3751 (python-info-dedenter-opening-block-positions)))
3753 (python-tests-look-at "else\n")
3756 (python-tests-look-at "while var5:" -1 t)
3757 (python-tests-look-at "if var4:" -1 t))
3758 (python-info-dedenter-opening-block-positions)))
3760 (python-tests-look-at "else\n")
3763 (python-tests-look-at "if value > 0:" -1 t)
3764 (python-tests-look-at "for value in var6:" -1 t)
3765 (python-tests-look-at "if var4:" -1 t))
3766 (python-info-dedenter-opening-block-positions)))))
3768 (ert-deftest python-info-dedenter-opening-block-positions-4 ()
3769 "Test detection of opening blocks for except."
3770 (python-tests-with-temp-buffer
3778 (python-tests-look-at "except ValueError:")
3780 (equal (list (python-tests-look-at "try:" -1 t))
3781 (python-info-dedenter-opening-block-positions)))
3783 (python-tests-look-at "except\n")
3785 (equal (list (python-tests-look-at "except ValueError:" -1 t))
3786 (python-info-dedenter-opening-block-positions)))))
3788 (ert-deftest python-info-dedenter-opening-block-positions-5 ()
3789 "Test detection of opening blocks for finally."
3790 (python-tests-with-temp-buffer
3799 logger.exception('something went wrong')
3803 something_else_else()
3805 logger.exception('something else went wrong')
3810 (python-tests-look-at "finally\n")
3812 (equal (list (python-tests-look-at "try:" -1 t))
3813 (python-info-dedenter-opening-block-positions)))
3815 (python-tests-look-at "finally\n")
3817 (equal (list (python-tests-look-at "except:" -1 t))
3818 (python-info-dedenter-opening-block-positions)))
3820 (python-tests-look-at "finally\n")
3822 (equal (list (python-tests-look-at "else:" -1 t))
3823 (python-info-dedenter-opening-block-positions)))))
3825 (ert-deftest python-info-dedenter-opening-block-message-1 ()
3826 "Test dedenters inside strings are ignored."
3827 (python-tests-with-temp-buffer
3832 logger.exception('something went wrong')
3835 (python-tests-look-at "except\n")
3836 (should (not (python-info-dedenter-opening-block-message)))))
3838 (ert-deftest python-info-dedenter-opening-block-message-2 ()
3839 "Test except keyword."
3840 (python-tests-with-temp-buffer
3845 logger.exception('something went wrong')
3847 (python-tests-look-at "except:")
3850 (substring-no-properties
3851 (python-info-dedenter-opening-block-message))))
3855 (substring-no-properties
3856 (python-info-dedenter-opening-block-message))))))
3858 (ert-deftest python-info-dedenter-opening-block-message-3 ()
3859 "Test else keyword."
3860 (python-tests-with-temp-buffer
3865 logger.exception('something went wrong')
3867 logger.debug('all good')
3869 (python-tests-look-at "else:")
3872 (substring-no-properties
3873 (python-info-dedenter-opening-block-message))))
3877 (substring-no-properties
3878 (python-info-dedenter-opening-block-message))))))
3880 (ert-deftest python-info-dedenter-opening-block-message-4 ()
3881 "Test finally keyword."
3882 (python-tests-with-temp-buffer
3887 logger.exception('something went wrong')
3889 logger.debug('all good')
3893 (python-tests-look-at "finally:")
3896 (substring-no-properties
3897 (python-info-dedenter-opening-block-message))))
3901 (substring-no-properties
3902 (python-info-dedenter-opening-block-message))))))
3904 (ert-deftest python-info-dedenter-opening-block-message-5 ()
3905 "Test elif keyword."
3906 (python-tests-with-temp-buffer
3912 (python-tests-look-at "elif b:")
3915 (substring-no-properties
3916 (python-info-dedenter-opening-block-message))))
3920 (substring-no-properties
3921 (python-info-dedenter-opening-block-message))))))
3924 (ert-deftest python-info-dedenter-statement-p-1 ()
3925 "Test dedenters inside strings are ignored."
3926 (python-tests-with-temp-buffer
3931 logger.exception('something went wrong')
3934 (python-tests-look-at "except\n")
3935 (should (not (python-info-dedenter-statement-p)))))
3937 (ert-deftest python-info-dedenter-statement-p-2 ()
3938 "Test except keyword."
3939 (python-tests-with-temp-buffer
3944 logger.exception('something went wrong')
3946 (python-tests-look-at "except:")
3947 (should (= (point) (python-info-dedenter-statement-p)))
3949 (should (= (save-excursion
3950 (back-to-indentation)
3952 (python-info-dedenter-statement-p)))))
3954 (ert-deftest python-info-dedenter-statement-p-3 ()
3955 "Test else keyword."
3956 (python-tests-with-temp-buffer
3961 logger.exception('something went wrong')
3963 logger.debug('all good')
3965 (python-tests-look-at "else:")
3966 (should (= (point) (python-info-dedenter-statement-p)))
3968 (should (= (save-excursion
3969 (back-to-indentation)
3971 (python-info-dedenter-statement-p)))))
3973 (ert-deftest python-info-dedenter-statement-p-4 ()
3974 "Test finally keyword."
3975 (python-tests-with-temp-buffer
3980 logger.exception('something went wrong')
3982 logger.debug('all good')
3986 (python-tests-look-at "finally:")
3987 (should (= (point) (python-info-dedenter-statement-p)))
3989 (should (= (save-excursion
3990 (back-to-indentation)
3992 (python-info-dedenter-statement-p)))))
3994 (ert-deftest python-info-dedenter-statement-p-5 ()
3995 "Test elif keyword."
3996 (python-tests-with-temp-buffer
4002 (python-tests-look-at "elif b:")
4003 (should (= (point) (python-info-dedenter-statement-p)))
4005 (should (= (save-excursion
4006 (back-to-indentation)
4008 (python-info-dedenter-statement-p)))))
4010 (ert-deftest python-info-line-ends-backslash-p-1 ()
4011 (python-tests-with-temp-buffer
4013 objects = Thing.objects.all() \\\\
4023 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
4024 (should (python-info-line-ends-backslash-p 3))
4025 (should (python-info-line-ends-backslash-p 4))
4026 (should (python-info-line-ends-backslash-p 5))
4027 (should (python-info-line-ends-backslash-p 6)) ; ) \...
4028 (should (python-info-line-ends-backslash-p 7))
4029 (should (python-info-line-ends-backslash-p 8))
4030 (should (python-info-line-ends-backslash-p 9))
4031 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
4033 (ert-deftest python-info-beginning-of-backslash-1 ()
4034 (python-tests-with-temp-buffer
4036 objects = Thing.objects.all() \\\\
4047 (second (python-tests-look-at ".filter("))
4048 (third (python-tests-look-at ".aggregate(")))
4049 (should (= first (python-info-beginning-of-backslash 2)))
4050 (should (= second (python-info-beginning-of-backslash 3)))
4051 (should (= second (python-info-beginning-of-backslash 4)))
4052 (should (= second (python-info-beginning-of-backslash 5)))
4053 (should (= second (python-info-beginning-of-backslash 6)))
4054 (should (= third (python-info-beginning-of-backslash 7)))
4055 (should (= third (python-info-beginning-of-backslash 8)))
4056 (should (= third (python-info-beginning-of-backslash 9)))
4057 (should (not (python-info-beginning-of-backslash 10))))))
4059 (ert-deftest python-info-continuation-line-p-1 ()
4060 (python-tests-with-temp-buffer
4062 if width == 0 and height == 0 and \\\\
4063 color == 'red' and emphasis == 'strong' or \\\\
4070 (python-tests-look-at "if width == 0 and height == 0 and")
4071 (should (not (python-info-continuation-line-p)))
4072 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
4073 (should (python-info-continuation-line-p))
4074 (python-tests-look-at "highlight > 100:")
4075 (should (python-info-continuation-line-p))
4076 (python-tests-look-at "raise ValueError(")
4077 (should (not (python-info-continuation-line-p)))
4078 (python-tests-look-at "'sorry, you lose'")
4079 (should (python-info-continuation-line-p))
4081 (should (python-info-continuation-line-p))
4082 (python-tests-look-at ")")
4083 (should (python-info-continuation-line-p))
4085 (should (not (python-info-continuation-line-p)))))
4087 (ert-deftest python-info-block-continuation-line-p-1 ()
4088 (python-tests-with-temp-buffer
4090 if width == 0 and height == 0 and \\\\
4091 color == 'red' and emphasis == 'strong' or \\\\
4098 (python-tests-look-at "if width == 0 and")
4099 (should (not (python-info-block-continuation-line-p)))
4100 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
4101 (should (= (python-info-block-continuation-line-p)
4102 (python-tests-look-at "if width == 0 and" -1 t)))
4103 (python-tests-look-at "highlight > 100:")
4104 (should (not (python-info-block-continuation-line-p)))))
4106 (ert-deftest python-info-block-continuation-line-p-2 ()
4107 (python-tests-with-temp-buffer
4114 (python-tests-look-at "def foo(a,")
4115 (should (not (python-info-block-continuation-line-p)))
4116 (python-tests-look-at "b,")
4117 (should (= (python-info-block-continuation-line-p)
4118 (python-tests-look-at "def foo(a," -1 t)))
4119 (python-tests-look-at "c):")
4120 (should (not (python-info-block-continuation-line-p)))))
4122 (ert-deftest python-info-assignment-continuation-line-p-1 ()
4123 (python-tests-with-temp-buffer
4125 data = foo(), bar() \\\\
4129 (python-tests-look-at "data = foo(), bar()")
4130 (should (not (python-info-assignment-continuation-line-p)))
4131 (python-tests-look-at "baz(), 4")
4132 (should (= (python-info-assignment-continuation-line-p)
4133 (python-tests-look-at "foo()," -1 t)))
4134 (python-tests-look-at "5, 6")
4135 (should (not (python-info-assignment-continuation-line-p)))))
4137 (ert-deftest python-info-assignment-continuation-line-p-2 ()
4138 (python-tests-with-temp-buffer
4140 data = (foo(), bar()
4144 (python-tests-look-at "data = (foo(), bar()")
4145 (should (not (python-info-assignment-continuation-line-p)))
4146 (python-tests-look-at "baz(), 4")
4147 (should (= (python-info-assignment-continuation-line-p)
4148 (python-tests-look-at "(foo()," -1 t)))
4149 (python-tests-look-at "5, 6)")
4150 (should (not (python-info-assignment-continuation-line-p)))))
4152 (ert-deftest python-info-looking-at-beginning-of-defun-1 ()
4153 (python-tests-with-temp-buffer
4155 def decorat0r(deff):
4166 (python-tests-look-at "def decorat0r(deff):")
4167 (should (python-info-looking-at-beginning-of-defun))
4168 (python-tests-look-at "def foo(arg):")
4169 (should (not (python-info-looking-at-beginning-of-defun)))
4170 (python-tests-look-at "def wrap():")
4171 (should (python-info-looking-at-beginning-of-defun))
4172 (python-tests-look-at "deff()")
4173 (should (not (python-info-looking-at-beginning-of-defun)))))
4175 (ert-deftest python-info-current-line-comment-p-1 ()
4176 (python-tests-with-temp-buffer
4179 foo = True # another comment
4183 print ('bar') # print bar
4185 (python-tests-look-at "# this is a comment")
4186 (should (python-info-current-line-comment-p))
4187 (python-tests-look-at "foo = True # another comment")
4188 (should (not (python-info-current-line-comment-p)))
4189 (python-tests-look-at "'#this is a string'")
4190 (should (not (python-info-current-line-comment-p)))
4191 (python-tests-look-at "# more comments")
4192 (should (python-info-current-line-comment-p))
4193 (python-tests-look-at "print ('bar') # print bar")
4194 (should (not (python-info-current-line-comment-p)))))
4196 (ert-deftest python-info-current-line-empty-p ()
4197 (python-tests-with-temp-buffer
4201 foo = True # another comment
4203 (should (python-info-current-line-empty-p))
4204 (python-tests-look-at "# this is a comment")
4205 (should (not (python-info-current-line-empty-p)))
4207 (should (python-info-current-line-empty-p))))
4209 (ert-deftest python-info-encoding-from-cookie-1 ()
4210 "Should detect it on first line."
4211 (python-tests-with-temp-buffer
4214 foo = True # another comment
4216 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4218 (ert-deftest python-info-encoding-from-cookie-2 ()
4219 "Should detect it on second line."
4220 (python-tests-with-temp-buffer
4224 foo = True # another comment
4226 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4228 (ert-deftest python-info-encoding-from-cookie-3 ()
4229 "Should not be detected on third line (and following ones)."
4230 (python-tests-with-temp-buffer
4234 foo = True # another comment
4236 (should (not (python-info-encoding-from-cookie)))))
4238 (ert-deftest python-info-encoding-from-cookie-4 ()
4239 "Should detect Emacs style."
4240 (python-tests-with-temp-buffer
4241 "# -*- coding: latin-1 -*-
4243 foo = True # another comment"
4244 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4246 (ert-deftest python-info-encoding-from-cookie-5 ()
4247 "Should detect Vim style."
4248 (python-tests-with-temp-buffer
4249 "# vim: set fileencoding=latin-1 :
4251 foo = True # another comment"
4252 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4254 (ert-deftest python-info-encoding-from-cookie-6 ()
4255 "First cookie wins."
4256 (python-tests-with-temp-buffer
4257 "# -*- coding: iso-8859-1 -*-
4258 # vim: set fileencoding=latin-1 :
4260 foo = True # another comment"
4261 (should (eq (python-info-encoding-from-cookie) 'iso-8859-1))))
4263 (ert-deftest python-info-encoding-from-cookie-7 ()
4264 "First cookie wins."
4265 (python-tests-with-temp-buffer
4266 "# vim: set fileencoding=latin-1 :
4267 # -*- coding: iso-8859-1 -*-
4269 foo = True # another comment"
4270 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4272 (ert-deftest python-info-encoding-1 ()
4273 "Should return the detected encoding from cookie."
4274 (python-tests-with-temp-buffer
4275 "# vim: set fileencoding=latin-1 :
4277 foo = True # another comment"
4278 (should (eq (python-info-encoding) 'latin-1))))
4280 (ert-deftest python-info-encoding-2 ()
4281 "Should default to utf-8."
4282 (python-tests-with-temp-buffer
4283 "# No encoding for you
4285 foo = True # another comment"
4286 (should (eq (python-info-encoding) 'utf-8))))
4289 ;;; Utility functions
4291 (ert-deftest python-util-goto-line-1 ()
4292 (python-tests-with-temp-buffer
4297 pass" (make-string 20 ?\n))
4298 (python-util-goto-line 10)
4299 (should (= (line-number-at-pos) 10))
4300 (python-util-goto-line 20)
4301 (should (= (line-number-at-pos) 20))))
4303 (ert-deftest python-util-clone-local-variables-1 ()
4304 (let ((buffer (generate-new-buffer
4305 "python-util-clone-local-variables-1"))
4307 '((python-fill-docstring-style . django)
4308 (python-shell-interpreter . "python")
4309 (python-shell-interpreter-args . "manage.py shell")
4310 (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
4311 (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
4312 (python-shell-extra-pythonpaths "/home/user/pylib/")
4313 (python-shell-completion-setup-code
4314 . "from IPython.core.completerlib import module_completion")
4315 (python-shell-completion-string-code
4316 . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
4317 (python-shell-virtualenv-root
4318 . "/home/user/.virtualenvs/project"))))
4319 (with-current-buffer buffer
4320 (kill-all-local-variables)
4321 (dolist (ccons varcons)
4322 (set (make-local-variable (car ccons)) (cdr ccons))))
4323 (python-tests-with-temp-buffer
4325 (python-util-clone-local-variables buffer)
4326 (dolist (ccons varcons)
4328 (equal (symbol-value (car ccons)) (cdr ccons)))))
4329 (kill-buffer buffer)))
4331 (ert-deftest python-util-strip-string-1 ()
4332 (should (string= (python-util-strip-string "\t\r\n str") "str"))
4333 (should (string= (python-util-strip-string "str \n\r") "str"))
4334 (should (string= (python-util-strip-string "\t\r\n str \n\r ") "str"))
4336 (string= (python-util-strip-string "\n str \nin \tg \n\r") "str \nin \tg"))
4337 (should (string= (python-util-strip-string "\n \t \n\r ") ""))
4338 (should (string= (python-util-strip-string "") "")))
4340 (ert-deftest python-util-forward-comment-1 ()
4341 (python-tests-with-temp-buffer
4345 # bad indented comment
4346 # more comments" (make-string 9999 ?\n))
4347 (python-util-forward-comment 1)
4348 (should (= (point) (point-max)))
4349 (python-util-forward-comment -1)
4350 (should (= (point) (point-min)))))
4352 (ert-deftest python-util-valid-regexp-p-1 ()
4353 (should (python-util-valid-regexp-p ""))
4354 (should (python-util-valid-regexp-p python-shell-prompt-regexp))
4355 (should (not (python-util-valid-regexp-p "\\("))))
4360 (ert-deftest python-parens-electric-indent-1 ()
4362 (let ((eim electric-indent-mode))
4365 (python-tests-with-temp-buffer
4367 from django.conf.urls import patterns, include, url
4369 from django.contrib import admin
4371 from myapp import views
4374 urlpatterns = patterns('',
4375 url(r'^$', views.index
4378 (electric-indent-mode 1)
4379 (python-tests-look-at "views.index")
4382 ;; Inserting commas within the same line should leave
4383 ;; indentation unchanged.
4384 (python-tests-self-insert ",")
4385 (should (= (current-indentation) 4))
4387 ;; As well as any other input happening within the same
4389 (python-tests-self-insert " name='index')")
4390 (should (= (current-indentation) 4))
4392 ;; But a comma outside it, should trigger indentation.
4393 (python-tests-self-insert ",")
4394 (should (= (current-indentation) 23))
4396 ;; Newline indents to the first argument column
4397 (python-tests-self-insert "\n")
4398 (should (= (current-indentation) 23))
4400 ;; All this input must not change indentation
4402 (python-tests-self-insert "url(r'^/login$', views.login)")
4403 (should (= (current-indentation) 4))
4405 ;; But this comma does
4406 (python-tests-self-insert ",")
4407 (should (= (current-indentation) 23))))
4408 (or eim (electric-indent-mode -1)))))
4410 (ert-deftest python-triple-quote-pairing ()
4412 (let ((epm electric-pair-mode))
4415 (python-tests-with-temp-buffer
4417 (or epm (electric-pair-mode 1))
4418 (goto-char (1- (point-max)))
4419 (python-tests-self-insert ?\")
4420 (should (string= (buffer-string)
4422 (should (= (point) 4)))
4423 (python-tests-with-temp-buffer
4425 (python-tests-self-insert (list ?\" ?\" ?\"))
4426 (should (string= (buffer-string)
4428 (should (= (point) 4)))
4429 (python-tests-with-temp-buffer
4431 (goto-char (1- (point-max)))
4432 (python-tests-self-insert ?\")
4433 (should (= (point) (1- (point-max))))
4434 (should (string= (buffer-string)
4436 (or epm (electric-pair-mode -1)))))
4439 (provide 'python-tests)
4443 ;; indent-tabs-mode: nil
4446 ;;; python-tests.el ends here