1 ;;; python-tests.el --- Test suite for python.el
3 ;; Copyright (C) 2013-2014 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)) 'inside-paren
))
202 (should (= (python-indent-calculate-indentation) 8))
203 (python-tests-look-at "var_four):")
204 (should (eq (car (python-indent-context)) 'inside-paren
))
205 (should (= (python-indent-calculate-indentation) 8))
206 (python-tests-look-at "print (var_one)")
207 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
208 (should (= (python-indent-calculate-indentation) 4))))
210 (ert-deftest python-indent-pep8-3
()
212 (python-tests-with-temp-buffer
213 "# Extra indentation is not necessary.
214 foo = long_function_name(
218 (should (eq (car (python-indent-context)) 'no-indent
))
219 (should (= (python-indent-calculate-indentation) 0))
220 (python-tests-look-at "foo = long_function_name(")
221 (should (eq (car (python-indent-context)) 'after-line
))
222 (should (= (python-indent-calculate-indentation) 0))
223 (python-tests-look-at "var_one, var_two,")
224 (should (eq (car (python-indent-context)) 'inside-paren
))
225 (should (= (python-indent-calculate-indentation) 4))
226 (python-tests-look-at "var_three, var_four)")
227 (should (eq (car (python-indent-context)) 'inside-paren
))
228 (should (= (python-indent-calculate-indentation) 4))))
230 (ert-deftest python-indent-after-comment-1
()
231 "The most simple after-comment case that shouldn't fail."
232 (python-tests-with-temp-buffer
233 "# Contents will be modified to correct indentation
235 def _on_child_complete(self, child_future):
236 if self.in_terminal_state():
238 # We only complete when all our async children have entered a
239 # terminal state. At that point, if any child failed, we fail
240 # with the exception with which the first child failed.
242 (python-tests-look-at "# We only complete")
243 (should (eq (car (python-indent-context)) 'after-line
))
244 (should (= (python-indent-calculate-indentation) 8))
245 (python-tests-look-at "# terminal state")
246 (should (eq (car (python-indent-context)) 'after-comment
))
247 (should (= (python-indent-calculate-indentation) 8))
248 (python-tests-look-at "# with the exception")
249 (should (eq (car (python-indent-context)) 'after-comment
))
250 ;; This one indents relative to previous block, even given the fact
251 ;; that it was under-indented.
252 (should (= (python-indent-calculate-indentation) 4))
253 (python-tests-look-at "# terminal state" -
1)
254 ;; It doesn't hurt to check again.
255 (should (eq (car (python-indent-context)) 'after-comment
))
257 (should (= (current-indentation) 8))
258 (python-tests-look-at "# with the exception")
259 (should (eq (car (python-indent-context)) 'after-comment
))
260 ;; Now everything should be lined up.
261 (should (= (python-indent-calculate-indentation) 8))))
263 (ert-deftest python-indent-after-comment-2
()
264 "Test after-comment in weird cases."
265 (python-tests-with-temp-buffer
266 "# Contents will be modified to correct indentation
270 # This comment is badly indented just because.
271 # But we won't mess with the user in this line.
273 now_we_do_mess_cause_this_is_not_a_comment = 1
277 (python-tests-look-at "# I don't do much")
278 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
279 (should (= (python-indent-calculate-indentation) 4))
280 (python-tests-look-at "return arg")
281 ;; Comment here just gets ignored, this line is not a comment so
282 ;; the rules won't apply here.
283 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
284 (should (= (python-indent-calculate-indentation) 4))
285 (python-tests-look-at "# This comment is badly")
286 (should (eq (car (python-indent-context)) 'after-line
))
287 ;; The return keyword moves indentation backwards 4 spaces, but
288 ;; let's assume this comment was placed there because the user
289 ;; wanted to (manually adding spaces or whatever).
290 (should (= (python-indent-calculate-indentation) 0))
291 (python-tests-look-at "# but we won't mess")
292 (should (eq (car (python-indent-context)) 'after-comment
))
293 (should (= (python-indent-calculate-indentation) 4))
294 ;; Behave the same for blank lines: potentially a comment.
296 (should (eq (car (python-indent-context)) 'after-comment
))
297 (should (= (python-indent-calculate-indentation) 4))
298 (python-tests-look-at "now_we_do_mess")
299 ;; Here is where comment indentation starts to get ignored and
300 ;; where the user can't freely indent anymore.
301 (should (eq (car (python-indent-context)) 'after-line
))
302 (should (= (python-indent-calculate-indentation) 0))
303 (python-tests-look-at "# yeah, that.")
304 (should (eq (car (python-indent-context)) 'after-line
))
305 (should (= (python-indent-calculate-indentation) 0))))
307 (ert-deftest python-indent-inside-paren-1
()
308 "The most simple inside-paren case that shouldn't fail."
309 (python-tests-with-temp-buffer
327 (python-tests-look-at "data = {")
328 (should (eq (car (python-indent-context)) 'after-line
))
329 (should (= (python-indent-calculate-indentation) 0))
330 (python-tests-look-at "'key':")
331 (should (eq (car (python-indent-context)) 'inside-paren
))
332 (should (= (python-indent-calculate-indentation) 4))
333 (python-tests-look-at "{")
334 (should (eq (car (python-indent-context)) 'inside-paren
))
335 (should (= (python-indent-calculate-indentation) 4))
336 (python-tests-look-at "'objlist': [")
337 (should (eq (car (python-indent-context)) 'inside-paren
))
338 (should (= (python-indent-calculate-indentation) 8))
339 (python-tests-look-at "{")
340 (should (eq (car (python-indent-context)) 'inside-paren
))
341 (should (= (python-indent-calculate-indentation) 12))
342 (python-tests-look-at "'pk': 1,")
343 (should (eq (car (python-indent-context)) 'inside-paren
))
344 (should (= (python-indent-calculate-indentation) 16))
345 (python-tests-look-at "'name': 'first',")
346 (should (eq (car (python-indent-context)) 'inside-paren
))
347 (should (= (python-indent-calculate-indentation) 16))
348 (python-tests-look-at "},")
349 (should (eq (car (python-indent-context)) 'inside-paren
))
350 (should (= (python-indent-calculate-indentation) 12))
351 (python-tests-look-at "{")
352 (should (eq (car (python-indent-context)) 'inside-paren
))
353 (should (= (python-indent-calculate-indentation) 12))
354 (python-tests-look-at "'pk': 2,")
355 (should (eq (car (python-indent-context)) 'inside-paren
))
356 (should (= (python-indent-calculate-indentation) 16))
357 (python-tests-look-at "'name': 'second',")
358 (should (eq (car (python-indent-context)) 'inside-paren
))
359 (should (= (python-indent-calculate-indentation) 16))
360 (python-tests-look-at "}")
361 (should (eq (car (python-indent-context)) 'inside-paren
))
362 (should (= (python-indent-calculate-indentation) 12))
363 (python-tests-look-at "]")
364 (should (eq (car (python-indent-context)) 'inside-paren
))
365 (should (= (python-indent-calculate-indentation) 8))
366 (python-tests-look-at "}")
367 (should (eq (car (python-indent-context)) 'inside-paren
))
368 (should (= (python-indent-calculate-indentation) 4))
369 (python-tests-look-at "}")
370 (should (eq (car (python-indent-context)) 'inside-paren
))
371 (should (= (python-indent-calculate-indentation) 0))))
373 (ert-deftest python-indent-inside-paren-2
()
374 "Another more compact paren group style."
375 (python-tests-with-temp-buffer
386 (python-tests-look-at "data = {")
387 (should (eq (car (python-indent-context)) 'after-line
))
388 (should (= (python-indent-calculate-indentation) 0))
389 (python-tests-look-at "'objlist': [")
390 (should (eq (car (python-indent-context)) 'inside-paren
))
391 (should (= (python-indent-calculate-indentation) 4))
392 (python-tests-look-at "{'pk': 1,")
393 (should (eq (car (python-indent-context)) 'inside-paren
))
394 (should (= (python-indent-calculate-indentation) 8))
395 (python-tests-look-at "'name': 'first'},")
396 (should (eq (car (python-indent-context)) 'inside-paren
))
397 (should (= (python-indent-calculate-indentation) 9))
398 (python-tests-look-at "{'pk': 2,")
399 (should (eq (car (python-indent-context)) 'inside-paren
))
400 (should (= (python-indent-calculate-indentation) 8))
401 (python-tests-look-at "'name': 'second'}")
402 (should (eq (car (python-indent-context)) 'inside-paren
))
403 (should (= (python-indent-calculate-indentation) 9))
404 (python-tests-look-at "]")
405 (should (eq (car (python-indent-context)) 'inside-paren
))
406 (should (= (python-indent-calculate-indentation) 4))
407 (python-tests-look-at "}}")
408 (should (eq (car (python-indent-context)) 'inside-paren
))
409 (should (= (python-indent-calculate-indentation) 0))
410 (python-tests-look-at "}")
411 (should (eq (car (python-indent-context)) 'inside-paren
))
412 (should (= (python-indent-calculate-indentation) 0))))
414 (ert-deftest python-indent-after-block-1
()
415 "The most simple after-block case that shouldn't fail."
416 (python-tests-with-temp-buffer
418 def foo(a, b, c=True):
420 (should (eq (car (python-indent-context)) 'no-indent
))
421 (should (= (python-indent-calculate-indentation) 0))
422 (goto-char (point-max))
423 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
424 (should (= (python-indent-calculate-indentation) 4))))
426 (ert-deftest python-indent-after-block-2
()
427 "A weird (malformed) multiline block statement."
428 (python-tests-with-temp-buffer
434 (goto-char (point-max))
435 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
436 (should (= (python-indent-calculate-indentation) 4))))
438 (ert-deftest python-indent-after-backslash-1
()
439 "The most common case."
440 (python-tests-with-temp-buffer
442 from foo.bar.baz import something, something_1 \\\\
443 something_2 something_3, \\\\
444 something_4, something_5
446 (python-tests-look-at "from foo.bar.baz import something, something_1")
447 (should (eq (car (python-indent-context)) 'after-line
))
448 (should (= (python-indent-calculate-indentation) 0))
449 (python-tests-look-at "something_2 something_3,")
450 (should (eq (car (python-indent-context)) 'after-backslash
))
451 (should (= (python-indent-calculate-indentation) 4))
452 (python-tests-look-at "something_4, something_5")
453 (should (eq (car (python-indent-context)) 'after-backslash
))
454 (should (= (python-indent-calculate-indentation) 4))
455 (goto-char (point-max))
456 (should (eq (car (python-indent-context)) 'after-line
))
457 (should (= (python-indent-calculate-indentation) 0))))
459 (ert-deftest python-indent-after-backslash-2
()
460 "A pretty extreme complicated case."
461 (python-tests-with-temp-buffer
463 objects = Thing.objects.all() \\\\
473 (python-tests-look-at "objects = Thing.objects.all()")
474 (should (eq (car (python-indent-context)) 'after-line
))
475 (should (= (python-indent-calculate-indentation) 0))
476 (python-tests-look-at ".filter(")
477 (should (eq (car (python-indent-context)) 'after-backslash
))
478 (should (= (python-indent-calculate-indentation) 23))
479 (python-tests-look-at "type='toy',")
480 (should (eq (car (python-indent-context)) 'inside-paren
))
481 (should (= (python-indent-calculate-indentation) 27))
482 (python-tests-look-at "status='bought'")
483 (should (eq (car (python-indent-context)) 'inside-paren
))
484 (should (= (python-indent-calculate-indentation) 27))
485 (python-tests-look-at ") \\\\")
486 (should (eq (car (python-indent-context)) 'inside-paren
))
487 (should (= (python-indent-calculate-indentation) 23))
488 (python-tests-look-at ".aggregate(")
489 (should (eq (car (python-indent-context)) 'after-backslash
))
490 (should (= (python-indent-calculate-indentation) 23))
491 (python-tests-look-at "Sum('amount')")
492 (should (eq (car (python-indent-context)) 'inside-paren
))
493 (should (= (python-indent-calculate-indentation) 27))
494 (python-tests-look-at ") \\\\")
495 (should (eq (car (python-indent-context)) 'inside-paren
))
496 (should (= (python-indent-calculate-indentation) 23))
497 (python-tests-look-at ".values_list()")
498 (should (eq (car (python-indent-context)) 'after-backslash
))
499 (should (= (python-indent-calculate-indentation) 23))
501 (should (eq (car (python-indent-context)) 'after-line
))
502 (should (= (python-indent-calculate-indentation) 0))))
504 (ert-deftest python-indent-block-enders-1
()
505 "Test de-indentation for pass keyword."
506 (python-tests-with-temp-buffer
519 (python-tests-look-at "3)")
521 (should (= (python-indent-calculate-indentation) 8))
522 (python-tests-look-at "pass")
524 (should (= (python-indent-calculate-indentation) 8))))
526 (ert-deftest python-indent-block-enders-2
()
527 "Test de-indentation for return keyword."
528 (python-tests-with-temp-buffer
531 '''raise lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
533 eiusmod tempor incididunt ut labore et dolore magna aliqua.
536 \"return (1, 2, 3).\"
542 (python-tests-look-at "def")
543 (should (= (python-indent-calculate-indentation) 4))
544 (python-tests-look-at "if")
545 (should (= (python-indent-calculate-indentation) 8))
546 (python-tests-look-at "return")
547 (should (= (python-indent-calculate-indentation) 12))
548 (goto-char (point-max))
549 (should (= (python-indent-calculate-indentation) 8))))
551 (ert-deftest python-indent-block-enders-3
()
552 "Test de-indentation for continue keyword."
553 (python-tests-with-temp-buffer
559 (python-tests-look-at "if")
560 (should (= (python-indent-calculate-indentation) 4))
561 (python-tests-look-at "continue")
562 (should (= (python-indent-calculate-indentation) 8))
564 (should (= (python-indent-calculate-indentation) 4))))
566 (ert-deftest python-indent-block-enders-4
()
567 "Test de-indentation for break keyword."
568 (python-tests-with-temp-buffer
574 (python-tests-look-at "if")
575 (should (= (python-indent-calculate-indentation) 4))
576 (python-tests-look-at "break")
577 (should (= (python-indent-calculate-indentation) 8))
579 (should (= (python-indent-calculate-indentation) 4))))
581 (ert-deftest python-indent-block-enders-5
()
582 "Test de-indentation for raise keyword."
583 (python-tests-with-temp-buffer
587 raise ValueError('Element cannot be None')
589 (python-tests-look-at "if")
590 (should (= (python-indent-calculate-indentation) 4))
591 (python-tests-look-at "raise")
592 (should (= (python-indent-calculate-indentation) 8))
594 (should (= (python-indent-calculate-indentation) 4))))
596 (ert-deftest python-indent-dedenters-1
()
597 "Test de-indentation for the elif keyword."
598 (python-tests-with-temp-buffer
607 (python-tests-look-at "elif\n")
608 (should (eq (car (python-indent-context)) 'dedenter-statement
))
609 (should (= (python-indent-calculate-indentation) 0))
610 (should (equal (python-indent-calculate-levels) '(0)))))
612 (ert-deftest python-indent-dedenters-2
()
613 "Test de-indentation for the else keyword."
614 (python-tests-with-temp-buffer
620 msg = 'Error saving to disk'
622 logger.exception(msg)
625 logger.exception('Unhandled exception')
630 (python-tests-look-at "else\n")
631 (should (eq (car (python-indent-context)) 'dedenter-statement
))
632 (should (= (python-indent-calculate-indentation) 8))
633 (should (equal (python-indent-calculate-levels) '(0 4 8)))))
635 (ert-deftest python-indent-dedenters-3
()
636 "Test de-indentation for the except keyword."
637 (python-tests-with-temp-buffer
644 (python-tests-look-at "except\n")
645 (should (eq (car (python-indent-context)) 'dedenter-statement
))
646 (should (= (python-indent-calculate-indentation) 4))
647 (should (equal (python-indent-calculate-levels) '(4)))))
649 (ert-deftest python-indent-dedenters-4
()
650 "Test de-indentation for the finally keyword."
651 (python-tests-with-temp-buffer
658 (python-tests-look-at "finally\n")
659 (should (eq (car (python-indent-context)) 'dedenter-statement
))
660 (should (= (python-indent-calculate-indentation) 4))
661 (should (equal (python-indent-calculate-levels) '(4)))))
663 (ert-deftest python-indent-dedenters-5
()
664 "Test invalid levels are skipped in a complex example."
665 (python-tests-with-temp-buffer
671 msg = 'Error saving to disk'
673 logger.exception(msg)
679 (python-tests-look-at "else\n")
680 (should (eq (car (python-indent-context)) 'dedenter-statement
))
681 (should (= (python-indent-calculate-indentation) 8))
682 (should (equal (python-indent-calculate-levels) '(0 8)))))
684 (ert-deftest python-indent-dedenters-6
()
685 "Test indentation is zero when no opening block for dedenter."
686 (python-tests-with-temp-buffer
693 (python-tests-look-at "else\n")
694 (should (eq (car (python-indent-context)) 'dedenter-statement
))
695 (should (= (python-indent-calculate-indentation) 0))
696 (should (equal (python-indent-calculate-levels) '(0)))))
698 (ert-deftest python-indent-dedenters-7
()
699 "Test indentation case from Bug#15163."
700 (python-tests-with-temp-buffer
709 (python-tests-look-at "else:" 2)
710 (should (eq (car (python-indent-context)) 'dedenter-statement
))
711 (should (= (python-indent-calculate-indentation) 0))
712 (should (equal (python-indent-calculate-levels) '(0)))))
714 (ert-deftest python-indent-dedenters-8
()
715 "Test indentation for Bug#18432."
716 (python-tests-with-temp-buffer
724 (python-tests-look-at "a == 4):\n")
725 (should (eq (car (python-indent-context)) 'inside-paren
))
726 (should (= (python-indent-calculate-indentation) 6))
727 (should (equal (python-indent-calculate-levels) '(0 4 6)))))
729 (ert-deftest python-indent-electric-colon-1
()
730 "Test indentation case from Bug#18228."
731 (python-tests-with-temp-buffer
738 (python-tests-look-at "def b()")
739 (goto-char (line-end-position))
740 (python-tests-self-insert ":")
741 (should (= (current-indentation) 0))))
743 (ert-deftest python-indent-region-1
()
744 "Test indentation case from Bug#18843."
752 (python-tests-with-temp-buffer
754 (python-indent-region (point-min) (point-max))
755 (should (string= (buffer-substring-no-properties (point-min) (point-max))
758 (ert-deftest python-indent-region-2
()
759 "Test region indentation on comments."
769 (python-tests-with-temp-buffer
771 (python-indent-region (point-min) (point-max))
772 (should (string= (buffer-substring-no-properties (point-min) (point-max))
775 (ert-deftest python-indent-region-3
()
776 "Test region indentation on comments."
793 (python-tests-with-temp-buffer
795 (python-indent-region (point-min) (point-max))
796 (should (string= (buffer-substring-no-properties (point-min) (point-max))
799 (ert-deftest python-indent-region-4
()
800 "Test region indentation block starts, dedenders and enders."
817 (python-tests-with-temp-buffer
819 (python-indent-region (point-min) (point-max))
820 (should (string= (buffer-substring-no-properties (point-min) (point-max))
823 (ert-deftest python-indent-region-5
()
824 "Test region indentation leaves strings untouched (start delimiter)."
841 (python-tests-with-temp-buffer
843 (python-indent-region (point-min) (point-max))
844 (should (string= (buffer-substring-no-properties (point-min) (point-max))
850 (ert-deftest python-nav-beginning-of-defun-1
()
851 (python-tests-with-temp-buffer
853 def decoratorFunctionWithArguments(arg1, arg2, arg3):
854 '''print decorated function call data to stdout.
858 @decoratorFunctionWithArguments('arg1', 'arg2')
859 def func(a, b, c=True):
864 print 'Inside wwrap()'
865 def wrapped_f(*args):
866 print 'Inside wrapped_f()'
867 print 'Decorator arguments:', arg1, arg2, arg3
869 print 'After f(*args)'
873 (python-tests-look-at "return wrap")
874 (should (= (save-excursion
875 (python-nav-beginning-of-defun)
878 (python-tests-look-at "def wrapped_f(*args):" -
1)
881 (python-tests-look-at "def wrapped_f(*args):" -
1)
882 (should (= (save-excursion
883 (python-nav-beginning-of-defun)
886 (python-tests-look-at "def wwrap(f):" -
1)
889 (python-tests-look-at "def wwrap(f):" -
1)
890 (should (= (save-excursion
891 (python-nav-beginning-of-defun)
894 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)
898 (ert-deftest python-nav-beginning-of-defun-2
()
899 (python-tests-with-temp-buffer
915 ;; Nested defuns, are handled with care.
916 (python-tests-look-at "def c(self):")
917 (should (= (save-excursion
918 (python-nav-beginning-of-defun)
921 (python-tests-look-at "def m(self):" -
1)
924 ;; Defuns on same levels should be respected.
925 (python-tests-look-at "def a():" -
1)
926 (should (= (save-excursion
927 (python-nav-beginning-of-defun)
930 (python-tests-look-at "def b():" -
1)
933 ;; Jump to a top level defun.
934 (python-tests-look-at "def b():" -
1)
935 (should (= (save-excursion
936 (python-nav-beginning-of-defun)
939 (python-tests-look-at "def m(self):" -
1)
942 ;; Jump to a top level defun again.
943 (python-tests-look-at "def m(self):" -
1)
944 (should (= (save-excursion
945 (python-nav-beginning-of-defun)
948 (python-tests-look-at "class C(object):" -
1)
952 (ert-deftest python-nav-end-of-defun-1
()
953 (python-tests-with-temp-buffer
969 (should (= (save-excursion
970 (python-tests-look-at "class C(object):")
971 (python-nav-end-of-defun)
975 (should (= (save-excursion
976 (python-tests-look-at "def m(self):")
977 (python-nav-end-of-defun)
980 (python-tests-look-at "def c(self):")
983 (should (= (save-excursion
984 (python-tests-look-at "def b():")
985 (python-nav-end-of-defun)
988 (python-tests-look-at "def b():")
991 (should (= (save-excursion
992 (python-tests-look-at "def c(self):")
993 (python-nav-end-of-defun)
998 (ert-deftest python-nav-end-of-defun-2
()
999 (python-tests-with-temp-buffer
1001 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1002 '''print decorated function call data to stdout.
1006 @decoratorFunctionWithArguments('arg1', 'arg2')
1007 def func(a, b, c=True):
1012 print 'Inside wwrap()'
1013 def wrapped_f(*args):
1014 print 'Inside wrapped_f()'
1015 print 'Decorator arguments:', arg1, arg2, arg3
1017 print 'After f(*args)'
1021 (should (= (save-excursion
1022 (python-tests-look-at "def decoratorFunctionWithArguments")
1023 (python-nav-end-of-defun)
1027 (should (= (save-excursion
1028 (python-tests-look-at "@decoratorFunctionWithArguments")
1029 (python-nav-end-of-defun)
1033 (should (= (save-excursion
1034 (python-tests-look-at "def wwrap(f):")
1035 (python-nav-end-of-defun)
1038 (python-tests-look-at "return wwrap")
1039 (line-beginning-position))))
1040 (should (= (save-excursion
1041 (python-tests-look-at "def wrapped_f(*args):")
1042 (python-nav-end-of-defun)
1045 (python-tests-look-at "return wrapped_f")
1046 (line-beginning-position))))
1047 (should (= (save-excursion
1048 (python-tests-look-at "f(*args)")
1049 (python-nav-end-of-defun)
1052 (python-tests-look-at "return wrapped_f")
1053 (line-beginning-position))))))
1055 (ert-deftest python-nav-backward-defun-1
()
1056 (python-tests-with-temp-buffer
1058 class A(object): # A
1066 class B(object): # B
1068 class C(object): # C
1082 (goto-char (point-max))
1083 (should (= (save-excursion (python-nav-backward-defun))
1084 (python-tests-look-at " def c(self): # c" -
1)))
1085 (should (= (save-excursion (python-nav-backward-defun))
1086 (python-tests-look-at " def d(self): # d" -
1)))
1087 (should (= (save-excursion (python-nav-backward-defun))
1088 (python-tests-look-at " class C(object): # C" -
1)))
1089 (should (= (save-excursion (python-nav-backward-defun))
1090 (python-tests-look-at " class B(object): # B" -
1)))
1091 (should (= (save-excursion (python-nav-backward-defun))
1092 (python-tests-look-at " def b(self): # b" -
1)))
1093 (should (= (save-excursion (python-nav-backward-defun))
1094 (python-tests-look-at " def a(self): # a" -
1)))
1095 (should (= (save-excursion (python-nav-backward-defun))
1096 (python-tests-look-at "class A(object): # A" -
1)))
1097 (should (not (python-nav-backward-defun)))))
1099 (ert-deftest python-nav-backward-defun-2
()
1100 (python-tests-with-temp-buffer
1102 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1103 '''print decorated function call data to stdout.
1107 @decoratorFunctionWithArguments('arg1', 'arg2')
1108 def func(a, b, c=True):
1113 print 'Inside wwrap()'
1114 def wrapped_f(*args):
1115 print 'Inside wrapped_f()'
1116 print 'Decorator arguments:', arg1, arg2, arg3
1118 print 'After f(*args)'
1122 (goto-char (point-max))
1123 (should (= (save-excursion (python-nav-backward-defun))
1124 (python-tests-look-at " def wrapped_f(*args):" -
1)))
1125 (should (= (save-excursion (python-nav-backward-defun))
1126 (python-tests-look-at " def wwrap(f):" -
1)))
1127 (should (= (save-excursion (python-nav-backward-defun))
1128 (python-tests-look-at "def decoratorFunctionWithArguments(arg1, arg2, arg3):" -
1)))
1129 (should (not (python-nav-backward-defun)))))
1131 (ert-deftest python-nav-backward-defun-3
()
1132 (python-tests-with-temp-buffer
1148 (goto-char (point-min))
1149 (let ((point (python-tests-look-at "class A(object):")))
1150 (should (not (python-nav-backward-defun)))
1151 (should (= point
(point))))))
1153 (ert-deftest python-nav-forward-defun-1
()
1154 (python-tests-with-temp-buffer
1156 class A(object): # A
1164 class B(object): # B
1166 class C(object): # C
1180 (goto-char (point-min))
1181 (should (= (save-excursion (python-nav-forward-defun))
1182 (python-tests-look-at "(object): # A")))
1183 (should (= (save-excursion (python-nav-forward-defun))
1184 (python-tests-look-at "(self): # a")))
1185 (should (= (save-excursion (python-nav-forward-defun))
1186 (python-tests-look-at "(self): # b")))
1187 (should (= (save-excursion (python-nav-forward-defun))
1188 (python-tests-look-at "(object): # B")))
1189 (should (= (save-excursion (python-nav-forward-defun))
1190 (python-tests-look-at "(object): # C")))
1191 (should (= (save-excursion (python-nav-forward-defun))
1192 (python-tests-look-at "(self): # d")))
1193 (should (= (save-excursion (python-nav-forward-defun))
1194 (python-tests-look-at "(self): # c")))
1195 (should (not (python-nav-forward-defun)))))
1197 (ert-deftest python-nav-forward-defun-2
()
1198 (python-tests-with-temp-buffer
1200 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1201 '''print decorated function call data to stdout.
1205 @decoratorFunctionWithArguments('arg1', 'arg2')
1206 def func(a, b, c=True):
1211 print 'Inside wwrap()'
1212 def wrapped_f(*args):
1213 print 'Inside wrapped_f()'
1214 print 'Decorator arguments:', arg1, arg2, arg3
1216 print 'After f(*args)'
1220 (goto-char (point-min))
1221 (should (= (save-excursion (python-nav-forward-defun))
1222 (python-tests-look-at "(arg1, arg2, arg3):")))
1223 (should (= (save-excursion (python-nav-forward-defun))
1224 (python-tests-look-at "(f):")))
1225 (should (= (save-excursion (python-nav-forward-defun))
1226 (python-tests-look-at "(*args):")))
1227 (should (not (python-nav-forward-defun)))))
1229 (ert-deftest python-nav-forward-defun-3
()
1230 (python-tests-with-temp-buffer
1246 (goto-char (point-min))
1247 (let ((point (python-tests-look-at "(object):")))
1248 (should (not (python-nav-forward-defun)))
1249 (should (= point
(point))))))
1251 (ert-deftest python-nav-beginning-of-statement-1
()
1252 (python-tests-with-temp-buffer
1262 v3 = ('this is a string'
1267 # this is a comment, yo
1268 'continue previous line')
1274 (python-tests-look-at "v2 =")
1275 (python-util-forward-comment -
1)
1276 (should (= (save-excursion
1277 (python-nav-beginning-of-statement)
1279 (python-tests-look-at "v1 =" -
1 t
)))
1280 (python-tests-look-at "v3 =")
1281 (python-util-forward-comment -
1)
1282 (should (= (save-excursion
1283 (python-nav-beginning-of-statement)
1285 (python-tests-look-at "v2 =" -
1 t
)))
1286 (python-tests-look-at "v4 =")
1287 (python-util-forward-comment -
1)
1288 (should (= (save-excursion
1289 (python-nav-beginning-of-statement)
1291 (python-tests-look-at "v3 =" -
1 t
)))
1292 (goto-char (point-max))
1293 (python-util-forward-comment -
1)
1294 (should (= (save-excursion
1295 (python-nav-beginning-of-statement)
1297 (python-tests-look-at "v4 =" -
1 t
)))))
1299 (ert-deftest python-nav-end-of-statement-1
()
1300 (python-tests-with-temp-buffer
1310 v3 = ('this is a string'
1315 # this is a comment, yo
1316 'continue previous line')
1322 (python-tests-look-at "v1 =")
1323 (should (= (save-excursion
1324 (python-nav-end-of-statement)
1327 (python-tests-look-at "789")
1328 (line-end-position))))
1329 (python-tests-look-at "v2 =")
1330 (should (= (save-excursion
1331 (python-nav-end-of-statement)
1334 (python-tests-look-at "value4)")
1335 (line-end-position))))
1336 (python-tests-look-at "v3 =")
1337 (should (= (save-excursion
1338 (python-nav-end-of-statement)
1341 (python-tests-look-at
1342 "'continue previous line')")
1343 (line-end-position))))
1344 (python-tests-look-at "v4 =")
1345 (should (= (save-excursion
1346 (python-nav-end-of-statement)
1349 (goto-char (point-max))
1350 (python-util-forward-comment -
1)
1353 (ert-deftest python-nav-forward-statement-1
()
1354 (python-tests-with-temp-buffer
1364 v3 = ('this is a string'
1369 # this is a comment, yo
1370 'continue previous line')
1376 (python-tests-look-at "v1 =")
1377 (should (= (save-excursion
1378 (python-nav-forward-statement)
1380 (python-tests-look-at "v2 =")))
1381 (should (= (save-excursion
1382 (python-nav-forward-statement)
1384 (python-tests-look-at "v3 =")))
1385 (should (= (save-excursion
1386 (python-nav-forward-statement)
1388 (python-tests-look-at "v4 =")))
1389 (should (= (save-excursion
1390 (python-nav-forward-statement)
1394 (ert-deftest python-nav-backward-statement-1
()
1395 (python-tests-with-temp-buffer
1405 v3 = ('this is a string'
1410 # this is a comment, yo
1411 'continue previous line')
1417 (goto-char (point-max))
1418 (should (= (save-excursion
1419 (python-nav-backward-statement)
1421 (python-tests-look-at "v4 =" -
1)))
1422 (should (= (save-excursion
1423 (python-nav-backward-statement)
1425 (python-tests-look-at "v3 =" -
1)))
1426 (should (= (save-excursion
1427 (python-nav-backward-statement)
1429 (python-tests-look-at "v2 =" -
1)))
1430 (should (= (save-excursion
1431 (python-nav-backward-statement)
1433 (python-tests-look-at "v1 =" -
1)))))
1435 (ert-deftest python-nav-backward-statement-2
()
1436 :expected-result
:failed
1437 (python-tests-with-temp-buffer
1448 ;; FIXME: For some reason `python-nav-backward-statement' is moving
1449 ;; back two sentences when starting from 'value4)'.
1450 (goto-char (point-max))
1451 (python-util-forward-comment -
1)
1452 (should (= (save-excursion
1453 (python-nav-backward-statement)
1455 (python-tests-look-at "v2 =" -
1 t
)))))
1457 (ert-deftest python-nav-beginning-of-block-1
()
1458 (python-tests-with-temp-buffer
1460 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1461 '''print decorated function call data to stdout.
1465 @decoratorFunctionWithArguments('arg1', 'arg2')
1466 def func(a, b, c=True):
1471 print 'Inside wwrap()'
1472 def wrapped_f(*args):
1473 print 'Inside wrapped_f()'
1474 print 'Decorator arguments:', arg1, arg2, arg3
1476 print 'After f(*args)'
1480 (python-tests-look-at "return wwrap")
1481 (should (= (save-excursion
1482 (python-nav-beginning-of-block)
1484 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)))
1485 (python-tests-look-at "print 'Inside wwrap()'")
1486 (should (= (save-excursion
1487 (python-nav-beginning-of-block)
1489 (python-tests-look-at "def wwrap(f):" -
1)))
1490 (python-tests-look-at "print 'After f(*args)'")
1492 (should (= (save-excursion
1493 (python-nav-beginning-of-block)
1495 (python-tests-look-at "def wrapped_f(*args):" -
1)))
1496 (python-tests-look-at "return wrapped_f")
1497 (should (= (save-excursion
1498 (python-nav-beginning-of-block)
1500 (python-tests-look-at "def wwrap(f):" -
1)))))
1502 (ert-deftest python-nav-end-of-block-1
()
1503 (python-tests-with-temp-buffer
1505 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1506 '''print decorated function call data to stdout.
1510 @decoratorFunctionWithArguments('arg1', 'arg2')
1511 def func(a, b, c=True):
1516 print 'Inside wwrap()'
1517 def wrapped_f(*args):
1518 print 'Inside wrapped_f()'
1519 print 'Decorator arguments:', arg1, arg2, arg3
1521 print 'After f(*args)'
1525 (python-tests-look-at "def decoratorFunctionWithArguments")
1526 (should (= (save-excursion
1527 (python-nav-end-of-block)
1530 (goto-char (point-max))
1531 (python-util-forward-comment -
1)
1533 (python-tests-look-at "def wwrap(f):")
1534 (should (= (save-excursion
1535 (python-nav-end-of-block)
1538 (python-tests-look-at "return wrapped_f")
1539 (line-end-position))))
1541 (should (= (save-excursion
1542 (python-nav-end-of-block)
1545 (python-tests-look-at "return wrapped_f")
1546 (line-end-position))))
1547 (python-tests-look-at "f(*args)")
1548 (should (= (save-excursion
1549 (python-nav-end-of-block)
1552 (python-tests-look-at "print 'After f(*args)'")
1553 (line-end-position))))))
1555 (ert-deftest python-nav-forward-block-1
()
1556 "This also accounts as a test for `python-nav-backward-block'."
1557 (python-tests-with-temp-buffer
1559 if request.user.is_authenticated():
1563 profile = request.user.get_profile()
1564 except Profile.DoesNotExist:
1565 profile = Profile.objects.create(user=request.user)
1568 profile.recalculate_stats()
1570 profile.clear_stats()
1575 (should (= (save-excursion (python-nav-forward-block))
1576 (python-tests-look-at "if request.user.is_authenticated():")))
1577 (should (= (save-excursion (python-nav-forward-block))
1578 (python-tests-look-at "try:")))
1579 (should (= (save-excursion (python-nav-forward-block))
1580 (python-tests-look-at "except Profile.DoesNotExist:")))
1581 (should (= (save-excursion (python-nav-forward-block))
1582 (python-tests-look-at "else:")))
1583 (should (= (save-excursion (python-nav-forward-block))
1584 (python-tests-look-at "if profile.stats:")))
1585 (should (= (save-excursion (python-nav-forward-block))
1586 (python-tests-look-at "else:")))
1587 (should (= (save-excursion (python-nav-forward-block))
1588 (python-tests-look-at "finally:")))
1589 ;; When point is at the last block, leave it there and return nil
1590 (should (not (save-excursion (python-nav-forward-block))))
1591 ;; Move backwards, and even if the number of moves is less than the
1592 ;; provided argument return the point.
1593 (should (= (save-excursion (python-nav-forward-block -
10))
1594 (python-tests-look-at
1595 "if request.user.is_authenticated():" -
1)))))
1597 (ert-deftest python-nav-forward-sexp-1
()
1598 (python-tests-with-temp-buffer
1604 (python-tests-look-at "a()")
1605 (python-nav-forward-sexp)
1606 (should (looking-at "$"))
1607 (should (save-excursion
1609 (looking-at "a()")))
1610 (python-nav-forward-sexp)
1611 (should (looking-at "$"))
1612 (should (save-excursion
1614 (looking-at "b()")))
1615 (python-nav-forward-sexp)
1616 (should (looking-at "$"))
1617 (should (save-excursion
1619 (looking-at "c()")))
1620 ;; Movement next to a paren should do what lisp does and
1621 ;; unfortunately It can't change, because otherwise
1622 ;; `blink-matching-open' breaks.
1623 (python-nav-forward-sexp -
1)
1624 (should (looking-at "()"))
1625 (should (save-excursion
1627 (looking-at "c()")))
1628 (python-nav-forward-sexp -
1)
1629 (should (looking-at "c()"))
1630 (python-nav-forward-sexp -
1)
1631 (should (looking-at "b()"))
1632 (python-nav-forward-sexp -
1)
1633 (should (looking-at "a()"))))
1635 (ert-deftest python-nav-forward-sexp-2
()
1636 (python-tests-with-temp-buffer
1645 (python-tests-look-at "aa =")
1646 (python-nav-forward-sexp)
1647 (should (looking-at " = bbb"))
1648 (python-nav-forward-sexp)
1649 (should (looking-at "$"))
1650 (should (save-excursion
1651 (back-to-indentation)
1652 (looking-at "aaa = bbb")))
1653 (python-nav-forward-sexp)
1654 (should (looking-at "$"))
1655 (should (save-excursion
1656 (back-to-indentation)
1657 (looking-at "ccc = ddd")))
1658 (python-nav-forward-sexp)
1659 (should (looking-at "$"))
1660 (should (save-excursion
1661 (back-to-indentation)
1662 (looking-at "eee = fff")))
1663 (python-nav-forward-sexp)
1664 (should (looking-at "$"))
1665 (should (save-excursion
1666 (back-to-indentation)
1667 (looking-at "return ggg")))
1668 (python-nav-forward-sexp -
1)
1669 (should (looking-at "def func():"))))
1671 (ert-deftest python-nav-forward-sexp-3
()
1672 (python-tests-with-temp-buffer
1674 from some_module import some_sub_module
1675 from another_module import another_sub_module
1677 def another_statement():
1680 (python-tests-look-at "some_module")
1681 (python-nav-forward-sexp)
1682 (should (looking-at " import"))
1683 (python-nav-forward-sexp)
1684 (should (looking-at " some_sub_module"))
1685 (python-nav-forward-sexp)
1686 (should (looking-at "$"))
1689 (back-to-indentation)
1691 "from some_module import some_sub_module")))
1692 (python-nav-forward-sexp)
1693 (should (looking-at "$"))
1696 (back-to-indentation)
1698 "from another_module import another_sub_module")))
1699 (python-nav-forward-sexp)
1700 (should (looking-at "$"))
1703 (back-to-indentation)
1706 (python-nav-forward-sexp -
1)
1707 (should (looking-at "def another_statement():"))
1708 (python-nav-forward-sexp -
1)
1709 (should (looking-at "from another_module import another_sub_module"))
1710 (python-nav-forward-sexp -
1)
1711 (should (looking-at "from some_module import some_sub_module"))))
1713 (ert-deftest python-nav-forward-sexp-safe-1
()
1714 (python-tests-with-temp-buffer
1716 profile = Profile.objects.create(user=request.user)
1719 (python-tests-look-at "profile =")
1720 (python-nav-forward-sexp-safe 1)
1721 (should (looking-at "$"))
1722 (beginning-of-line 1)
1723 (python-tests-look-at "user=request.user")
1724 (python-nav-forward-sexp-safe -
1)
1725 (should (looking-at "(user=request.user)"))
1726 (python-nav-forward-sexp-safe -
4)
1727 (should (looking-at "profile ="))
1728 (python-tests-look-at "user=request.user")
1729 (python-nav-forward-sexp-safe 3)
1730 (should (looking-at ")"))
1731 (python-nav-forward-sexp-safe 1)
1732 (should (looking-at "$"))
1733 (python-nav-forward-sexp-safe 1)
1734 (should (looking-at "$"))))
1736 (ert-deftest python-nav-up-list-1
()
1737 (python-tests-with-temp-buffer
1741 return [i for i in range(3)]
1743 (python-tests-look-at "3)]")
1744 (python-nav-up-list)
1745 (should (looking-at "]"))
1746 (python-nav-up-list)
1747 (should (looking-at "$"))))
1749 (ert-deftest python-nav-backward-up-list-1
()
1750 :expected-result
:failed
1751 (python-tests-with-temp-buffer
1755 return [i for i in range(3)]
1757 (python-tests-look-at "3)]")
1758 (python-nav-backward-up-list)
1759 (should (looking-at "(3)\\]"))
1760 (python-nav-backward-up-list)
1762 "\\[i for i in range(3)\\]"))
1763 ;; FIXME: Need to move to beginning-of-statement.
1764 (python-nav-backward-up-list)
1766 "return \\[i for i in range(3)\\]"))
1767 (python-nav-backward-up-list)
1768 (should (looking-at "if True:"))
1769 (python-nav-backward-up-list)
1770 (should (looking-at "def f():"))))
1773 ;;; Shell integration
1775 (defvar python-tests-shell-interpreter
"python")
1777 (ert-deftest python-shell-get-process-name-1
()
1778 "Check process name calculation on different scenarios."
1779 (python-tests-with-temp-buffer
1781 (should (string= (python-shell-get-process-name nil
)
1782 python-shell-buffer-name
))
1783 ;; When the `current-buffer' doesn't have `buffer-file-name', even
1784 ;; if dedicated flag is non-nil should not include its name.
1785 (should (string= (python-shell-get-process-name t
)
1786 python-shell-buffer-name
)))
1787 (python-tests-with-temp-file
1789 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1790 ;; should be respected.
1791 (should (string= (python-shell-get-process-name nil
)
1792 python-shell-buffer-name
))
1794 (python-shell-get-process-name t
)
1795 (format "%s[%s]" python-shell-buffer-name buffer-file-name
)))))
1797 (ert-deftest python-shell-internal-get-process-name-1
()
1798 "Check the internal process name is config-unique."
1799 (let* ((python-shell-interpreter python-tests-shell-interpreter
)
1800 (python-shell-interpreter-args "")
1801 (python-shell-prompt-regexp ">>> ")
1802 (python-shell-prompt-block-regexp "[.][.][.] ")
1803 (python-shell-setup-codes "")
1804 (python-shell-process-environment "")
1805 (python-shell-extra-pythonpaths "")
1806 (python-shell-exec-path "")
1807 (python-shell-virtualenv-path "")
1808 (expected (python-tests-with-temp-buffer
1809 "" (python-shell-internal-get-process-name))))
1810 ;; Same configurations should match.
1813 (python-tests-with-temp-buffer
1814 "" (python-shell-internal-get-process-name))))
1815 (let ((python-shell-interpreter-args "-B"))
1816 ;; A minimal change should generate different names.
1820 (python-tests-with-temp-buffer
1821 "" (python-shell-internal-get-process-name))))))))
1823 (ert-deftest python-shell-parse-command-1
()
1824 "Check the command to execute is calculated correctly.
1825 Using `python-shell-interpreter' and
1826 `python-shell-interpreter-args'."
1827 (skip-unless (executable-find python-tests-shell-interpreter
))
1828 (let ((python-shell-interpreter (executable-find
1829 python-tests-shell-interpreter
))
1830 (python-shell-interpreter-args "-B"))
1833 python-shell-interpreter
1834 python-shell-interpreter-args
)
1835 (python-shell-parse-command)))))
1837 (ert-deftest python-shell-calculate-process-environment-1
()
1838 "Test `python-shell-process-environment' modification."
1839 (let* ((original-process-environment process-environment
)
1840 (python-shell-process-environment
1841 '("TESTVAR1=value1" "TESTVAR2=value2"))
1842 (process-environment
1843 (python-shell-calculate-process-environment)))
1844 (should (equal (getenv "TESTVAR1") "value1"))
1845 (should (equal (getenv "TESTVAR2") "value2"))))
1847 (ert-deftest python-shell-calculate-process-environment-2
()
1848 "Test `python-shell-extra-pythonpaths' modification."
1849 (let* ((original-process-environment process-environment
)
1850 (original-pythonpath (getenv "PYTHONPATH"))
1851 (paths '("path1" "path2"))
1852 (python-shell-extra-pythonpaths paths
)
1853 (process-environment
1854 (python-shell-calculate-process-environment)))
1855 (should (equal (getenv "PYTHONPATH")
1857 (mapconcat 'identity paths path-separator
)
1858 path-separator original-pythonpath
)))))
1860 (ert-deftest python-shell-calculate-process-environment-3
()
1861 "Test `python-shell-virtualenv-path' modification."
1862 (let* ((original-process-environment process-environment
)
1863 (original-path (or (getenv "PATH") ""))
1864 (python-shell-virtualenv-path
1865 (directory-file-name user-emacs-directory
))
1866 (process-environment
1867 (python-shell-calculate-process-environment)))
1868 (should (not (getenv "PYTHONHOME")))
1869 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-path
))
1870 (should (equal (getenv "PATH")
1871 (format "%s/bin%s%s"
1872 python-shell-virtualenv-path
1873 path-separator original-path
)))))
1875 (ert-deftest python-shell-calculate-exec-path-1
()
1876 "Test `python-shell-exec-path' modification."
1877 (let* ((original-exec-path exec-path
)
1878 (python-shell-exec-path '("path1" "path2"))
1879 (exec-path (python-shell-calculate-exec-path)))
1882 (append python-shell-exec-path
1883 original-exec-path
)))))
1885 (ert-deftest python-shell-calculate-exec-path-2
()
1886 "Test `python-shell-exec-path' modification."
1887 (let* ((original-exec-path exec-path
)
1888 (python-shell-virtualenv-path
1889 (directory-file-name (expand-file-name user-emacs-directory
)))
1890 (exec-path (python-shell-calculate-exec-path)))
1894 (format "%s/bin" python-shell-virtualenv-path
)
1895 original-exec-path
))))))
1897 (ert-deftest python-shell-make-comint-1
()
1898 "Check comint creation for global shell buffer."
1899 (skip-unless (executable-find python-tests-shell-interpreter
))
1900 ;; The interpreter can get killed too quickly to allow it to clean
1901 ;; up the tempfiles that the default python-shell-setup-codes create,
1902 ;; so it leaves tempfiles behind, which is a minor irritation.
1903 (let* ((python-shell-setup-codes nil
)
1904 (python-shell-interpreter
1905 (executable-find python-tests-shell-interpreter
))
1906 (proc-name (python-shell-get-process-name nil
))
1908 (python-tests-with-temp-buffer
1909 "" (python-shell-make-comint
1910 (python-shell-parse-command) proc-name
)))
1911 (process (get-buffer-process shell-buffer
)))
1914 (set-process-query-on-exit-flag process nil
)
1915 (should (process-live-p process
))
1916 (with-current-buffer shell-buffer
1917 (should (eq major-mode
'inferior-python-mode
))
1918 (should (string= (buffer-name) (format "*%s*" proc-name
)))))
1919 (kill-buffer shell-buffer
))))
1921 (ert-deftest python-shell-make-comint-2
()
1922 "Check comint creation for internal shell buffer."
1923 (skip-unless (executable-find python-tests-shell-interpreter
))
1924 (let* ((python-shell-setup-codes nil
)
1925 (python-shell-interpreter
1926 (executable-find python-tests-shell-interpreter
))
1927 (proc-name (python-shell-internal-get-process-name))
1929 (python-tests-with-temp-buffer
1930 "" (python-shell-make-comint
1931 (python-shell-parse-command) proc-name nil t
)))
1932 (process (get-buffer-process shell-buffer
)))
1935 (set-process-query-on-exit-flag process nil
)
1936 (should (process-live-p process
))
1937 (with-current-buffer shell-buffer
1938 (should (eq major-mode
'inferior-python-mode
))
1939 (should (string= (buffer-name) (format " *%s*" proc-name
)))))
1940 (kill-buffer shell-buffer
))))
1942 (ert-deftest python-shell-make-comint-3
()
1943 "Check comint creation with overridden python interpreter and args.
1944 The command passed to `python-shell-make-comint' as argument must
1945 locally override global values set in `python-shell-interpreter'
1946 and `python-shell-interpreter-args' in the new shell buffer."
1947 (skip-unless (executable-find python-tests-shell-interpreter
))
1948 (let* ((python-shell-setup-codes nil
)
1949 (python-shell-interpreter "interpreter")
1950 (python-shell-interpreter-args "--some-args")
1951 (proc-name (python-shell-get-process-name nil
))
1952 (interpreter-override
1953 (concat (executable-find python-tests-shell-interpreter
) " " "-i"))
1955 (python-tests-with-temp-buffer
1956 "" (python-shell-make-comint interpreter-override proc-name nil
)))
1957 (process (get-buffer-process shell-buffer
)))
1960 (set-process-query-on-exit-flag process nil
)
1961 (should (process-live-p process
))
1962 (with-current-buffer shell-buffer
1963 (should (eq major-mode
'inferior-python-mode
))
1964 (should (string= python-shell-interpreter
1965 (executable-find python-tests-shell-interpreter
)))
1966 (should (string= python-shell-interpreter-args
"-i"))))
1967 (kill-buffer shell-buffer
))))
1969 (ert-deftest python-shell-make-comint-4
()
1970 "Check shell calculated prompts regexps are set."
1971 (skip-unless (executable-find python-tests-shell-interpreter
))
1972 (let* ((process-environment process-environment
)
1973 (python-shell-setup-codes nil
)
1974 (python-shell-interpreter
1975 (executable-find python-tests-shell-interpreter
))
1976 (python-shell-interpreter-args "-i")
1977 (python-shell--prompt-calculated-input-regexp nil
)
1978 (python-shell--prompt-calculated-output-regexp nil
)
1979 (python-shell-prompt-detect-enabled t
)
1980 (python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
1981 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
1982 (python-shell-prompt-regexp "in")
1983 (python-shell-prompt-block-regexp "block")
1984 (python-shell-prompt-pdb-regexp "pdf")
1985 (python-shell-prompt-output-regexp "output")
1986 (startup-code (concat "import sys\n"
1987 "sys.ps1 = 'py> '\n"
1988 "sys.ps2 = '..> '\n"
1989 "sys.ps3 = 'out '\n"))
1990 (startup-file (python-shell--save-temp-file startup-code
))
1991 (proc-name (python-shell-get-process-name nil
))
1994 (setenv "PYTHONSTARTUP" startup-file
)
1995 (python-tests-with-temp-buffer
1996 "" (python-shell-make-comint
1997 (python-shell-parse-command) proc-name nil
))))
1998 (process (get-buffer-process shell-buffer
)))
2001 (set-process-query-on-exit-flag process nil
)
2002 (should (process-live-p process
))
2003 (with-current-buffer shell-buffer
2004 (should (eq major-mode
'inferior-python-mode
))
2006 python-shell--prompt-calculated-input-regexp
2007 (concat "^\\(extralargeinputprompt\\|\\.\\.> \\|"
2008 "block\\|py> \\|pdf\\|sml\\|in\\)")))
2010 python-shell--prompt-calculated-output-regexp
2011 "^\\(extralargeoutputprompt\\|output\\|out \\|sml\\)"))))
2012 (delete-file startup-file
)
2013 (kill-buffer shell-buffer
))))
2015 (ert-deftest python-shell-get-process-1
()
2016 "Check dedicated shell process preference over global."
2017 (skip-unless (executable-find python-tests-shell-interpreter
))
2018 (python-tests-with-temp-file
2020 (let* ((python-shell-setup-codes nil
)
2021 (python-shell-interpreter
2022 (executable-find python-tests-shell-interpreter
))
2023 (global-proc-name (python-shell-get-process-name nil
))
2024 (dedicated-proc-name (python-shell-get-process-name t
))
2025 (global-shell-buffer
2026 (python-shell-make-comint
2027 (python-shell-parse-command) global-proc-name
))
2028 (dedicated-shell-buffer
2029 (python-shell-make-comint
2030 (python-shell-parse-command) dedicated-proc-name
))
2031 (global-process (get-buffer-process global-shell-buffer
))
2032 (dedicated-process (get-buffer-process dedicated-shell-buffer
)))
2035 (set-process-query-on-exit-flag global-process nil
)
2036 (set-process-query-on-exit-flag dedicated-process nil
)
2037 ;; Prefer dedicated if global also exists.
2038 (should (equal (python-shell-get-process) dedicated-process
))
2039 (kill-buffer dedicated-shell-buffer
)
2040 ;; If there's only global, use it.
2041 (should (equal (python-shell-get-process) global-process
))
2042 (kill-buffer global-shell-buffer
)
2043 ;; No buffer available.
2044 (should (not (python-shell-get-process))))
2045 (ignore-errors (kill-buffer global-shell-buffer
))
2046 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
2048 (ert-deftest python-shell-get-or-create-process-1
()
2049 "Check shell dedicated process creation."
2050 (skip-unless (executable-find python-tests-shell-interpreter
))
2051 (python-tests-with-temp-file
2053 (let* ((python-shell-interpreter
2054 (executable-find python-tests-shell-interpreter
))
2056 (dedicated-process-name (python-shell-get-process-name t
))
2058 (python-shell-get-or-create-process python-shell-interpreter t
))
2059 (dedicated-shell-buffer (process-buffer dedicated-process
)))
2062 (set-process-query-on-exit-flag dedicated-process nil
)
2063 ;; should be dedicated.
2064 (should (equal (process-name dedicated-process
)
2065 dedicated-process-name
))
2066 (kill-buffer dedicated-shell-buffer
)
2067 ;; Check there are no processes for current buffer.
2068 (should (not (python-shell-get-process))))
2069 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
2071 (ert-deftest python-shell-get-or-create-process-2
()
2072 "Check shell global process creation."
2073 (skip-unless (executable-find python-tests-shell-interpreter
))
2074 (python-tests-with-temp-file
2076 (let* ((python-shell-interpreter
2077 (executable-find python-tests-shell-interpreter
))
2079 (process-name (python-shell-get-process-name nil
))
2081 (python-shell-get-or-create-process python-shell-interpreter
))
2082 (shell-buffer (process-buffer process
)))
2085 (set-process-query-on-exit-flag process nil
)
2086 ;; should be global.
2087 (should (equal (process-name process
) process-name
))
2088 (kill-buffer shell-buffer
)
2089 ;; Check there are no processes for current buffer.
2090 (should (not (python-shell-get-process))))
2091 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
2093 (ert-deftest python-shell-get-or-create-process-3
()
2094 "Check shell dedicated/global process preference."
2095 (skip-unless (executable-find python-tests-shell-interpreter
))
2096 (python-tests-with-temp-file
2098 (let* ((python-shell-interpreter
2099 (executable-find python-tests-shell-interpreter
))
2101 (dedicated-process-name (python-shell-get-process-name t
))
2103 (dedicated-process))
2106 ;; Create global process
2107 (run-python python-shell-interpreter nil
)
2108 (setq global-process
(get-buffer-process "*Python*"))
2109 (should global-process
)
2110 (set-process-query-on-exit-flag global-process nil
)
2111 ;; Create dedicated process
2112 (run-python python-shell-interpreter t
)
2113 (setq dedicated-process
(get-process dedicated-process-name
))
2114 (should dedicated-process
)
2115 (set-process-query-on-exit-flag dedicated-process nil
)
2116 ;; Prefer dedicated.
2117 (should (equal (python-shell-get-or-create-process)
2119 ;; Kill the dedicated so the global takes over.
2120 (kill-buffer (process-buffer dedicated-process
))
2122 (should (equal (python-shell-get-or-create-process) global-process
))
2124 (kill-buffer (process-buffer global-process
))
2125 ;; Check there are no processes for current buffer.
2126 (should (not (python-shell-get-process))))
2127 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
2129 (ert-deftest python-shell-internal-get-or-create-process-1
()
2130 "Check internal shell process creation fallback."
2131 (skip-unless (executable-find python-tests-shell-interpreter
))
2132 (python-tests-with-temp-file
2134 (should (not (process-live-p (python-shell-internal-get-process-name))))
2135 (let* ((python-shell-interpreter
2136 (executable-find python-tests-shell-interpreter
))
2137 (internal-process-name (python-shell-internal-get-process-name))
2138 (internal-process (python-shell-internal-get-or-create-process))
2139 (internal-shell-buffer (process-buffer internal-process
)))
2142 (set-process-query-on-exit-flag internal-process nil
)
2143 (should (equal (process-name internal-process
)
2144 internal-process-name
))
2145 (should (equal internal-process
2146 (python-shell-internal-get-or-create-process)))
2147 ;; Assert the internal process is not a user process
2148 (should (not (python-shell-get-process)))
2149 (kill-buffer internal-shell-buffer
))
2150 (ignore-errors (kill-buffer internal-shell-buffer
))))))
2152 (ert-deftest python-shell-prompt-detect-1
()
2153 "Check prompt autodetection."
2154 (skip-unless (executable-find python-tests-shell-interpreter
))
2155 (let ((process-environment process-environment
))
2156 ;; Ensure no startup file is enabled
2157 (setenv "PYTHONSTARTUP" "")
2158 (should python-shell-prompt-detect-enabled
)
2159 (should (equal (python-shell-prompt-detect) '(">>> " "... " "")))))
2161 (ert-deftest python-shell-prompt-detect-2
()
2162 "Check prompt autodetection with startup file. Bug#17370."
2163 (skip-unless (executable-find python-tests-shell-interpreter
))
2164 (let* ((process-environment process-environment
)
2165 (startup-code (concat "import sys\n"
2166 "sys.ps1 = 'py> '\n"
2167 "sys.ps2 = '..> '\n"
2168 "sys.ps3 = 'out '\n"))
2169 (startup-file (python-shell--save-temp-file startup-code
)))
2172 ;; Ensure startup file is enabled
2173 (setenv "PYTHONSTARTUP" startup-file
)
2174 (should python-shell-prompt-detect-enabled
)
2175 (should (equal (python-shell-prompt-detect) '("py> " "..> " "out "))))
2176 (ignore-errors (delete-file startup-file
)))))
2178 (ert-deftest python-shell-prompt-detect-3
()
2179 "Check prompts are not autodetected when feature is disabled."
2180 (skip-unless (executable-find python-tests-shell-interpreter
))
2181 (let ((process-environment process-environment
)
2182 (python-shell-prompt-detect-enabled nil
))
2183 ;; Ensure no startup file is enabled
2184 (should (not python-shell-prompt-detect-enabled
))
2185 (should (not (python-shell-prompt-detect)))))
2187 (ert-deftest python-shell-prompt-detect-4
()
2188 "Check warning is shown when detection fails."
2189 (skip-unless (executable-find python-tests-shell-interpreter
))
2190 (let* ((process-environment process-environment
)
2191 ;; Trigger failure by removing prompts in the startup file
2192 (startup-code (concat "import sys\n"
2196 (startup-file (python-shell--save-temp-file startup-code
)))
2199 (kill-buffer (get-buffer-create "*Warnings*"))
2200 (should (not (get-buffer "*Warnings*")))
2201 (setenv "PYTHONSTARTUP" startup-file
)
2202 (should python-shell-prompt-detect-failure-warning
)
2203 (should python-shell-prompt-detect-enabled
)
2204 (should (not (python-shell-prompt-detect)))
2205 (should (get-buffer "*Warnings*")))
2206 (ignore-errors (delete-file startup-file
)))))
2208 (ert-deftest python-shell-prompt-detect-5
()
2209 "Check disabled warnings are not shown when detection fails."
2210 (skip-unless (executable-find python-tests-shell-interpreter
))
2211 (let* ((process-environment process-environment
)
2212 (startup-code (concat "import sys\n"
2216 (startup-file (python-shell--save-temp-file startup-code
))
2217 (python-shell-prompt-detect-failure-warning nil
))
2220 (kill-buffer (get-buffer-create "*Warnings*"))
2221 (should (not (get-buffer "*Warnings*")))
2222 (setenv "PYTHONSTARTUP" startup-file
)
2223 (should (not python-shell-prompt-detect-failure-warning
))
2224 (should python-shell-prompt-detect-enabled
)
2225 (should (not (python-shell-prompt-detect)))
2226 (should (not (get-buffer "*Warnings*"))))
2227 (ignore-errors (delete-file startup-file
)))))
2229 (ert-deftest python-shell-prompt-detect-6
()
2230 "Warnings are not shown when detection is disabled."
2231 (skip-unless (executable-find python-tests-shell-interpreter
))
2232 (let* ((process-environment process-environment
)
2233 (startup-code (concat "import sys\n"
2237 (startup-file (python-shell--save-temp-file startup-code
))
2238 (python-shell-prompt-detect-failure-warning t
)
2239 (python-shell-prompt-detect-enabled nil
))
2242 (kill-buffer (get-buffer-create "*Warnings*"))
2243 (should (not (get-buffer "*Warnings*")))
2244 (setenv "PYTHONSTARTUP" startup-file
)
2245 (should python-shell-prompt-detect-failure-warning
)
2246 (should (not python-shell-prompt-detect-enabled
))
2247 (should (not (python-shell-prompt-detect)))
2248 (should (not (get-buffer "*Warnings*"))))
2249 (ignore-errors (delete-file startup-file
)))))
2251 (ert-deftest python-shell-prompt-validate-regexps-1
()
2252 "Check `python-shell-prompt-input-regexps' are validated."
2253 (let* ((python-shell-prompt-input-regexps '("\\("))
2254 (error-data (should-error (python-shell-prompt-validate-regexps)
2255 :type
'user-error
)))
2257 (string= (cadr error-data
)
2258 "Invalid regexp \\( in `python-shell-prompt-input-regexps'"))))
2260 (ert-deftest python-shell-prompt-validate-regexps-2
()
2261 "Check `python-shell-prompt-output-regexps' are validated."
2262 (let* ((python-shell-prompt-output-regexps '("\\("))
2263 (error-data (should-error (python-shell-prompt-validate-regexps)
2264 :type
'user-error
)))
2266 (string= (cadr error-data
)
2267 "Invalid regexp \\( in `python-shell-prompt-output-regexps'"))))
2269 (ert-deftest python-shell-prompt-validate-regexps-3
()
2270 "Check `python-shell-prompt-regexp' is validated."
2271 (let* ((python-shell-prompt-regexp "\\(")
2272 (error-data (should-error (python-shell-prompt-validate-regexps)
2273 :type
'user-error
)))
2275 (string= (cadr error-data
)
2276 "Invalid regexp \\( in `python-shell-prompt-regexp'"))))
2278 (ert-deftest python-shell-prompt-validate-regexps-4
()
2279 "Check `python-shell-prompt-block-regexp' is validated."
2280 (let* ((python-shell-prompt-block-regexp "\\(")
2281 (error-data (should-error (python-shell-prompt-validate-regexps)
2282 :type
'user-error
)))
2284 (string= (cadr error-data
)
2285 "Invalid regexp \\( in `python-shell-prompt-block-regexp'"))))
2287 (ert-deftest python-shell-prompt-validate-regexps-5
()
2288 "Check `python-shell-prompt-pdb-regexp' is validated."
2289 (let* ((python-shell-prompt-pdb-regexp "\\(")
2290 (error-data (should-error (python-shell-prompt-validate-regexps)
2291 :type
'user-error
)))
2293 (string= (cadr error-data
)
2294 "Invalid regexp \\( in `python-shell-prompt-pdb-regexp'"))))
2296 (ert-deftest python-shell-prompt-validate-regexps-6
()
2297 "Check `python-shell-prompt-output-regexp' is validated."
2298 (let* ((python-shell-prompt-output-regexp "\\(")
2299 (error-data (should-error (python-shell-prompt-validate-regexps)
2300 :type
'user-error
)))
2302 (string= (cadr error-data
)
2303 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2305 (ert-deftest python-shell-prompt-validate-regexps-7
()
2306 "Check default regexps are valid."
2307 ;; should not signal error
2308 (python-shell-prompt-validate-regexps))
2310 (ert-deftest python-shell-prompt-set-calculated-regexps-1
()
2311 "Check regexps are validated."
2312 (let* ((python-shell-prompt-output-regexp '("\\("))
2313 (python-shell--prompt-calculated-input-regexp nil
)
2314 (python-shell--prompt-calculated-output-regexp nil
)
2315 (python-shell-prompt-detect-enabled nil
)
2316 (error-data (should-error (python-shell-prompt-set-calculated-regexps)
2317 :type
'user-error
)))
2319 (string= (cadr error-data
)
2320 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2322 (ert-deftest python-shell-prompt-set-calculated-regexps-2
()
2323 "Check `python-shell-prompt-input-regexps' are set."
2324 (let* ((python-shell-prompt-input-regexps '("my" "prompt"))
2325 (python-shell-prompt-output-regexps '(""))
2326 (python-shell-prompt-regexp "")
2327 (python-shell-prompt-block-regexp "")
2328 (python-shell-prompt-pdb-regexp "")
2329 (python-shell-prompt-output-regexp "")
2330 (python-shell--prompt-calculated-input-regexp nil
)
2331 (python-shell--prompt-calculated-output-regexp nil
)
2332 (python-shell-prompt-detect-enabled nil
))
2333 (python-shell-prompt-set-calculated-regexps)
2334 (should (string= python-shell--prompt-calculated-input-regexp
2335 "^\\(prompt\\|my\\|\\)"))))
2337 (ert-deftest python-shell-prompt-set-calculated-regexps-3
()
2338 "Check `python-shell-prompt-output-regexps' are set."
2339 (let* ((python-shell-prompt-input-regexps '(""))
2340 (python-shell-prompt-output-regexps '("my" "prompt"))
2341 (python-shell-prompt-regexp "")
2342 (python-shell-prompt-block-regexp "")
2343 (python-shell-prompt-pdb-regexp "")
2344 (python-shell-prompt-output-regexp "")
2345 (python-shell--prompt-calculated-input-regexp nil
)
2346 (python-shell--prompt-calculated-output-regexp nil
)
2347 (python-shell-prompt-detect-enabled nil
))
2348 (python-shell-prompt-set-calculated-regexps)
2349 (should (string= python-shell--prompt-calculated-output-regexp
2350 "^\\(prompt\\|my\\|\\)"))))
2352 (ert-deftest python-shell-prompt-set-calculated-regexps-4
()
2353 "Check user defined prompts are set."
2354 (let* ((python-shell-prompt-input-regexps '(""))
2355 (python-shell-prompt-output-regexps '(""))
2356 (python-shell-prompt-regexp "prompt")
2357 (python-shell-prompt-block-regexp "block")
2358 (python-shell-prompt-pdb-regexp "pdb")
2359 (python-shell-prompt-output-regexp "output")
2360 (python-shell--prompt-calculated-input-regexp nil
)
2361 (python-shell--prompt-calculated-output-regexp nil
)
2362 (python-shell-prompt-detect-enabled nil
))
2363 (python-shell-prompt-set-calculated-regexps)
2364 (should (string= python-shell--prompt-calculated-input-regexp
2365 "^\\(prompt\\|block\\|pdb\\|\\)"))
2366 (should (string= python-shell--prompt-calculated-output-regexp
2367 "^\\(output\\|\\)"))))
2369 (ert-deftest python-shell-prompt-set-calculated-regexps-5
()
2370 "Check order of regexps (larger first)."
2371 (let* ((python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2372 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2373 (python-shell-prompt-regexp "in")
2374 (python-shell-prompt-block-regexp "block")
2375 (python-shell-prompt-pdb-regexp "pdf")
2376 (python-shell-prompt-output-regexp "output")
2377 (python-shell--prompt-calculated-input-regexp nil
)
2378 (python-shell--prompt-calculated-output-regexp nil
)
2379 (python-shell-prompt-detect-enabled nil
))
2380 (python-shell-prompt-set-calculated-regexps)
2381 (should (string= python-shell--prompt-calculated-input-regexp
2382 "^\\(extralargeinputprompt\\|block\\|pdf\\|sml\\|in\\)"))
2383 (should (string= python-shell--prompt-calculated-output-regexp
2384 "^\\(extralargeoutputprompt\\|output\\|sml\\)"))))
2386 (ert-deftest python-shell-prompt-set-calculated-regexps-6
()
2387 "Check detected prompts are included `regexp-quote'd."
2388 (skip-unless (executable-find python-tests-shell-interpreter
))
2389 (let* ((python-shell-prompt-input-regexps '(""))
2390 (python-shell-prompt-output-regexps '(""))
2391 (python-shell-prompt-regexp "")
2392 (python-shell-prompt-block-regexp "")
2393 (python-shell-prompt-pdb-regexp "")
2394 (python-shell-prompt-output-regexp "")
2395 (python-shell--prompt-calculated-input-regexp nil
)
2396 (python-shell--prompt-calculated-output-regexp nil
)
2397 (python-shell-prompt-detect-enabled t
)
2398 (process-environment process-environment
)
2399 (startup-code (concat "import sys\n"
2400 "sys.ps1 = 'p.> '\n"
2401 "sys.ps2 = '..> '\n"
2402 "sys.ps3 = 'o.t '\n"))
2403 (startup-file (python-shell--save-temp-file startup-code
)))
2406 (setenv "PYTHONSTARTUP" startup-file
)
2407 (python-shell-prompt-set-calculated-regexps)
2408 (should (string= python-shell--prompt-calculated-input-regexp
2409 "^\\(\\.\\.> \\|p\\.> \\|\\)"))
2410 (should (string= python-shell--prompt-calculated-output-regexp
2411 "^\\(o\\.t \\|\\)")))
2412 (ignore-errors (delete-file startup-file
)))))
2415 ;;; Shell completion
2418 ;;; PDB Track integration
2421 ;;; Symbol completion
2441 (ert-deftest python-imenu-create-index-1
()
2442 (python-tests-with-temp-buffer
2444 class Foo(models.Model):
2448 class Bar(models.Model):
2452 def decorator(arg1, arg2, arg3):
2453 '''print decorated function call data to stdout.
2457 @decorator('arg1', 'arg2')
2458 def func(a, b, c=True):
2464 def wrapped_f(*args):
2466 print ('Decorator arguments:', arg1, arg2, arg3)
2468 print ('called f(*args)')
2486 (goto-char (point-max))
2489 (cons "Foo (class)" (copy-marker 2))
2490 (cons "Bar (class)" (copy-marker 38))
2493 (cons "*function definition*" (copy-marker 74))
2496 (cons "*function definition*" (copy-marker 254))
2497 (cons "wrapped_f (def)" (copy-marker 294))))
2500 (cons "*class definition*" (copy-marker 519))
2501 (cons "a (def)" (copy-marker 539))
2502 (cons "b (def)" (copy-marker 570))
2505 (cons "*class definition*" (copy-marker 601))
2506 (cons "c (def)" (copy-marker 626)))))
2507 (python-imenu-create-index)))))
2509 (ert-deftest python-imenu-create-index-2
()
2510 (python-tests-with-temp-buffer
2520 (goto-char (point-max))
2525 (cons "*class definition*" (copy-marker 2))
2528 (cons "*function definition*" (copy-marker 21))
2529 (cons "foo1 (def)" (copy-marker 40)))
2530 (cons "foobar (def)" (copy-marker 78))))
2531 (python-imenu-create-index)))))
2533 (ert-deftest python-imenu-create-index-3
()
2534 (python-tests-with-temp-buffer
2543 (goto-char (point-max))
2548 (cons "*class definition*" (copy-marker 2))
2551 (cons "*function definition*" (copy-marker 21))
2552 (cons "foo1 (def)" (copy-marker 40))
2553 (cons "foo2 (def)" (copy-marker 77)))))
2554 (python-imenu-create-index)))))
2556 (ert-deftest python-imenu-create-index-4
()
2557 (python-tests-with-temp-buffer
2570 (goto-char (point-max))
2575 (cons "*class definition*" (copy-marker 2))
2578 (cons "*class definition*" (copy-marker 21))
2579 (cons "__init__ (def)" (copy-marker 44))
2580 (cons "__str__ (def)" (copy-marker 90)))
2581 (cons "__init__ (def)" (copy-marker 135))))
2582 (python-imenu-create-index)))))
2584 (ert-deftest python-imenu-create-flat-index-1
()
2585 (python-tests-with-temp-buffer
2587 class Foo(models.Model):
2591 class Bar(models.Model):
2595 def decorator(arg1, arg2, arg3):
2596 '''print decorated function call data to stdout.
2600 @decorator('arg1', 'arg2')
2601 def func(a, b, c=True):
2607 def wrapped_f(*args):
2609 print ('Decorator arguments:', arg1, arg2, arg3)
2611 print ('called f(*args)')
2629 (goto-char (point-max))
2631 (list (cons "Foo" (copy-marker 2))
2632 (cons "Bar" (copy-marker 38))
2633 (cons "decorator" (copy-marker 74))
2634 (cons "decorator.wrap" (copy-marker 254))
2635 (cons "decorator.wrap.wrapped_f" (copy-marker 294))
2636 (cons "Baz" (copy-marker 519))
2637 (cons "Baz.a" (copy-marker 539))
2638 (cons "Baz.b" (copy-marker 570))
2639 (cons "Baz.Frob" (copy-marker 601))
2640 (cons "Baz.Frob.c" (copy-marker 626)))
2641 (python-imenu-create-flat-index)))))
2643 (ert-deftest python-imenu-create-flat-index-2
()
2644 (python-tests-with-temp-buffer
2657 (goto-char (point-max))
2660 (cons "Foo" (copy-marker 2))
2661 (cons "Foo.Bar" (copy-marker 21))
2662 (cons "Foo.Bar.__init__" (copy-marker 44))
2663 (cons "Foo.Bar.__str__" (copy-marker 90))
2664 (cons "Foo.__init__" (copy-marker 135)))
2665 (python-imenu-create-flat-index)))))
2670 (ert-deftest python-info-current-defun-1
()
2671 (python-tests-with-temp-buffer
2676 (should (string= "foo" (python-info-current-defun)))
2677 (should (string= "def foo" (python-info-current-defun t)))
2679 (should (not (python-info-current-defun)))
2680 (indent-for-tab-command)
2681 (should (string= "foo" (python-info-current-defun)))
2682 (should (string= "def foo" (python-info-current-defun t)))))
2684 (ert-deftest python-info-current-defun-2
()
2685 (python-tests-with-temp-buffer
2691 return [i for i in range(3)]
2705 (should (string= "C" (python-info-current-defun)))
2706 (should (string= "class C" (python-info-current-defun t)))
2707 (python-tests-look-at "return [i for ")
2708 (should (string= "C.m" (python-info-current-defun)))
2709 (should (string= "def C.m" (python-info-current-defun t)))
2710 (python-tests-look-at "def b():")
2711 (should (string= "C.m.b" (python-info-current-defun)))
2712 (should (string= "def C.m.b" (python-info-current-defun t)))
2714 (indent-for-tab-command)
2715 (python-indent-dedent-line-backspace 1)
2716 (should (string= "C.m" (python-info-current-defun)))
2717 (should (string= "def C.m" (python-info-current-defun t)))
2718 (python-tests-look-at "def c(self):")
2720 (indent-for-tab-command)
2721 (should (string= "C.m.a" (python-info-current-defun)))
2722 (should (string= "def C.m.a" (python-info-current-defun t)))
2723 (python-indent-dedent-line-backspace 1)
2724 (should (string= "C.m" (python-info-current-defun)))
2725 (should (string= "def C.m" (python-info-current-defun t)))
2726 (python-indent-dedent-line-backspace 1)
2727 (should (string= "C" (python-info-current-defun)))
2728 (should (string= "class C" (python-info-current-defun t)))
2729 (python-tests-look-at "def c(self):")
2730 (should (string= "C.c" (python-info-current-defun)))
2731 (should (string= "def C.c" (python-info-current-defun t)))
2732 (python-tests-look-at "do_c()")
2733 (should (string= "C.c" (python-info-current-defun)))
2734 (should (string= "def C.c" (python-info-current-defun t)))))
2736 (ert-deftest python-info-current-defun-3
()
2737 (python-tests-with-temp-buffer
2739 def decoratorFunctionWithArguments(arg1, arg2, arg3):
2740 '''print decorated function call data to stdout.
2744 @decoratorFunctionWithArguments('arg1', 'arg2')
2745 def func(a, b, c=True):
2750 print 'Inside wwrap()'
2751 def wrapped_f(*args):
2752 print 'Inside wrapped_f()'
2753 print 'Decorator arguments:', arg1, arg2, arg3
2755 print 'After f(*args)'
2759 (python-tests-look-at "def wwrap(f):")
2761 (should (not (python-info-current-defun)))
2762 (indent-for-tab-command 1)
2763 (should (string= (python-info-current-defun)
2764 "decoratorFunctionWithArguments"))
2765 (should (string= (python-info-current-defun t)
2766 "def decoratorFunctionWithArguments"))
2767 (python-tests-look-at "def wrapped_f(*args):")
2768 (should (string= (python-info-current-defun)
2769 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
2770 (should (string= (python-info-current-defun t)
2771 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
2772 (python-tests-look-at "return wrapped_f")
2773 (should (string= (python-info-current-defun)
2774 "decoratorFunctionWithArguments.wwrap"))
2775 (should (string= (python-info-current-defun t)
2776 "def decoratorFunctionWithArguments.wwrap"))
2778 (python-tests-look-at "return wwrap")
2779 (should (string= (python-info-current-defun)
2780 "decoratorFunctionWithArguments"))
2781 (should (string= (python-info-current-defun t)
2782 "def decoratorFunctionWithArguments"))))
2784 (ert-deftest python-info-current-symbol-1
()
2785 (python-tests-with-temp-buffer
2795 (python-tests-look-at "self.c()")
2796 (should (string= "self.c" (python-info-current-symbol)))
2797 (should (string= "C.c" (python-info-current-symbol t
)))))
2799 (ert-deftest python-info-current-symbol-2
()
2800 (python-tests-with-temp-buffer
2812 (python-tests-look-at "self.c()")
2813 (should (string= "self.c" (python-info-current-symbol)))
2814 (should (string= "C.M.c" (python-info-current-symbol t
)))))
2816 (ert-deftest python-info-current-symbol-3
()
2817 "Keywords should not be considered symbols."
2818 :expected-result
:failed
2819 (python-tests-with-temp-buffer
2824 ;; FIXME: keywords are not symbols.
2825 (python-tests-look-at "class C")
2826 (should (not (python-info-current-symbol)))
2827 (should (not (python-info-current-symbol t
)))
2828 (python-tests-look-at "C(object)")
2829 (should (string= "C" (python-info-current-symbol)))
2830 (should (string= "class C" (python-info-current-symbol t
)))))
2832 (ert-deftest python-info-statement-starts-block-p-1
()
2833 (python-tests-with-temp-buffer
2835 def long_function_name(
2836 var_one, var_two, var_three,
2840 (python-tests-look-at "def long_function_name")
2841 (should (python-info-statement-starts-block-p))
2842 (python-tests-look-at "print (var_one)")
2843 (python-util-forward-comment -
1)
2844 (should (python-info-statement-starts-block-p))))
2846 (ert-deftest python-info-statement-starts-block-p-2
()
2847 (python-tests-with-temp-buffer
2849 if width == 0 and height == 0 and \\\\
2850 color == 'red' and emphasis == 'strong' or \\\\
2852 raise ValueError('sorry, you lose')
2854 (python-tests-look-at "if width == 0 and")
2855 (should (python-info-statement-starts-block-p))
2856 (python-tests-look-at "raise ValueError(")
2857 (python-util-forward-comment -
1)
2858 (should (python-info-statement-starts-block-p))))
2860 (ert-deftest python-info-statement-ends-block-p-1
()
2861 (python-tests-with-temp-buffer
2863 def long_function_name(
2864 var_one, var_two, var_three,
2868 (python-tests-look-at "print (var_one)")
2869 (should (python-info-statement-ends-block-p))))
2871 (ert-deftest python-info-statement-ends-block-p-2
()
2872 (python-tests-with-temp-buffer
2874 if width == 0 and height == 0 and \\\\
2875 color == 'red' and emphasis == 'strong' or \\\\
2882 (python-tests-look-at "raise ValueError(")
2883 (should (python-info-statement-ends-block-p))))
2885 (ert-deftest python-info-beginning-of-statement-p-1
()
2886 (python-tests-with-temp-buffer
2888 def long_function_name(
2889 var_one, var_two, var_three,
2893 (python-tests-look-at "def long_function_name")
2894 (should (python-info-beginning-of-statement-p))
2896 (should (not (python-info-beginning-of-statement-p)))
2897 (python-tests-look-at "print (var_one)")
2898 (should (python-info-beginning-of-statement-p))
2899 (goto-char (line-beginning-position))
2900 (should (not (python-info-beginning-of-statement-p)))))
2902 (ert-deftest python-info-beginning-of-statement-p-2
()
2903 (python-tests-with-temp-buffer
2905 if width == 0 and height == 0 and \\\\
2906 color == 'red' and emphasis == 'strong' or \\\\
2913 (python-tests-look-at "if width == 0 and")
2914 (should (python-info-beginning-of-statement-p))
2916 (should (not (python-info-beginning-of-statement-p)))
2917 (python-tests-look-at "raise ValueError(")
2918 (should (python-info-beginning-of-statement-p))
2919 (goto-char (line-beginning-position))
2920 (should (not (python-info-beginning-of-statement-p)))))
2922 (ert-deftest python-info-end-of-statement-p-1
()
2923 (python-tests-with-temp-buffer
2925 def long_function_name(
2926 var_one, var_two, var_three,
2930 (python-tests-look-at "def long_function_name")
2931 (should (not (python-info-end-of-statement-p)))
2933 (should (not (python-info-end-of-statement-p)))
2934 (python-tests-look-at "print (var_one)")
2935 (python-util-forward-comment -
1)
2936 (should (python-info-end-of-statement-p))
2937 (python-tests-look-at "print (var_one)")
2938 (should (not (python-info-end-of-statement-p)))
2940 (should (python-info-end-of-statement-p))))
2942 (ert-deftest python-info-end-of-statement-p-2
()
2943 (python-tests-with-temp-buffer
2945 if width == 0 and height == 0 and \\\\
2946 color == 'red' and emphasis == 'strong' or \\\\
2953 (python-tests-look-at "if width == 0 and")
2954 (should (not (python-info-end-of-statement-p)))
2956 (should (not (python-info-end-of-statement-p)))
2957 (python-tests-look-at "raise ValueError(")
2958 (python-util-forward-comment -
1)
2959 (should (python-info-end-of-statement-p))
2960 (python-tests-look-at "raise ValueError(")
2961 (should (not (python-info-end-of-statement-p)))
2963 (should (not (python-info-end-of-statement-p)))
2964 (goto-char (point-max))
2965 (python-util-forward-comment -
1)
2966 (should (python-info-end-of-statement-p))))
2968 (ert-deftest python-info-beginning-of-block-p-1
()
2969 (python-tests-with-temp-buffer
2971 def long_function_name(
2972 var_one, var_two, var_three,
2976 (python-tests-look-at "def long_function_name")
2977 (should (python-info-beginning-of-block-p))
2978 (python-tests-look-at "var_one, var_two, var_three,")
2979 (should (not (python-info-beginning-of-block-p)))
2980 (python-tests-look-at "print (var_one)")
2981 (should (not (python-info-beginning-of-block-p)))))
2983 (ert-deftest python-info-beginning-of-block-p-2
()
2984 (python-tests-with-temp-buffer
2986 if width == 0 and height == 0 and \\\\
2987 color == 'red' and emphasis == 'strong' or \\\\
2994 (python-tests-look-at "if width == 0 and")
2995 (should (python-info-beginning-of-block-p))
2996 (python-tests-look-at "color == 'red' and emphasis")
2997 (should (not (python-info-beginning-of-block-p)))
2998 (python-tests-look-at "raise ValueError(")
2999 (should (not (python-info-beginning-of-block-p)))))
3001 (ert-deftest python-info-end-of-block-p-1
()
3002 (python-tests-with-temp-buffer
3004 def long_function_name(
3005 var_one, var_two, var_three,
3009 (python-tests-look-at "def long_function_name")
3010 (should (not (python-info-end-of-block-p)))
3011 (python-tests-look-at "var_one, var_two, var_three,")
3012 (should (not (python-info-end-of-block-p)))
3013 (python-tests-look-at "var_four):")
3015 (should (not (python-info-end-of-block-p)))
3016 (python-tests-look-at "print (var_one)")
3017 (should (not (python-info-end-of-block-p)))
3019 (should (python-info-end-of-block-p))))
3021 (ert-deftest python-info-end-of-block-p-2
()
3022 (python-tests-with-temp-buffer
3024 if width == 0 and height == 0 and \\\\
3025 color == 'red' and emphasis == 'strong' or \\\\
3032 (python-tests-look-at "if width == 0 and")
3033 (should (not (python-info-end-of-block-p)))
3034 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3035 (should (not (python-info-end-of-block-p)))
3036 (python-tests-look-at "highlight > 100:")
3038 (should (not (python-info-end-of-block-p)))
3039 (python-tests-look-at "raise ValueError(")
3040 (should (not (python-info-end-of-block-p)))
3042 (should (not (python-info-end-of-block-p)))
3043 (goto-char (point-max))
3044 (python-util-forward-comment -
1)
3045 (should (python-info-end-of-block-p))))
3047 (ert-deftest python-info-dedenter-opening-block-position-1
()
3048 (python-tests-with-temp-buffer
3050 if request.user.is_authenticated():
3052 profile = request.user.get_profile()
3053 except Profile.DoesNotExist:
3054 profile = Profile.objects.create(user=request.user)
3057 profile.recalculate_stats()
3059 profile.clear_stats()
3064 (python-tests-look-at "try:")
3065 (should (not (python-info-dedenter-opening-block-position)))
3066 (python-tests-look-at "except Profile.DoesNotExist:")
3067 (should (= (python-tests-look-at "try:" -
1 t
)
3068 (python-info-dedenter-opening-block-position)))
3069 (python-tests-look-at "else:")
3070 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -
1 t
)
3071 (python-info-dedenter-opening-block-position)))
3072 (python-tests-look-at "if profile.stats:")
3073 (should (not (python-info-dedenter-opening-block-position)))
3074 (python-tests-look-at "else:")
3075 (should (= (python-tests-look-at "if profile.stats:" -
1 t
)
3076 (python-info-dedenter-opening-block-position)))
3077 (python-tests-look-at "finally:")
3078 (should (= (python-tests-look-at "else:" -
2 t
)
3079 (python-info-dedenter-opening-block-position)))))
3081 (ert-deftest python-info-dedenter-opening-block-position-2
()
3082 (python-tests-with-temp-buffer
3084 if request.user.is_authenticated():
3085 profile = Profile.objects.get_or_create(user=request.user)
3087 profile.recalculate_stats()
3094 (python-tests-look-at "'else': 'do it'")
3095 (should (not (python-info-dedenter-opening-block-position)))
3096 (python-tests-look-at "'else'")
3097 (should (not (python-info-dedenter-opening-block-position)))))
3099 (ert-deftest python-info-dedenter-opening-block-position-3
()
3100 (python-tests-with-temp-buffer
3106 msg = 'Error saving to disk'
3108 logger.exception(msg)
3111 logger.exception('Unhandled exception')
3116 (python-tests-look-at "try:")
3117 (should (not (python-info-dedenter-opening-block-position)))
3119 (python-tests-look-at "except IOError:")
3120 (should (= (python-tests-look-at "try:" -
1 t
)
3121 (python-info-dedenter-opening-block-position)))
3123 (python-tests-look-at "except Exception:")
3124 (should (= (python-tests-look-at "except IOError:" -
1 t
)
3125 (python-info-dedenter-opening-block-position)))
3127 (python-tests-look-at "if hide_details:")
3128 (should (not (python-info-dedenter-opening-block-position)))
3130 ;; check indentation modifies the detected opening block
3131 (python-tests-look-at "else")
3132 (should (= (python-tests-look-at "if hide_details:" -
1 t
)
3133 (python-info-dedenter-opening-block-position)))
3136 (should (= (python-tests-look-at "if hide_details:" -
1 t
)
3137 (python-info-dedenter-opening-block-position)))
3140 (should (= (python-tests-look-at "except Exception:" -
1 t
)
3141 (python-info-dedenter-opening-block-position)))
3144 (should (= (python-tests-look-at "if save:" -
1 t
)
3145 (python-info-dedenter-opening-block-position)))))
3147 (ert-deftest python-info-dedenter-opening-block-positions-1
()
3148 (python-tests-with-temp-buffer
3154 msg = 'Error saving to disk'
3156 logger.exception(msg)
3159 logger.exception('Unhandled exception')
3164 (python-tests-look-at "try:")
3165 (should (not (python-info-dedenter-opening-block-positions)))
3167 (python-tests-look-at "except IOError:")
3170 (python-tests-look-at "try:" -
1 t
))
3171 (python-info-dedenter-opening-block-positions)))
3173 (python-tests-look-at "except Exception:")
3176 (python-tests-look-at "except IOError:" -
1 t
))
3177 (python-info-dedenter-opening-block-positions)))
3179 (python-tests-look-at "if hide_details:")
3180 (should (not (python-info-dedenter-opening-block-positions)))
3182 ;; check indentation does not modify the detected opening blocks
3183 (python-tests-look-at "else")
3186 (python-tests-look-at "if hide_details:" -
1 t
)
3187 (python-tests-look-at "except Exception:" -
1 t
)
3188 (python-tests-look-at "if save:" -
1 t
))
3189 (python-info-dedenter-opening-block-positions)))
3194 (python-tests-look-at "if hide_details:" -
1 t
)
3195 (python-tests-look-at "except Exception:" -
1 t
)
3196 (python-tests-look-at "if save:" -
1 t
))
3197 (python-info-dedenter-opening-block-positions)))
3202 (python-tests-look-at "if hide_details:" -
1 t
)
3203 (python-tests-look-at "except Exception:" -
1 t
)
3204 (python-tests-look-at "if save:" -
1 t
))
3205 (python-info-dedenter-opening-block-positions)))
3210 (python-tests-look-at "if hide_details:" -
1 t
)
3211 (python-tests-look-at "except Exception:" -
1 t
)
3212 (python-tests-look-at "if save:" -
1 t
))
3213 (python-info-dedenter-opening-block-positions)))))
3215 (ert-deftest python-info-dedenter-opening-block-positions-2
()
3216 "Test detection of opening blocks for elif."
3217 (python-tests-with-temp-buffer
3226 (python-tests-look-at "elif var3:")
3229 (python-tests-look-at "if var2:" -
1 t
)
3230 (python-tests-look-at "if var:" -
1 t
))
3231 (python-info-dedenter-opening-block-positions)))
3233 (python-tests-look-at "elif\n")
3236 (python-tests-look-at "elif var3:" -
1 t
)
3237 (python-tests-look-at "if var:" -
1 t
))
3238 (python-info-dedenter-opening-block-positions)))))
3240 (ert-deftest python-info-dedenter-opening-block-positions-3
()
3241 "Test detection of opening blocks for else."
3242 (python-tests-with-temp-buffer
3264 (python-tests-look-at "else\n")
3267 (python-tests-look-at "elif var3:" -
1 t
)
3268 (python-tests-look-at "if var:" -
1 t
)
3269 (python-tests-look-at "except:" -
1 t
))
3270 (python-info-dedenter-opening-block-positions)))
3272 (python-tests-look-at "else\n")
3275 (python-tests-look-at "while var5:" -
1 t
)
3276 (python-tests-look-at "if var4:" -
1 t
))
3277 (python-info-dedenter-opening-block-positions)))
3279 (python-tests-look-at "else\n")
3282 (python-tests-look-at "if value > 0:" -
1 t
)
3283 (python-tests-look-at "for value in var6:" -
1 t
)
3284 (python-tests-look-at "if var4:" -
1 t
))
3285 (python-info-dedenter-opening-block-positions)))))
3287 (ert-deftest python-info-dedenter-opening-block-positions-4
()
3288 "Test detection of opening blocks for except."
3289 (python-tests-with-temp-buffer
3297 (python-tests-look-at "except ValueError:")
3299 (equal (list (python-tests-look-at "try:" -
1 t
))
3300 (python-info-dedenter-opening-block-positions)))
3302 (python-tests-look-at "except\n")
3304 (equal (list (python-tests-look-at "except ValueError:" -
1 t
))
3305 (python-info-dedenter-opening-block-positions)))))
3307 (ert-deftest python-info-dedenter-opening-block-positions-5
()
3308 "Test detection of opening blocks for finally."
3309 (python-tests-with-temp-buffer
3318 logger.exception('something went wrong')
3322 something_else_else()
3324 logger.exception('something else went wrong')
3329 (python-tests-look-at "finally\n")
3331 (equal (list (python-tests-look-at "try:" -
1 t
))
3332 (python-info-dedenter-opening-block-positions)))
3334 (python-tests-look-at "finally\n")
3336 (equal (list (python-tests-look-at "except:" -
1 t
))
3337 (python-info-dedenter-opening-block-positions)))
3339 (python-tests-look-at "finally\n")
3341 (equal (list (python-tests-look-at "else:" -
1 t
))
3342 (python-info-dedenter-opening-block-positions)))))
3344 (ert-deftest python-info-dedenter-opening-block-message-1
()
3345 "Test dedenters inside strings are ignored."
3346 (python-tests-with-temp-buffer
3351 logger.exception('something went wrong')
3354 (python-tests-look-at "except\n")
3355 (should (not (python-info-dedenter-opening-block-message)))))
3357 (ert-deftest python-info-dedenter-opening-block-message-2
()
3358 "Test except keyword."
3359 (python-tests-with-temp-buffer
3364 logger.exception('something went wrong')
3366 (python-tests-look-at "except:")
3369 (substring-no-properties
3370 (python-info-dedenter-opening-block-message))))
3374 (substring-no-properties
3375 (python-info-dedenter-opening-block-message))))))
3377 (ert-deftest python-info-dedenter-opening-block-message-3
()
3378 "Test else keyword."
3379 (python-tests-with-temp-buffer
3384 logger.exception('something went wrong')
3386 logger.debug('all good')
3388 (python-tests-look-at "else:")
3391 (substring-no-properties
3392 (python-info-dedenter-opening-block-message))))
3396 (substring-no-properties
3397 (python-info-dedenter-opening-block-message))))))
3399 (ert-deftest python-info-dedenter-opening-block-message-4
()
3400 "Test finally keyword."
3401 (python-tests-with-temp-buffer
3406 logger.exception('something went wrong')
3408 logger.debug('all good')
3412 (python-tests-look-at "finally:")
3415 (substring-no-properties
3416 (python-info-dedenter-opening-block-message))))
3420 (substring-no-properties
3421 (python-info-dedenter-opening-block-message))))))
3423 (ert-deftest python-info-dedenter-opening-block-message-5
()
3424 "Test elif keyword."
3425 (python-tests-with-temp-buffer
3431 (python-tests-look-at "elif b:")
3434 (substring-no-properties
3435 (python-info-dedenter-opening-block-message))))
3439 (substring-no-properties
3440 (python-info-dedenter-opening-block-message))))))
3443 (ert-deftest python-info-dedenter-statement-p-1
()
3444 "Test dedenters inside strings are ignored."
3445 (python-tests-with-temp-buffer
3450 logger.exception('something went wrong')
3453 (python-tests-look-at "except\n")
3454 (should (not (python-info-dedenter-statement-p)))))
3456 (ert-deftest python-info-dedenter-statement-p-2
()
3457 "Test except keyword."
3458 (python-tests-with-temp-buffer
3463 logger.exception('something went wrong')
3465 (python-tests-look-at "except:")
3466 (should (= (point) (python-info-dedenter-statement-p)))
3468 (should (= (save-excursion
3469 (back-to-indentation)
3471 (python-info-dedenter-statement-p)))))
3473 (ert-deftest python-info-dedenter-statement-p-3
()
3474 "Test else keyword."
3475 (python-tests-with-temp-buffer
3480 logger.exception('something went wrong')
3482 logger.debug('all good')
3484 (python-tests-look-at "else:")
3485 (should (= (point) (python-info-dedenter-statement-p)))
3487 (should (= (save-excursion
3488 (back-to-indentation)
3490 (python-info-dedenter-statement-p)))))
3492 (ert-deftest python-info-dedenter-statement-p-4
()
3493 "Test finally keyword."
3494 (python-tests-with-temp-buffer
3499 logger.exception('something went wrong')
3501 logger.debug('all good')
3505 (python-tests-look-at "finally:")
3506 (should (= (point) (python-info-dedenter-statement-p)))
3508 (should (= (save-excursion
3509 (back-to-indentation)
3511 (python-info-dedenter-statement-p)))))
3513 (ert-deftest python-info-dedenter-statement-p-5
()
3514 "Test elif keyword."
3515 (python-tests-with-temp-buffer
3521 (python-tests-look-at "elif b:")
3522 (should (= (point) (python-info-dedenter-statement-p)))
3524 (should (= (save-excursion
3525 (back-to-indentation)
3527 (python-info-dedenter-statement-p)))))
3529 (ert-deftest python-info-line-ends-backslash-p-1
()
3530 (python-tests-with-temp-buffer
3532 objects = Thing.objects.all() \\\\
3542 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
3543 (should (python-info-line-ends-backslash-p 3))
3544 (should (python-info-line-ends-backslash-p 4))
3545 (should (python-info-line-ends-backslash-p 5))
3546 (should (python-info-line-ends-backslash-p 6)) ; ) \...
3547 (should (python-info-line-ends-backslash-p 7))
3548 (should (python-info-line-ends-backslash-p 8))
3549 (should (python-info-line-ends-backslash-p 9))
3550 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
3552 (ert-deftest python-info-beginning-of-backslash-1
()
3553 (python-tests-with-temp-buffer
3555 objects = Thing.objects.all() \\\\
3566 (second (python-tests-look-at ".filter("))
3567 (third (python-tests-look-at ".aggregate(")))
3568 (should (= first
(python-info-beginning-of-backslash 2)))
3569 (should (= second
(python-info-beginning-of-backslash 3)))
3570 (should (= second
(python-info-beginning-of-backslash 4)))
3571 (should (= second
(python-info-beginning-of-backslash 5)))
3572 (should (= second
(python-info-beginning-of-backslash 6)))
3573 (should (= third
(python-info-beginning-of-backslash 7)))
3574 (should (= third
(python-info-beginning-of-backslash 8)))
3575 (should (= third
(python-info-beginning-of-backslash 9)))
3576 (should (not (python-info-beginning-of-backslash 10))))))
3578 (ert-deftest python-info-continuation-line-p-1
()
3579 (python-tests-with-temp-buffer
3581 if width == 0 and height == 0 and \\\\
3582 color == 'red' and emphasis == 'strong' or \\\\
3589 (python-tests-look-at "if width == 0 and height == 0 and")
3590 (should (not (python-info-continuation-line-p)))
3591 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3592 (should (python-info-continuation-line-p))
3593 (python-tests-look-at "highlight > 100:")
3594 (should (python-info-continuation-line-p))
3595 (python-tests-look-at "raise ValueError(")
3596 (should (not (python-info-continuation-line-p)))
3597 (python-tests-look-at "'sorry, you lose'")
3598 (should (python-info-continuation-line-p))
3600 (should (python-info-continuation-line-p))
3601 (python-tests-look-at ")")
3602 (should (python-info-continuation-line-p))
3604 (should (not (python-info-continuation-line-p)))))
3606 (ert-deftest python-info-block-continuation-line-p-1
()
3607 (python-tests-with-temp-buffer
3609 if width == 0 and height == 0 and \\\\
3610 color == 'red' and emphasis == 'strong' or \\\\
3617 (python-tests-look-at "if width == 0 and")
3618 (should (not (python-info-block-continuation-line-p)))
3619 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3620 (should (= (python-info-block-continuation-line-p)
3621 (python-tests-look-at "if width == 0 and" -
1 t
)))
3622 (python-tests-look-at "highlight > 100:")
3623 (should (not (python-info-block-continuation-line-p)))))
3625 (ert-deftest python-info-block-continuation-line-p-2
()
3626 (python-tests-with-temp-buffer
3633 (python-tests-look-at "def foo(a,")
3634 (should (not (python-info-block-continuation-line-p)))
3635 (python-tests-look-at "b,")
3636 (should (= (python-info-block-continuation-line-p)
3637 (python-tests-look-at "def foo(a," -
1 t
)))
3638 (python-tests-look-at "c):")
3639 (should (not (python-info-block-continuation-line-p)))))
3641 (ert-deftest python-info-assignment-continuation-line-p-1
()
3642 (python-tests-with-temp-buffer
3644 data = foo(), bar() \\\\
3648 (python-tests-look-at "data = foo(), bar()")
3649 (should (not (python-info-assignment-continuation-line-p)))
3650 (python-tests-look-at "baz(), 4")
3651 (should (= (python-info-assignment-continuation-line-p)
3652 (python-tests-look-at "foo()," -
1 t
)))
3653 (python-tests-look-at "5, 6")
3654 (should (not (python-info-assignment-continuation-line-p)))))
3656 (ert-deftest python-info-assignment-continuation-line-p-2
()
3657 (python-tests-with-temp-buffer
3659 data = (foo(), bar()
3663 (python-tests-look-at "data = (foo(), bar()")
3664 (should (not (python-info-assignment-continuation-line-p)))
3665 (python-tests-look-at "baz(), 4")
3666 (should (= (python-info-assignment-continuation-line-p)
3667 (python-tests-look-at "(foo()," -
1 t
)))
3668 (python-tests-look-at "5, 6)")
3669 (should (not (python-info-assignment-continuation-line-p)))))
3671 (ert-deftest python-info-looking-at-beginning-of-defun-1
()
3672 (python-tests-with-temp-buffer
3674 def decorat0r(deff):
3685 (python-tests-look-at "def decorat0r(deff):")
3686 (should (python-info-looking-at-beginning-of-defun))
3687 (python-tests-look-at "def foo(arg):")
3688 (should (not (python-info-looking-at-beginning-of-defun)))
3689 (python-tests-look-at "def wrap():")
3690 (should (python-info-looking-at-beginning-of-defun))
3691 (python-tests-look-at "deff()")
3692 (should (not (python-info-looking-at-beginning-of-defun)))))
3694 (ert-deftest python-info-current-line-comment-p-1
()
3695 (python-tests-with-temp-buffer
3698 foo = True # another comment
3702 print ('bar') # print bar
3704 (python-tests-look-at "# this is a comment")
3705 (should (python-info-current-line-comment-p))
3706 (python-tests-look-at "foo = True # another comment")
3707 (should (not (python-info-current-line-comment-p)))
3708 (python-tests-look-at "'#this is a string'")
3709 (should (not (python-info-current-line-comment-p)))
3710 (python-tests-look-at "# more comments")
3711 (should (python-info-current-line-comment-p))
3712 (python-tests-look-at "print ('bar') # print bar")
3713 (should (not (python-info-current-line-comment-p)))))
3715 (ert-deftest python-info-current-line-empty-p
()
3716 (python-tests-with-temp-buffer
3720 foo = True # another comment
3722 (should (python-info-current-line-empty-p))
3723 (python-tests-look-at "# this is a comment")
3724 (should (not (python-info-current-line-empty-p)))
3726 (should (python-info-current-line-empty-p))))
3729 ;;; Utility functions
3731 (ert-deftest python-util-goto-line-1
()
3732 (python-tests-with-temp-buffer
3737 pass" (make-string 20 ?
\n))
3738 (python-util-goto-line 10)
3739 (should (= (line-number-at-pos) 10))
3740 (python-util-goto-line 20)
3741 (should (= (line-number-at-pos) 20))))
3743 (ert-deftest python-util-clone-local-variables-1
()
3744 (let ((buffer (generate-new-buffer
3745 "python-util-clone-local-variables-1"))
3747 '((python-fill-docstring-style . django
)
3748 (python-shell-interpreter .
"python")
3749 (python-shell-interpreter-args .
"manage.py shell")
3750 (python-shell-prompt-regexp .
"In \\[[0-9]+\\]: ")
3751 (python-shell-prompt-output-regexp .
"Out\\[[0-9]+\\]: ")
3752 (python-shell-extra-pythonpaths "/home/user/pylib/")
3753 (python-shell-completion-setup-code
3754 .
"from IPython.core.completerlib import module_completion")
3755 (python-shell-completion-string-code
3756 .
"';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
3757 (python-shell-virtualenv-path
3758 .
"/home/user/.virtualenvs/project"))))
3759 (with-current-buffer buffer
3760 (kill-all-local-variables)
3761 (dolist (ccons varcons
)
3762 (set (make-local-variable (car ccons
)) (cdr ccons
))))
3763 (python-tests-with-temp-buffer
3765 (python-util-clone-local-variables buffer
)
3766 (dolist (ccons varcons
)
3768 (equal (symbol-value (car ccons
)) (cdr ccons
)))))
3769 (kill-buffer buffer
)))
3771 (ert-deftest python-util-strip-string-1
()
3772 (should (string= (python-util-strip-string "\t\r\n str") "str"))
3773 (should (string= (python-util-strip-string "str \n\r") "str"))
3774 (should (string= (python-util-strip-string "\t\r\n str \n\r ") "str"))
3776 (string= (python-util-strip-string "\n str \nin \tg \n\r") "str \nin \tg"))
3777 (should (string= (python-util-strip-string "\n \t \n\r ") ""))
3778 (should (string= (python-util-strip-string "") "")))
3780 (ert-deftest python-util-forward-comment-1
()
3781 (python-tests-with-temp-buffer
3785 # bad indented comment
3786 # more comments" (make-string 9999 ?
\n))
3787 (python-util-forward-comment 1)
3788 (should (= (point) (point-max)))
3789 (python-util-forward-comment -
1)
3790 (should (= (point) (point-min)))))
3792 (ert-deftest python-util-valid-regexp-p-1
()
3793 (should (python-util-valid-regexp-p ""))
3794 (should (python-util-valid-regexp-p python-shell-prompt-regexp
))
3795 (should (not (python-util-valid-regexp-p "\\("))))
3800 (ert-deftest python-parens-electric-indent-1
()
3802 (let ((eim electric-indent-mode
))
3805 (python-tests-with-temp-buffer
3807 from django.conf.urls import patterns, include, url
3809 from django.contrib import admin
3811 from myapp import views
3814 urlpatterns = patterns('',
3815 url(r'^$', views.index
3818 (electric-indent-mode 1)
3819 (python-tests-look-at "views.index")
3822 ;; Inserting commas within the same line should leave
3823 ;; indentation unchanged.
3824 (python-tests-self-insert ",")
3825 (should (= (current-indentation) 4))
3827 ;; As well as any other input happening within the same
3829 (python-tests-self-insert " name='index')")
3830 (should (= (current-indentation) 4))
3832 ;; But a comma outside it, should trigger indentation.
3833 (python-tests-self-insert ",")
3834 (should (= (current-indentation) 23))
3836 ;; Newline indents to the first argument column
3837 (python-tests-self-insert "\n")
3838 (should (= (current-indentation) 23))
3840 ;; All this input must not change indentation
3842 (python-tests-self-insert "url(r'^/login$', views.login)")
3843 (should (= (current-indentation) 4))
3845 ;; But this comma does
3846 (python-tests-self-insert ",")
3847 (should (= (current-indentation) 23))))
3848 (or eim
(electric-indent-mode -
1)))))
3850 (ert-deftest python-triple-quote-pairing
()
3852 (let ((epm electric-pair-mode
))
3855 (python-tests-with-temp-buffer
3857 (or epm
(electric-pair-mode 1))
3858 (goto-char (1- (point-max)))
3859 (python-tests-self-insert ?
\")
3860 (should (string= (buffer-string)
3862 (should (= (point) 4)))
3863 (python-tests-with-temp-buffer
3865 (python-tests-self-insert (list ?
\" ?
\" ?
\"))
3866 (should (string= (buffer-string)
3868 (should (= (point) 4)))
3869 (python-tests-with-temp-buffer
3871 (goto-char (1- (point-max)))
3872 (python-tests-self-insert ?
\")
3873 (should (= (point) (1- (point-max))))
3874 (should (string= (buffer-string)
3876 (or epm
(electric-pair-mode -
1)))))
3879 (provide 'python-tests
)
3883 ;; indent-tabs-mode: nil
3886 ;;; python-tests.el ends here