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)))))
717 (ert-deftest python-nav-beginning-of-defun-1
()
718 (python-tests-with-temp-buffer
720 def decoratorFunctionWithArguments(arg1, arg2, arg3):
721 '''print decorated function call data to stdout.
725 @decoratorFunctionWithArguments('arg1', 'arg2')
726 def func(a, b, c=True):
731 print 'Inside wwrap()'
732 def wrapped_f(*args):
733 print 'Inside wrapped_f()'
734 print 'Decorator arguments:', arg1, arg2, arg3
736 print 'After f(*args)'
740 (python-tests-look-at "return wrap")
741 (should (= (save-excursion
742 (python-nav-beginning-of-defun)
745 (python-tests-look-at "def wrapped_f(*args):" -
1)
748 (python-tests-look-at "def wrapped_f(*args):" -
1)
749 (should (= (save-excursion
750 (python-nav-beginning-of-defun)
753 (python-tests-look-at "def wwrap(f):" -
1)
756 (python-tests-look-at "def wwrap(f):" -
1)
757 (should (= (save-excursion
758 (python-nav-beginning-of-defun)
761 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)
765 (ert-deftest python-nav-beginning-of-defun-2
()
766 (python-tests-with-temp-buffer
782 ;; Nested defuns, are handled with care.
783 (python-tests-look-at "def c(self):")
784 (should (= (save-excursion
785 (python-nav-beginning-of-defun)
788 (python-tests-look-at "def m(self):" -
1)
791 ;; Defuns on same levels should be respected.
792 (python-tests-look-at "def a():" -
1)
793 (should (= (save-excursion
794 (python-nav-beginning-of-defun)
797 (python-tests-look-at "def b():" -
1)
800 ;; Jump to a top level defun.
801 (python-tests-look-at "def b():" -
1)
802 (should (= (save-excursion
803 (python-nav-beginning-of-defun)
806 (python-tests-look-at "def m(self):" -
1)
809 ;; Jump to a top level defun again.
810 (python-tests-look-at "def m(self):" -
1)
811 (should (= (save-excursion
812 (python-nav-beginning-of-defun)
815 (python-tests-look-at "class C(object):" -
1)
819 (ert-deftest python-nav-end-of-defun-1
()
820 (python-tests-with-temp-buffer
836 (should (= (save-excursion
837 (python-tests-look-at "class C(object):")
838 (python-nav-end-of-defun)
842 (should (= (save-excursion
843 (python-tests-look-at "def m(self):")
844 (python-nav-end-of-defun)
847 (python-tests-look-at "def c(self):")
850 (should (= (save-excursion
851 (python-tests-look-at "def b():")
852 (python-nav-end-of-defun)
855 (python-tests-look-at "def b():")
858 (should (= (save-excursion
859 (python-tests-look-at "def c(self):")
860 (python-nav-end-of-defun)
865 (ert-deftest python-nav-end-of-defun-2
()
866 (python-tests-with-temp-buffer
868 def decoratorFunctionWithArguments(arg1, arg2, arg3):
869 '''print decorated function call data to stdout.
873 @decoratorFunctionWithArguments('arg1', 'arg2')
874 def func(a, b, c=True):
879 print 'Inside wwrap()'
880 def wrapped_f(*args):
881 print 'Inside wrapped_f()'
882 print 'Decorator arguments:', arg1, arg2, arg3
884 print 'After f(*args)'
888 (should (= (save-excursion
889 (python-tests-look-at "def decoratorFunctionWithArguments")
890 (python-nav-end-of-defun)
894 (should (= (save-excursion
895 (python-tests-look-at "@decoratorFunctionWithArguments")
896 (python-nav-end-of-defun)
900 (should (= (save-excursion
901 (python-tests-look-at "def wwrap(f):")
902 (python-nav-end-of-defun)
905 (python-tests-look-at "return wwrap")
906 (line-beginning-position))))
907 (should (= (save-excursion
908 (python-tests-look-at "def wrapped_f(*args):")
909 (python-nav-end-of-defun)
912 (python-tests-look-at "return wrapped_f")
913 (line-beginning-position))))
914 (should (= (save-excursion
915 (python-tests-look-at "f(*args)")
916 (python-nav-end-of-defun)
919 (python-tests-look-at "return wrapped_f")
920 (line-beginning-position))))))
922 (ert-deftest python-nav-backward-defun-1
()
923 (python-tests-with-temp-buffer
949 (goto-char (point-max))
950 (should (= (save-excursion (python-nav-backward-defun))
951 (python-tests-look-at " def c(self): # c" -
1)))
952 (should (= (save-excursion (python-nav-backward-defun))
953 (python-tests-look-at " def d(self): # d" -
1)))
954 (should (= (save-excursion (python-nav-backward-defun))
955 (python-tests-look-at " class C(object): # C" -
1)))
956 (should (= (save-excursion (python-nav-backward-defun))
957 (python-tests-look-at " class B(object): # B" -
1)))
958 (should (= (save-excursion (python-nav-backward-defun))
959 (python-tests-look-at " def b(self): # b" -
1)))
960 (should (= (save-excursion (python-nav-backward-defun))
961 (python-tests-look-at " def a(self): # a" -
1)))
962 (should (= (save-excursion (python-nav-backward-defun))
963 (python-tests-look-at "class A(object): # A" -
1)))
964 (should (not (python-nav-backward-defun)))))
966 (ert-deftest python-nav-backward-defun-2
()
967 (python-tests-with-temp-buffer
969 def decoratorFunctionWithArguments(arg1, arg2, arg3):
970 '''print decorated function call data to stdout.
974 @decoratorFunctionWithArguments('arg1', 'arg2')
975 def func(a, b, c=True):
980 print 'Inside wwrap()'
981 def wrapped_f(*args):
982 print 'Inside wrapped_f()'
983 print 'Decorator arguments:', arg1, arg2, arg3
985 print 'After f(*args)'
989 (goto-char (point-max))
990 (should (= (save-excursion (python-nav-backward-defun))
991 (python-tests-look-at " def wrapped_f(*args):" -
1)))
992 (should (= (save-excursion (python-nav-backward-defun))
993 (python-tests-look-at " def wwrap(f):" -
1)))
994 (should (= (save-excursion (python-nav-backward-defun))
995 (python-tests-look-at "def decoratorFunctionWithArguments(arg1, arg2, arg3):" -
1)))
996 (should (not (python-nav-backward-defun)))))
998 (ert-deftest python-nav-backward-defun-3
()
999 (python-tests-with-temp-buffer
1015 (goto-char (point-min))
1016 (let ((point (python-tests-look-at "class A(object):")))
1017 (should (not (python-nav-backward-defun)))
1018 (should (= point
(point))))))
1020 (ert-deftest python-nav-forward-defun-1
()
1021 (python-tests-with-temp-buffer
1023 class A(object): # A
1031 class B(object): # B
1033 class C(object): # C
1047 (goto-char (point-min))
1048 (should (= (save-excursion (python-nav-forward-defun))
1049 (python-tests-look-at "(object): # A")))
1050 (should (= (save-excursion (python-nav-forward-defun))
1051 (python-tests-look-at "(self): # a")))
1052 (should (= (save-excursion (python-nav-forward-defun))
1053 (python-tests-look-at "(self): # b")))
1054 (should (= (save-excursion (python-nav-forward-defun))
1055 (python-tests-look-at "(object): # B")))
1056 (should (= (save-excursion (python-nav-forward-defun))
1057 (python-tests-look-at "(object): # C")))
1058 (should (= (save-excursion (python-nav-forward-defun))
1059 (python-tests-look-at "(self): # d")))
1060 (should (= (save-excursion (python-nav-forward-defun))
1061 (python-tests-look-at "(self): # c")))
1062 (should (not (python-nav-forward-defun)))))
1064 (ert-deftest python-nav-forward-defun-2
()
1065 (python-tests-with-temp-buffer
1067 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1068 '''print decorated function call data to stdout.
1072 @decoratorFunctionWithArguments('arg1', 'arg2')
1073 def func(a, b, c=True):
1078 print 'Inside wwrap()'
1079 def wrapped_f(*args):
1080 print 'Inside wrapped_f()'
1081 print 'Decorator arguments:', arg1, arg2, arg3
1083 print 'After f(*args)'
1087 (goto-char (point-min))
1088 (should (= (save-excursion (python-nav-forward-defun))
1089 (python-tests-look-at "(arg1, arg2, arg3):")))
1090 (should (= (save-excursion (python-nav-forward-defun))
1091 (python-tests-look-at "(f):")))
1092 (should (= (save-excursion (python-nav-forward-defun))
1093 (python-tests-look-at "(*args):")))
1094 (should (not (python-nav-forward-defun)))))
1096 (ert-deftest python-nav-forward-defun-3
()
1097 (python-tests-with-temp-buffer
1113 (goto-char (point-min))
1114 (let ((point (python-tests-look-at "(object):")))
1115 (should (not (python-nav-forward-defun)))
1116 (should (= point
(point))))))
1118 (ert-deftest python-nav-beginning-of-statement-1
()
1119 (python-tests-with-temp-buffer
1129 v3 = ('this is a string'
1134 # this is a comment, yo
1135 'continue previous line')
1141 (python-tests-look-at "v2 =")
1142 (python-util-forward-comment -
1)
1143 (should (= (save-excursion
1144 (python-nav-beginning-of-statement)
1146 (python-tests-look-at "v1 =" -
1 t
)))
1147 (python-tests-look-at "v3 =")
1148 (python-util-forward-comment -
1)
1149 (should (= (save-excursion
1150 (python-nav-beginning-of-statement)
1152 (python-tests-look-at "v2 =" -
1 t
)))
1153 (python-tests-look-at "v4 =")
1154 (python-util-forward-comment -
1)
1155 (should (= (save-excursion
1156 (python-nav-beginning-of-statement)
1158 (python-tests-look-at "v3 =" -
1 t
)))
1159 (goto-char (point-max))
1160 (python-util-forward-comment -
1)
1161 (should (= (save-excursion
1162 (python-nav-beginning-of-statement)
1164 (python-tests-look-at "v4 =" -
1 t
)))))
1166 (ert-deftest python-nav-end-of-statement-1
()
1167 (python-tests-with-temp-buffer
1177 v3 = ('this is a string'
1182 # this is a comment, yo
1183 'continue previous line')
1189 (python-tests-look-at "v1 =")
1190 (should (= (save-excursion
1191 (python-nav-end-of-statement)
1194 (python-tests-look-at "789")
1195 (line-end-position))))
1196 (python-tests-look-at "v2 =")
1197 (should (= (save-excursion
1198 (python-nav-end-of-statement)
1201 (python-tests-look-at "value4)")
1202 (line-end-position))))
1203 (python-tests-look-at "v3 =")
1204 (should (= (save-excursion
1205 (python-nav-end-of-statement)
1208 (python-tests-look-at
1209 "'continue previous line')")
1210 (line-end-position))))
1211 (python-tests-look-at "v4 =")
1212 (should (= (save-excursion
1213 (python-nav-end-of-statement)
1216 (goto-char (point-max))
1217 (python-util-forward-comment -
1)
1220 (ert-deftest python-nav-forward-statement-1
()
1221 (python-tests-with-temp-buffer
1231 v3 = ('this is a string'
1236 # this is a comment, yo
1237 'continue previous line')
1243 (python-tests-look-at "v1 =")
1244 (should (= (save-excursion
1245 (python-nav-forward-statement)
1247 (python-tests-look-at "v2 =")))
1248 (should (= (save-excursion
1249 (python-nav-forward-statement)
1251 (python-tests-look-at "v3 =")))
1252 (should (= (save-excursion
1253 (python-nav-forward-statement)
1255 (python-tests-look-at "v4 =")))
1256 (should (= (save-excursion
1257 (python-nav-forward-statement)
1261 (ert-deftest python-nav-backward-statement-1
()
1262 (python-tests-with-temp-buffer
1272 v3 = ('this is a string'
1277 # this is a comment, yo
1278 'continue previous line')
1284 (goto-char (point-max))
1285 (should (= (save-excursion
1286 (python-nav-backward-statement)
1288 (python-tests-look-at "v4 =" -
1)))
1289 (should (= (save-excursion
1290 (python-nav-backward-statement)
1292 (python-tests-look-at "v3 =" -
1)))
1293 (should (= (save-excursion
1294 (python-nav-backward-statement)
1296 (python-tests-look-at "v2 =" -
1)))
1297 (should (= (save-excursion
1298 (python-nav-backward-statement)
1300 (python-tests-look-at "v1 =" -
1)))))
1302 (ert-deftest python-nav-backward-statement-2
()
1303 :expected-result
:failed
1304 (python-tests-with-temp-buffer
1315 ;; FIXME: For some reason `python-nav-backward-statement' is moving
1316 ;; back two sentences when starting from 'value4)'.
1317 (goto-char (point-max))
1318 (python-util-forward-comment -
1)
1319 (should (= (save-excursion
1320 (python-nav-backward-statement)
1322 (python-tests-look-at "v2 =" -
1 t
)))))
1324 (ert-deftest python-nav-beginning-of-block-1
()
1325 (python-tests-with-temp-buffer
1327 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1328 '''print decorated function call data to stdout.
1332 @decoratorFunctionWithArguments('arg1', 'arg2')
1333 def func(a, b, c=True):
1338 print 'Inside wwrap()'
1339 def wrapped_f(*args):
1340 print 'Inside wrapped_f()'
1341 print 'Decorator arguments:', arg1, arg2, arg3
1343 print 'After f(*args)'
1347 (python-tests-look-at "return wwrap")
1348 (should (= (save-excursion
1349 (python-nav-beginning-of-block)
1351 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)))
1352 (python-tests-look-at "print 'Inside wwrap()'")
1353 (should (= (save-excursion
1354 (python-nav-beginning-of-block)
1356 (python-tests-look-at "def wwrap(f):" -
1)))
1357 (python-tests-look-at "print 'After f(*args)'")
1359 (should (= (save-excursion
1360 (python-nav-beginning-of-block)
1362 (python-tests-look-at "def wrapped_f(*args):" -
1)))
1363 (python-tests-look-at "return wrapped_f")
1364 (should (= (save-excursion
1365 (python-nav-beginning-of-block)
1367 (python-tests-look-at "def wwrap(f):" -
1)))))
1369 (ert-deftest python-nav-end-of-block-1
()
1370 (python-tests-with-temp-buffer
1372 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1373 '''print decorated function call data to stdout.
1377 @decoratorFunctionWithArguments('arg1', 'arg2')
1378 def func(a, b, c=True):
1383 print 'Inside wwrap()'
1384 def wrapped_f(*args):
1385 print 'Inside wrapped_f()'
1386 print 'Decorator arguments:', arg1, arg2, arg3
1388 print 'After f(*args)'
1392 (python-tests-look-at "def decoratorFunctionWithArguments")
1393 (should (= (save-excursion
1394 (python-nav-end-of-block)
1397 (goto-char (point-max))
1398 (python-util-forward-comment -
1)
1400 (python-tests-look-at "def wwrap(f):")
1401 (should (= (save-excursion
1402 (python-nav-end-of-block)
1405 (python-tests-look-at "return wrapped_f")
1406 (line-end-position))))
1408 (should (= (save-excursion
1409 (python-nav-end-of-block)
1412 (python-tests-look-at "return wrapped_f")
1413 (line-end-position))))
1414 (python-tests-look-at "f(*args)")
1415 (should (= (save-excursion
1416 (python-nav-end-of-block)
1419 (python-tests-look-at "print 'After f(*args)'")
1420 (line-end-position))))))
1422 (ert-deftest python-nav-forward-block-1
()
1423 "This also accounts as a test for `python-nav-backward-block'."
1424 (python-tests-with-temp-buffer
1426 if request.user.is_authenticated():
1430 profile = request.user.get_profile()
1431 except Profile.DoesNotExist:
1432 profile = Profile.objects.create(user=request.user)
1435 profile.recalculate_stats()
1437 profile.clear_stats()
1442 (should (= (save-excursion (python-nav-forward-block))
1443 (python-tests-look-at "if request.user.is_authenticated():")))
1444 (should (= (save-excursion (python-nav-forward-block))
1445 (python-tests-look-at "try:")))
1446 (should (= (save-excursion (python-nav-forward-block))
1447 (python-tests-look-at "except Profile.DoesNotExist:")))
1448 (should (= (save-excursion (python-nav-forward-block))
1449 (python-tests-look-at "else:")))
1450 (should (= (save-excursion (python-nav-forward-block))
1451 (python-tests-look-at "if profile.stats:")))
1452 (should (= (save-excursion (python-nav-forward-block))
1453 (python-tests-look-at "else:")))
1454 (should (= (save-excursion (python-nav-forward-block))
1455 (python-tests-look-at "finally:")))
1456 ;; When point is at the last block, leave it there and return nil
1457 (should (not (save-excursion (python-nav-forward-block))))
1458 ;; Move backwards, and even if the number of moves is less than the
1459 ;; provided argument return the point.
1460 (should (= (save-excursion (python-nav-forward-block -
10))
1461 (python-tests-look-at
1462 "if request.user.is_authenticated():" -
1)))))
1464 (ert-deftest python-nav-forward-sexp-1
()
1465 (python-tests-with-temp-buffer
1471 (python-tests-look-at "a()")
1472 (python-nav-forward-sexp)
1473 (should (looking-at "$"))
1474 (should (save-excursion
1476 (looking-at "a()")))
1477 (python-nav-forward-sexp)
1478 (should (looking-at "$"))
1479 (should (save-excursion
1481 (looking-at "b()")))
1482 (python-nav-forward-sexp)
1483 (should (looking-at "$"))
1484 (should (save-excursion
1486 (looking-at "c()")))
1487 ;; Movement next to a paren should do what lisp does and
1488 ;; unfortunately It can't change, because otherwise
1489 ;; `blink-matching-open' breaks.
1490 (python-nav-forward-sexp -
1)
1491 (should (looking-at "()"))
1492 (should (save-excursion
1494 (looking-at "c()")))
1495 (python-nav-forward-sexp -
1)
1496 (should (looking-at "c()"))
1497 (python-nav-forward-sexp -
1)
1498 (should (looking-at "b()"))
1499 (python-nav-forward-sexp -
1)
1500 (should (looking-at "a()"))))
1502 (ert-deftest python-nav-forward-sexp-2
()
1503 (python-tests-with-temp-buffer
1512 (python-tests-look-at "aa =")
1513 (python-nav-forward-sexp)
1514 (should (looking-at " = bbb"))
1515 (python-nav-forward-sexp)
1516 (should (looking-at "$"))
1517 (should (save-excursion
1518 (back-to-indentation)
1519 (looking-at "aaa = bbb")))
1520 (python-nav-forward-sexp)
1521 (should (looking-at "$"))
1522 (should (save-excursion
1523 (back-to-indentation)
1524 (looking-at "ccc = ddd")))
1525 (python-nav-forward-sexp)
1526 (should (looking-at "$"))
1527 (should (save-excursion
1528 (back-to-indentation)
1529 (looking-at "eee = fff")))
1530 (python-nav-forward-sexp)
1531 (should (looking-at "$"))
1532 (should (save-excursion
1533 (back-to-indentation)
1534 (looking-at "return ggg")))
1535 (python-nav-forward-sexp -
1)
1536 (should (looking-at "def func():"))))
1538 (ert-deftest python-nav-forward-sexp-3
()
1539 (python-tests-with-temp-buffer
1541 from some_module import some_sub_module
1542 from another_module import another_sub_module
1544 def another_statement():
1547 (python-tests-look-at "some_module")
1548 (python-nav-forward-sexp)
1549 (should (looking-at " import"))
1550 (python-nav-forward-sexp)
1551 (should (looking-at " some_sub_module"))
1552 (python-nav-forward-sexp)
1553 (should (looking-at "$"))
1556 (back-to-indentation)
1558 "from some_module import some_sub_module")))
1559 (python-nav-forward-sexp)
1560 (should (looking-at "$"))
1563 (back-to-indentation)
1565 "from another_module import another_sub_module")))
1566 (python-nav-forward-sexp)
1567 (should (looking-at "$"))
1570 (back-to-indentation)
1573 (python-nav-forward-sexp -
1)
1574 (should (looking-at "def another_statement():"))
1575 (python-nav-forward-sexp -
1)
1576 (should (looking-at "from another_module import another_sub_module"))
1577 (python-nav-forward-sexp -
1)
1578 (should (looking-at "from some_module import some_sub_module"))))
1580 (ert-deftest python-nav-forward-sexp-safe-1
()
1581 (python-tests-with-temp-buffer
1583 profile = Profile.objects.create(user=request.user)
1586 (python-tests-look-at "profile =")
1587 (python-nav-forward-sexp-safe 1)
1588 (should (looking-at "$"))
1589 (beginning-of-line 1)
1590 (python-tests-look-at "user=request.user")
1591 (python-nav-forward-sexp-safe -
1)
1592 (should (looking-at "(user=request.user)"))
1593 (python-nav-forward-sexp-safe -
4)
1594 (should (looking-at "profile ="))
1595 (python-tests-look-at "user=request.user")
1596 (python-nav-forward-sexp-safe 3)
1597 (should (looking-at ")"))
1598 (python-nav-forward-sexp-safe 1)
1599 (should (looking-at "$"))
1600 (python-nav-forward-sexp-safe 1)
1601 (should (looking-at "$"))))
1603 (ert-deftest python-nav-up-list-1
()
1604 (python-tests-with-temp-buffer
1608 return [i for i in range(3)]
1610 (python-tests-look-at "3)]")
1611 (python-nav-up-list)
1612 (should (looking-at "]"))
1613 (python-nav-up-list)
1614 (should (looking-at "$"))))
1616 (ert-deftest python-nav-backward-up-list-1
()
1617 :expected-result
:failed
1618 (python-tests-with-temp-buffer
1622 return [i for i in range(3)]
1624 (python-tests-look-at "3)]")
1625 (python-nav-backward-up-list)
1626 (should (looking-at "(3)\\]"))
1627 (python-nav-backward-up-list)
1629 "\\[i for i in range(3)\\]"))
1630 ;; FIXME: Need to move to beginning-of-statement.
1631 (python-nav-backward-up-list)
1633 "return \\[i for i in range(3)\\]"))
1634 (python-nav-backward-up-list)
1635 (should (looking-at "if True:"))
1636 (python-nav-backward-up-list)
1637 (should (looking-at "def f():"))))
1640 ;;; Shell integration
1642 (defvar python-tests-shell-interpreter
"python")
1644 (ert-deftest python-shell-get-process-name-1
()
1645 "Check process name calculation on different scenarios."
1646 (python-tests-with-temp-buffer
1648 (should (string= (python-shell-get-process-name nil
)
1649 python-shell-buffer-name
))
1650 ;; When the `current-buffer' doesn't have `buffer-file-name', even
1651 ;; if dedicated flag is non-nil should not include its name.
1652 (should (string= (python-shell-get-process-name t
)
1653 python-shell-buffer-name
)))
1654 (python-tests-with-temp-file
1656 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1657 ;; should be respected.
1658 (should (string= (python-shell-get-process-name nil
)
1659 python-shell-buffer-name
))
1661 (python-shell-get-process-name t
)
1662 (format "%s[%s]" python-shell-buffer-name buffer-file-name
)))))
1664 (ert-deftest python-shell-internal-get-process-name-1
()
1665 "Check the internal process name is config-unique."
1666 (let* ((python-shell-interpreter python-tests-shell-interpreter
)
1667 (python-shell-interpreter-args "")
1668 (python-shell-prompt-regexp ">>> ")
1669 (python-shell-prompt-block-regexp "[.][.][.] ")
1670 (python-shell-setup-codes "")
1671 (python-shell-process-environment "")
1672 (python-shell-extra-pythonpaths "")
1673 (python-shell-exec-path "")
1674 (python-shell-virtualenv-path "")
1675 (expected (python-tests-with-temp-buffer
1676 "" (python-shell-internal-get-process-name))))
1677 ;; Same configurations should match.
1680 (python-tests-with-temp-buffer
1681 "" (python-shell-internal-get-process-name))))
1682 (let ((python-shell-interpreter-args "-B"))
1683 ;; A minimal change should generate different names.
1687 (python-tests-with-temp-buffer
1688 "" (python-shell-internal-get-process-name))))))))
1690 (ert-deftest python-shell-parse-command-1
()
1691 "Check the command to execute is calculated correctly.
1692 Using `python-shell-interpreter' and
1693 `python-shell-interpreter-args'."
1694 (skip-unless (executable-find python-tests-shell-interpreter
))
1695 (let ((python-shell-interpreter (executable-find
1696 python-tests-shell-interpreter
))
1697 (python-shell-interpreter-args "-B"))
1700 python-shell-interpreter
1701 python-shell-interpreter-args
)
1702 (python-shell-parse-command)))))
1704 (ert-deftest python-shell-calculate-process-environment-1
()
1705 "Test `python-shell-process-environment' modification."
1706 (let* ((original-process-environment process-environment
)
1707 (python-shell-process-environment
1708 '("TESTVAR1=value1" "TESTVAR2=value2"))
1709 (process-environment
1710 (python-shell-calculate-process-environment)))
1711 (should (equal (getenv "TESTVAR1") "value1"))
1712 (should (equal (getenv "TESTVAR2") "value2"))))
1714 (ert-deftest python-shell-calculate-process-environment-2
()
1715 "Test `python-shell-extra-pythonpaths' modification."
1716 (let* ((original-process-environment process-environment
)
1717 (original-pythonpath (getenv "PYTHONPATH"))
1718 (paths '("path1" "path2"))
1719 (python-shell-extra-pythonpaths paths
)
1720 (process-environment
1721 (python-shell-calculate-process-environment)))
1722 (should (equal (getenv "PYTHONPATH")
1724 (mapconcat 'identity paths path-separator
)
1725 path-separator original-pythonpath
)))))
1727 (ert-deftest python-shell-calculate-process-environment-3
()
1728 "Test `python-shell-virtualenv-path' modification."
1729 (let* ((original-process-environment process-environment
)
1730 (original-path (or (getenv "PATH") ""))
1731 (python-shell-virtualenv-path
1732 (directory-file-name user-emacs-directory
))
1733 (process-environment
1734 (python-shell-calculate-process-environment)))
1735 (should (not (getenv "PYTHONHOME")))
1736 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-path
))
1737 (should (equal (getenv "PATH")
1738 (format "%s/bin%s%s"
1739 python-shell-virtualenv-path
1740 path-separator original-path
)))))
1742 (ert-deftest python-shell-calculate-exec-path-1
()
1743 "Test `python-shell-exec-path' modification."
1744 (let* ((original-exec-path exec-path
)
1745 (python-shell-exec-path '("path1" "path2"))
1746 (exec-path (python-shell-calculate-exec-path)))
1749 (append python-shell-exec-path
1750 original-exec-path
)))))
1752 (ert-deftest python-shell-calculate-exec-path-2
()
1753 "Test `python-shell-exec-path' modification."
1754 (let* ((original-exec-path exec-path
)
1755 (python-shell-virtualenv-path
1756 (directory-file-name user-emacs-directory
))
1757 (exec-path (python-shell-calculate-exec-path)))
1761 (format "%s/bin" python-shell-virtualenv-path
)
1762 original-exec-path
))))))
1764 (ert-deftest python-shell-make-comint-1
()
1765 "Check comint creation for global shell buffer."
1766 (skip-unless (executable-find python-tests-shell-interpreter
))
1767 ;; The interpreter can get killed too quickly to allow it to clean
1768 ;; up the tempfiles that the default python-shell-setup-codes create,
1769 ;; so it leaves tempfiles behind, which is a minor irritation.
1770 (let* ((python-shell-setup-codes nil
)
1771 (python-shell-interpreter
1772 (executable-find python-tests-shell-interpreter
))
1773 (proc-name (python-shell-get-process-name nil
))
1775 (python-tests-with-temp-buffer
1776 "" (python-shell-make-comint
1777 (python-shell-parse-command) proc-name
)))
1778 (process (get-buffer-process shell-buffer
)))
1781 (set-process-query-on-exit-flag process nil
)
1782 (should (process-live-p process
))
1783 (with-current-buffer shell-buffer
1784 (should (eq major-mode
'inferior-python-mode
))
1785 (should (string= (buffer-name) (format "*%s*" proc-name
)))))
1786 (kill-buffer shell-buffer
))))
1788 (ert-deftest python-shell-make-comint-2
()
1789 "Check comint creation for internal shell buffer."
1790 (skip-unless (executable-find python-tests-shell-interpreter
))
1791 (let* ((python-shell-setup-codes nil
)
1792 (python-shell-interpreter
1793 (executable-find python-tests-shell-interpreter
))
1794 (proc-name (python-shell-internal-get-process-name))
1796 (python-tests-with-temp-buffer
1797 "" (python-shell-make-comint
1798 (python-shell-parse-command) proc-name nil t
)))
1799 (process (get-buffer-process shell-buffer
)))
1802 (set-process-query-on-exit-flag process nil
)
1803 (should (process-live-p process
))
1804 (with-current-buffer shell-buffer
1805 (should (eq major-mode
'inferior-python-mode
))
1806 (should (string= (buffer-name) (format " *%s*" proc-name
)))))
1807 (kill-buffer shell-buffer
))))
1809 (ert-deftest python-shell-make-comint-3
()
1810 "Check comint creation with overriden python interpreter and args.
1811 The command passed to `python-shell-make-comint' as argument must
1812 locally override global values set in `python-shell-interpreter'
1813 and `python-shell-interpreter-args' in the new shell buffer."
1814 (skip-unless (executable-find python-tests-shell-interpreter
))
1815 (let* ((python-shell-setup-codes nil
)
1816 (python-shell-interpreter "interpreter")
1817 (python-shell-interpreter-args "--some-args")
1818 (proc-name (python-shell-get-process-name nil
))
1819 (interpreter-override
1820 (concat (executable-find python-tests-shell-interpreter
) " " "-i"))
1822 (python-tests-with-temp-buffer
1823 "" (python-shell-make-comint interpreter-override proc-name nil
)))
1824 (process (get-buffer-process shell-buffer
)))
1827 (set-process-query-on-exit-flag process nil
)
1828 (should (process-live-p process
))
1829 (with-current-buffer shell-buffer
1830 (should (eq major-mode
'inferior-python-mode
))
1831 (should (string= python-shell-interpreter
1832 (executable-find python-tests-shell-interpreter
)))
1833 (should (string= python-shell-interpreter-args
"-i"))))
1834 (kill-buffer shell-buffer
))))
1836 (ert-deftest python-shell-make-comint-4
()
1837 "Check shell calculated prompts regexps are set."
1838 (skip-unless (executable-find python-tests-shell-interpreter
))
1839 (let* ((process-environment process-environment
)
1840 (python-shell-setup-codes nil
)
1841 (python-shell-interpreter
1842 (executable-find python-tests-shell-interpreter
))
1843 (python-shell-interpreter-args "-i")
1844 (python-shell--prompt-calculated-input-regexp nil
)
1845 (python-shell--prompt-calculated-output-regexp nil
)
1846 (python-shell-prompt-detect-enabled t
)
1847 (python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
1848 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
1849 (python-shell-prompt-regexp "in")
1850 (python-shell-prompt-block-regexp "block")
1851 (python-shell-prompt-pdb-regexp "pdf")
1852 (python-shell-prompt-output-regexp "output")
1853 (startup-code (concat "import sys\n"
1854 "sys.ps1 = 'py> '\n"
1855 "sys.ps2 = '..> '\n"
1856 "sys.ps3 = 'out '\n"))
1857 (startup-file (python-shell--save-temp-file startup-code
))
1858 (proc-name (python-shell-get-process-name nil
))
1861 (setenv "PYTHONSTARTUP" startup-file
)
1862 (python-tests-with-temp-buffer
1863 "" (python-shell-make-comint
1864 (python-shell-parse-command) proc-name nil
))))
1865 (process (get-buffer-process shell-buffer
)))
1868 (set-process-query-on-exit-flag process nil
)
1869 (should (process-live-p process
))
1870 (with-current-buffer shell-buffer
1871 (should (eq major-mode
'inferior-python-mode
))
1873 python-shell--prompt-calculated-input-regexp
1874 (concat "^\\(extralargeinputprompt\\|\\.\\.> \\|"
1875 "block\\|py> \\|pdf\\|sml\\|in\\)")))
1877 python-shell--prompt-calculated-output-regexp
1878 "^\\(extralargeoutputprompt\\|output\\|out \\|sml\\)"))))
1879 (delete-file startup-file
)
1880 (kill-buffer shell-buffer
))))
1882 (ert-deftest python-shell-get-process-1
()
1883 "Check dedicated shell process preference over global."
1884 (skip-unless (executable-find python-tests-shell-interpreter
))
1885 (python-tests-with-temp-file
1887 (let* ((python-shell-setup-codes nil
)
1888 (python-shell-interpreter
1889 (executable-find python-tests-shell-interpreter
))
1890 (global-proc-name (python-shell-get-process-name nil
))
1891 (dedicated-proc-name (python-shell-get-process-name t
))
1892 (global-shell-buffer
1893 (python-shell-make-comint
1894 (python-shell-parse-command) global-proc-name
))
1895 (dedicated-shell-buffer
1896 (python-shell-make-comint
1897 (python-shell-parse-command) dedicated-proc-name
))
1898 (global-process (get-buffer-process global-shell-buffer
))
1899 (dedicated-process (get-buffer-process dedicated-shell-buffer
)))
1902 (set-process-query-on-exit-flag global-process nil
)
1903 (set-process-query-on-exit-flag dedicated-process nil
)
1904 ;; Prefer dedicated if global also exists.
1905 (should (equal (python-shell-get-process) dedicated-process
))
1906 (kill-buffer dedicated-shell-buffer
)
1907 ;; If there's only global, use it.
1908 (should (equal (python-shell-get-process) global-process
))
1909 (kill-buffer global-shell-buffer
)
1910 ;; No buffer available.
1911 (should (not (python-shell-get-process))))
1912 (ignore-errors (kill-buffer global-shell-buffer
))
1913 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
1915 (ert-deftest python-shell-get-or-create-process-1
()
1916 "Check shell dedicated process creation."
1917 (skip-unless (executable-find python-tests-shell-interpreter
))
1918 (python-tests-with-temp-file
1920 (let* ((python-shell-interpreter
1921 (executable-find python-tests-shell-interpreter
))
1923 (dedicated-process-name (python-shell-get-process-name t
))
1925 (python-shell-get-or-create-process python-shell-interpreter t
))
1926 (dedicated-shell-buffer (process-buffer dedicated-process
)))
1929 (set-process-query-on-exit-flag dedicated-process nil
)
1930 ;; should be dedicated.
1931 (should (equal (process-name dedicated-process
)
1932 dedicated-process-name
))
1933 (kill-buffer dedicated-shell-buffer
)
1934 ;; Check there are no processes for current buffer.
1935 (should (not (python-shell-get-process))))
1936 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
1938 (ert-deftest python-shell-get-or-create-process-2
()
1939 "Check shell global process creation."
1940 (skip-unless (executable-find python-tests-shell-interpreter
))
1941 (python-tests-with-temp-file
1943 (let* ((python-shell-interpreter
1944 (executable-find python-tests-shell-interpreter
))
1946 (process-name (python-shell-get-process-name nil
))
1948 (python-shell-get-or-create-process python-shell-interpreter
))
1949 (shell-buffer (process-buffer process
)))
1952 (set-process-query-on-exit-flag process nil
)
1953 ;; should be global.
1954 (should (equal (process-name process
) process-name
))
1955 (kill-buffer shell-buffer
)
1956 ;; Check there are no processes for current buffer.
1957 (should (not (python-shell-get-process))))
1958 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
1960 (ert-deftest python-shell-get-or-create-process-3
()
1961 "Check shell dedicated/global process preference."
1962 (skip-unless (executable-find python-tests-shell-interpreter
))
1963 (python-tests-with-temp-file
1965 (let* ((python-shell-interpreter
1966 (executable-find python-tests-shell-interpreter
))
1968 (dedicated-process-name (python-shell-get-process-name t
))
1970 (dedicated-process))
1973 ;; Create global process
1974 (run-python python-shell-interpreter nil
)
1975 (setq global-process
(get-buffer-process "*Python*"))
1976 (should global-process
)
1977 (set-process-query-on-exit-flag global-process nil
)
1978 ;; Create dedicated process
1979 (run-python python-shell-interpreter t
)
1980 (setq dedicated-process
(get-process dedicated-process-name
))
1981 (should dedicated-process
)
1982 (set-process-query-on-exit-flag dedicated-process nil
)
1983 ;; Prefer dedicated.
1984 (should (equal (python-shell-get-or-create-process)
1986 ;; Kill the dedicated so the global takes over.
1987 (kill-buffer (process-buffer dedicated-process
))
1989 (should (equal (python-shell-get-or-create-process) global-process
))
1991 (kill-buffer (process-buffer global-process
))
1992 ;; Check there are no processes for current buffer.
1993 (should (not (python-shell-get-process))))
1994 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
1996 (ert-deftest python-shell-internal-get-or-create-process-1
()
1997 "Check internal shell process creation fallback."
1998 (skip-unless (executable-find python-tests-shell-interpreter
))
1999 (python-tests-with-temp-file
2001 (should (not (process-live-p (python-shell-internal-get-process-name))))
2002 (let* ((python-shell-interpreter
2003 (executable-find python-tests-shell-interpreter
))
2004 (internal-process-name (python-shell-internal-get-process-name))
2005 (internal-process (python-shell-internal-get-or-create-process))
2006 (internal-shell-buffer (process-buffer internal-process
)))
2009 (set-process-query-on-exit-flag internal-process nil
)
2010 (should (equal (process-name internal-process
)
2011 internal-process-name
))
2012 (should (equal internal-process
2013 (python-shell-internal-get-or-create-process)))
2014 ;; Assert the internal process is not a user process
2015 (should (not (python-shell-get-process)))
2016 (kill-buffer internal-shell-buffer
))
2017 (ignore-errors (kill-buffer internal-shell-buffer
))))))
2019 (ert-deftest python-shell-prompt-detect-1
()
2020 "Check prompt autodetection."
2021 (skip-unless (executable-find python-tests-shell-interpreter
))
2022 (let ((process-environment process-environment
))
2023 ;; Ensure no startup file is enabled
2024 (setenv "PYTHONSTARTUP" "")
2025 (should python-shell-prompt-detect-enabled
)
2026 (should (equal (python-shell-prompt-detect) '(">>> " "... " "")))))
2028 (ert-deftest python-shell-prompt-detect-2
()
2029 "Check prompt autodetection with startup file. Bug#17370."
2030 (skip-unless (executable-find python-tests-shell-interpreter
))
2031 (let* ((process-environment process-environment
)
2032 (startup-code (concat "import sys\n"
2033 "sys.ps1 = 'py> '\n"
2034 "sys.ps2 = '..> '\n"
2035 "sys.ps3 = 'out '\n"))
2036 (startup-file (python-shell--save-temp-file startup-code
)))
2039 ;; Ensure startup file is enabled
2040 (setenv "PYTHONSTARTUP" startup-file
)
2041 (should python-shell-prompt-detect-enabled
)
2042 (should (equal (python-shell-prompt-detect) '("py> " "..> " "out "))))
2043 (ignore-errors (delete-file startup-file
)))))
2045 (ert-deftest python-shell-prompt-detect-3
()
2046 "Check prompts are not autodetected when feature is disabled."
2047 (skip-unless (executable-find python-tests-shell-interpreter
))
2048 (let ((process-environment process-environment
)
2049 (python-shell-prompt-detect-enabled nil
))
2050 ;; Ensure no startup file is enabled
2051 (should (not python-shell-prompt-detect-enabled
))
2052 (should (not (python-shell-prompt-detect)))))
2054 (ert-deftest python-shell-prompt-detect-4
()
2055 "Check warning is shown when detection fails."
2056 (skip-unless (executable-find python-tests-shell-interpreter
))
2057 (let* ((process-environment process-environment
)
2058 ;; Trigger failure by removing prompts in the startup file
2059 (startup-code (concat "import sys\n"
2063 (startup-file (python-shell--save-temp-file startup-code
)))
2066 (kill-buffer (get-buffer-create "*Warnings*"))
2067 (should (not (get-buffer "*Warnings*")))
2068 (setenv "PYTHONSTARTUP" startup-file
)
2069 (should python-shell-prompt-detect-failure-warning
)
2070 (should python-shell-prompt-detect-enabled
)
2071 (should (not (python-shell-prompt-detect)))
2072 (should (get-buffer "*Warnings*")))
2073 (ignore-errors (delete-file startup-file
)))))
2075 (ert-deftest python-shell-prompt-detect-5
()
2076 "Check disabled warnings are not shown when detection fails."
2077 (skip-unless (executable-find python-tests-shell-interpreter
))
2078 (let* ((process-environment process-environment
)
2079 (startup-code (concat "import sys\n"
2083 (startup-file (python-shell--save-temp-file startup-code
))
2084 (python-shell-prompt-detect-failure-warning nil
))
2087 (kill-buffer (get-buffer-create "*Warnings*"))
2088 (should (not (get-buffer "*Warnings*")))
2089 (setenv "PYTHONSTARTUP" startup-file
)
2090 (should (not python-shell-prompt-detect-failure-warning
))
2091 (should python-shell-prompt-detect-enabled
)
2092 (should (not (python-shell-prompt-detect)))
2093 (should (not (get-buffer "*Warnings*"))))
2094 (ignore-errors (delete-file startup-file
)))))
2096 (ert-deftest python-shell-prompt-detect-6
()
2097 "Warnings are not shown when detection is disabled."
2098 (skip-unless (executable-find python-tests-shell-interpreter
))
2099 (let* ((process-environment process-environment
)
2100 (startup-code (concat "import sys\n"
2104 (startup-file (python-shell--save-temp-file startup-code
))
2105 (python-shell-prompt-detect-failure-warning t
)
2106 (python-shell-prompt-detect-enabled nil
))
2109 (kill-buffer (get-buffer-create "*Warnings*"))
2110 (should (not (get-buffer "*Warnings*")))
2111 (setenv "PYTHONSTARTUP" startup-file
)
2112 (should python-shell-prompt-detect-failure-warning
)
2113 (should (not python-shell-prompt-detect-enabled
))
2114 (should (not (python-shell-prompt-detect)))
2115 (should (not (get-buffer "*Warnings*"))))
2116 (ignore-errors (delete-file startup-file
)))))
2118 (ert-deftest python-shell-prompt-validate-regexps-1
()
2119 "Check `python-shell-prompt-input-regexps' are validated."
2120 (let* ((python-shell-prompt-input-regexps '("\\("))
2121 (error-data (should-error (python-shell-prompt-validate-regexps)
2122 :type
'user-error
)))
2124 (string= (cadr error-data
)
2125 "Invalid regexp \\( in `python-shell-prompt-input-regexps'"))))
2127 (ert-deftest python-shell-prompt-validate-regexps-2
()
2128 "Check `python-shell-prompt-output-regexps' are validated."
2129 (let* ((python-shell-prompt-output-regexps '("\\("))
2130 (error-data (should-error (python-shell-prompt-validate-regexps)
2131 :type
'user-error
)))
2133 (string= (cadr error-data
)
2134 "Invalid regexp \\( in `python-shell-prompt-output-regexps'"))))
2136 (ert-deftest python-shell-prompt-validate-regexps-3
()
2137 "Check `python-shell-prompt-regexp' is validated."
2138 (let* ((python-shell-prompt-regexp "\\(")
2139 (error-data (should-error (python-shell-prompt-validate-regexps)
2140 :type
'user-error
)))
2142 (string= (cadr error-data
)
2143 "Invalid regexp \\( in `python-shell-prompt-regexp'"))))
2145 (ert-deftest python-shell-prompt-validate-regexps-4
()
2146 "Check `python-shell-prompt-block-regexp' is validated."
2147 (let* ((python-shell-prompt-block-regexp "\\(")
2148 (error-data (should-error (python-shell-prompt-validate-regexps)
2149 :type
'user-error
)))
2151 (string= (cadr error-data
)
2152 "Invalid regexp \\( in `python-shell-prompt-block-regexp'"))))
2154 (ert-deftest python-shell-prompt-validate-regexps-5
()
2155 "Check `python-shell-prompt-pdb-regexp' is validated."
2156 (let* ((python-shell-prompt-pdb-regexp "\\(")
2157 (error-data (should-error (python-shell-prompt-validate-regexps)
2158 :type
'user-error
)))
2160 (string= (cadr error-data
)
2161 "Invalid regexp \\( in `python-shell-prompt-pdb-regexp'"))))
2163 (ert-deftest python-shell-prompt-validate-regexps-6
()
2164 "Check `python-shell-prompt-output-regexp' is validated."
2165 (let* ((python-shell-prompt-output-regexp "\\(")
2166 (error-data (should-error (python-shell-prompt-validate-regexps)
2167 :type
'user-error
)))
2169 (string= (cadr error-data
)
2170 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2172 (ert-deftest python-shell-prompt-validate-regexps-7
()
2173 "Check default regexps are valid."
2174 ;; should not signal error
2175 (python-shell-prompt-validate-regexps))
2177 (ert-deftest python-shell-prompt-set-calculated-regexps-1
()
2178 "Check regexps are validated."
2179 (let* ((python-shell-prompt-output-regexp '("\\("))
2180 (python-shell--prompt-calculated-input-regexp nil
)
2181 (python-shell--prompt-calculated-output-regexp nil
)
2182 (python-shell-prompt-detect-enabled nil
)
2183 (error-data (should-error (python-shell-prompt-set-calculated-regexps)
2184 :type
'user-error
)))
2186 (string= (cadr error-data
)
2187 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2189 (ert-deftest python-shell-prompt-set-calculated-regexps-2
()
2190 "Check `python-shell-prompt-input-regexps' are set."
2191 (let* ((python-shell-prompt-input-regexps '("my" "prompt"))
2192 (python-shell-prompt-output-regexps '(""))
2193 (python-shell-prompt-regexp "")
2194 (python-shell-prompt-block-regexp "")
2195 (python-shell-prompt-pdb-regexp "")
2196 (python-shell-prompt-output-regexp "")
2197 (python-shell--prompt-calculated-input-regexp nil
)
2198 (python-shell--prompt-calculated-output-regexp nil
)
2199 (python-shell-prompt-detect-enabled nil
))
2200 (python-shell-prompt-set-calculated-regexps)
2201 (should (string= python-shell--prompt-calculated-input-regexp
2202 "^\\(prompt\\|my\\|\\)"))))
2204 (ert-deftest python-shell-prompt-set-calculated-regexps-3
()
2205 "Check `python-shell-prompt-output-regexps' are set."
2206 (let* ((python-shell-prompt-input-regexps '(""))
2207 (python-shell-prompt-output-regexps '("my" "prompt"))
2208 (python-shell-prompt-regexp "")
2209 (python-shell-prompt-block-regexp "")
2210 (python-shell-prompt-pdb-regexp "")
2211 (python-shell-prompt-output-regexp "")
2212 (python-shell--prompt-calculated-input-regexp nil
)
2213 (python-shell--prompt-calculated-output-regexp nil
)
2214 (python-shell-prompt-detect-enabled nil
))
2215 (python-shell-prompt-set-calculated-regexps)
2216 (should (string= python-shell--prompt-calculated-output-regexp
2217 "^\\(prompt\\|my\\|\\)"))))
2219 (ert-deftest python-shell-prompt-set-calculated-regexps-4
()
2220 "Check user defined prompts are set."
2221 (let* ((python-shell-prompt-input-regexps '(""))
2222 (python-shell-prompt-output-regexps '(""))
2223 (python-shell-prompt-regexp "prompt")
2224 (python-shell-prompt-block-regexp "block")
2225 (python-shell-prompt-pdb-regexp "pdb")
2226 (python-shell-prompt-output-regexp "output")
2227 (python-shell--prompt-calculated-input-regexp nil
)
2228 (python-shell--prompt-calculated-output-regexp nil
)
2229 (python-shell-prompt-detect-enabled nil
))
2230 (python-shell-prompt-set-calculated-regexps)
2231 (should (string= python-shell--prompt-calculated-input-regexp
2232 "^\\(prompt\\|block\\|pdb\\|\\)"))
2233 (should (string= python-shell--prompt-calculated-output-regexp
2234 "^\\(output\\|\\)"))))
2236 (ert-deftest python-shell-prompt-set-calculated-regexps-5
()
2237 "Check order of regexps (larger first)."
2238 (let* ((python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2239 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2240 (python-shell-prompt-regexp "in")
2241 (python-shell-prompt-block-regexp "block")
2242 (python-shell-prompt-pdb-regexp "pdf")
2243 (python-shell-prompt-output-regexp "output")
2244 (python-shell--prompt-calculated-input-regexp nil
)
2245 (python-shell--prompt-calculated-output-regexp nil
)
2246 (python-shell-prompt-detect-enabled nil
))
2247 (python-shell-prompt-set-calculated-regexps)
2248 (should (string= python-shell--prompt-calculated-input-regexp
2249 "^\\(extralargeinputprompt\\|block\\|pdf\\|sml\\|in\\)"))
2250 (should (string= python-shell--prompt-calculated-output-regexp
2251 "^\\(extralargeoutputprompt\\|output\\|sml\\)"))))
2253 (ert-deftest python-shell-prompt-set-calculated-regexps-6
()
2254 "Check detected prompts are included `regexp-quote'd."
2255 (skip-unless (executable-find python-tests-shell-interpreter
))
2256 (let* ((python-shell-prompt-input-regexps '(""))
2257 (python-shell-prompt-output-regexps '(""))
2258 (python-shell-prompt-regexp "")
2259 (python-shell-prompt-block-regexp "")
2260 (python-shell-prompt-pdb-regexp "")
2261 (python-shell-prompt-output-regexp "")
2262 (python-shell--prompt-calculated-input-regexp nil
)
2263 (python-shell--prompt-calculated-output-regexp nil
)
2264 (python-shell-prompt-detect-enabled t
)
2265 (process-environment process-environment
)
2266 (startup-code (concat "import sys\n"
2267 "sys.ps1 = 'p.> '\n"
2268 "sys.ps2 = '..> '\n"
2269 "sys.ps3 = 'o.t '\n"))
2270 (startup-file (python-shell--save-temp-file startup-code
)))
2273 (setenv "PYTHONSTARTUP" startup-file
)
2274 (python-shell-prompt-set-calculated-regexps)
2275 (should (string= python-shell--prompt-calculated-input-regexp
2276 "^\\(\\.\\.> \\|p\\.> \\|\\)"))
2277 (should (string= python-shell--prompt-calculated-output-regexp
2278 "^\\(o\\.t \\|\\)")))
2279 (ignore-errors (delete-file startup-file
)))))
2282 ;;; Shell completion
2285 ;;; PDB Track integration
2288 ;;; Symbol completion
2308 (ert-deftest python-imenu-create-index-1
()
2309 (python-tests-with-temp-buffer
2311 class Foo(models.Model):
2315 class Bar(models.Model):
2319 def decorator(arg1, arg2, arg3):
2320 '''print decorated function call data to stdout.
2324 @decorator('arg1', 'arg2')
2325 def func(a, b, c=True):
2331 def wrapped_f(*args):
2333 print ('Decorator arguments:', arg1, arg2, arg3)
2335 print ('called f(*args)')
2353 (goto-char (point-max))
2356 (cons "Foo (class)" (copy-marker 2))
2357 (cons "Bar (class)" (copy-marker 38))
2360 (cons "*function definition*" (copy-marker 74))
2363 (cons "*function definition*" (copy-marker 254))
2364 (cons "wrapped_f (def)" (copy-marker 294))))
2367 (cons "*class definition*" (copy-marker 519))
2368 (cons "a (def)" (copy-marker 539))
2369 (cons "b (def)" (copy-marker 570))
2372 (cons "*class definition*" (copy-marker 601))
2373 (cons "c (def)" (copy-marker 626)))))
2374 (python-imenu-create-index)))))
2376 (ert-deftest python-imenu-create-index-2
()
2377 (python-tests-with-temp-buffer
2387 (goto-char (point-max))
2392 (cons "*class definition*" (copy-marker 2))
2395 (cons "*function definition*" (copy-marker 21))
2396 (cons "foo1 (def)" (copy-marker 40)))
2397 (cons "foobar (def)" (copy-marker 78))))
2398 (python-imenu-create-index)))))
2400 (ert-deftest python-imenu-create-index-3
()
2401 (python-tests-with-temp-buffer
2410 (goto-char (point-max))
2415 (cons "*class definition*" (copy-marker 2))
2418 (cons "*function definition*" (copy-marker 21))
2419 (cons "foo1 (def)" (copy-marker 40))
2420 (cons "foo2 (def)" (copy-marker 77)))))
2421 (python-imenu-create-index)))))
2423 (ert-deftest python-imenu-create-index-4
()
2424 (python-tests-with-temp-buffer
2437 (goto-char (point-max))
2442 (cons "*class definition*" (copy-marker 2))
2445 (cons "*class definition*" (copy-marker 21))
2446 (cons "__init__ (def)" (copy-marker 44))
2447 (cons "__str__ (def)" (copy-marker 90)))
2448 (cons "__init__ (def)" (copy-marker 135))))
2449 (python-imenu-create-index)))))
2451 (ert-deftest python-imenu-create-flat-index-1
()
2452 (python-tests-with-temp-buffer
2454 class Foo(models.Model):
2458 class Bar(models.Model):
2462 def decorator(arg1, arg2, arg3):
2463 '''print decorated function call data to stdout.
2467 @decorator('arg1', 'arg2')
2468 def func(a, b, c=True):
2474 def wrapped_f(*args):
2476 print ('Decorator arguments:', arg1, arg2, arg3)
2478 print ('called f(*args)')
2496 (goto-char (point-max))
2498 (list (cons "Foo" (copy-marker 2))
2499 (cons "Bar" (copy-marker 38))
2500 (cons "decorator" (copy-marker 74))
2501 (cons "decorator.wrap" (copy-marker 254))
2502 (cons "decorator.wrap.wrapped_f" (copy-marker 294))
2503 (cons "Baz" (copy-marker 519))
2504 (cons "Baz.a" (copy-marker 539))
2505 (cons "Baz.b" (copy-marker 570))
2506 (cons "Baz.Frob" (copy-marker 601))
2507 (cons "Baz.Frob.c" (copy-marker 626)))
2508 (python-imenu-create-flat-index)))))
2510 (ert-deftest python-imenu-create-flat-index-2
()
2511 (python-tests-with-temp-buffer
2524 (goto-char (point-max))
2527 (cons "Foo" (copy-marker 2))
2528 (cons "Foo.Bar" (copy-marker 21))
2529 (cons "Foo.Bar.__init__" (copy-marker 44))
2530 (cons "Foo.Bar.__str__" (copy-marker 90))
2531 (cons "Foo.__init__" (copy-marker 135)))
2532 (python-imenu-create-flat-index)))))
2537 (ert-deftest python-info-current-defun-1
()
2538 (python-tests-with-temp-buffer
2543 (should (string= "foo" (python-info-current-defun)))
2544 (should (string= "def foo" (python-info-current-defun t)))
2546 (should (not (python-info-current-defun)))
2547 (indent-for-tab-command)
2548 (should (string= "foo" (python-info-current-defun)))
2549 (should (string= "def foo" (python-info-current-defun t)))))
2551 (ert-deftest python-info-current-defun-2
()
2552 (python-tests-with-temp-buffer
2558 return [i for i in range(3)]
2572 (should (string= "C" (python-info-current-defun)))
2573 (should (string= "class C" (python-info-current-defun t)))
2574 (python-tests-look-at "return [i for ")
2575 (should (string= "C.m" (python-info-current-defun)))
2576 (should (string= "def C.m" (python-info-current-defun t)))
2577 (python-tests-look-at "def b():")
2578 (should (string= "C.m.b" (python-info-current-defun)))
2579 (should (string= "def C.m.b" (python-info-current-defun t)))
2581 (indent-for-tab-command)
2582 (python-indent-dedent-line-backspace 1)
2583 (should (string= "C.m" (python-info-current-defun)))
2584 (should (string= "def C.m" (python-info-current-defun t)))
2585 (python-tests-look-at "def c(self):")
2587 (indent-for-tab-command)
2588 (should (string= "C.m.a" (python-info-current-defun)))
2589 (should (string= "def C.m.a" (python-info-current-defun t)))
2590 (python-indent-dedent-line-backspace 1)
2591 (should (string= "C.m" (python-info-current-defun)))
2592 (should (string= "def C.m" (python-info-current-defun t)))
2593 (python-indent-dedent-line-backspace 1)
2594 (should (string= "C" (python-info-current-defun)))
2595 (should (string= "class C" (python-info-current-defun t)))
2596 (python-tests-look-at "def c(self):")
2597 (should (string= "C.c" (python-info-current-defun)))
2598 (should (string= "def C.c" (python-info-current-defun t)))
2599 (python-tests-look-at "do_c()")
2600 (should (string= "C.c" (python-info-current-defun)))
2601 (should (string= "def C.c" (python-info-current-defun t)))))
2603 (ert-deftest python-info-current-defun-3
()
2604 (python-tests-with-temp-buffer
2606 def decoratorFunctionWithArguments(arg1, arg2, arg3):
2607 '''print decorated function call data to stdout.
2611 @decoratorFunctionWithArguments('arg1', 'arg2')
2612 def func(a, b, c=True):
2617 print 'Inside wwrap()'
2618 def wrapped_f(*args):
2619 print 'Inside wrapped_f()'
2620 print 'Decorator arguments:', arg1, arg2, arg3
2622 print 'After f(*args)'
2626 (python-tests-look-at "def wwrap(f):")
2628 (should (not (python-info-current-defun)))
2629 (indent-for-tab-command 1)
2630 (should (string= (python-info-current-defun)
2631 "decoratorFunctionWithArguments"))
2632 (should (string= (python-info-current-defun t)
2633 "def decoratorFunctionWithArguments"))
2634 (python-tests-look-at "def wrapped_f(*args):")
2635 (should (string= (python-info-current-defun)
2636 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
2637 (should (string= (python-info-current-defun t)
2638 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
2639 (python-tests-look-at "return wrapped_f")
2640 (should (string= (python-info-current-defun)
2641 "decoratorFunctionWithArguments.wwrap"))
2642 (should (string= (python-info-current-defun t)
2643 "def decoratorFunctionWithArguments.wwrap"))
2645 (python-tests-look-at "return wwrap")
2646 (should (string= (python-info-current-defun)
2647 "decoratorFunctionWithArguments"))
2648 (should (string= (python-info-current-defun t)
2649 "def decoratorFunctionWithArguments"))))
2651 (ert-deftest python-info-current-symbol-1
()
2652 (python-tests-with-temp-buffer
2662 (python-tests-look-at "self.c()")
2663 (should (string= "self.c" (python-info-current-symbol)))
2664 (should (string= "C.c" (python-info-current-symbol t
)))))
2666 (ert-deftest python-info-current-symbol-2
()
2667 (python-tests-with-temp-buffer
2679 (python-tests-look-at "self.c()")
2680 (should (string= "self.c" (python-info-current-symbol)))
2681 (should (string= "C.M.c" (python-info-current-symbol t
)))))
2683 (ert-deftest python-info-current-symbol-3
()
2684 "Keywords should not be considered symbols."
2685 :expected-result
:failed
2686 (python-tests-with-temp-buffer
2691 ;; FIXME: keywords are not symbols.
2692 (python-tests-look-at "class C")
2693 (should (not (python-info-current-symbol)))
2694 (should (not (python-info-current-symbol t
)))
2695 (python-tests-look-at "C(object)")
2696 (should (string= "C" (python-info-current-symbol)))
2697 (should (string= "class C" (python-info-current-symbol t
)))))
2699 (ert-deftest python-info-statement-starts-block-p-1
()
2700 (python-tests-with-temp-buffer
2702 def long_function_name(
2703 var_one, var_two, var_three,
2707 (python-tests-look-at "def long_function_name")
2708 (should (python-info-statement-starts-block-p))
2709 (python-tests-look-at "print (var_one)")
2710 (python-util-forward-comment -
1)
2711 (should (python-info-statement-starts-block-p))))
2713 (ert-deftest python-info-statement-starts-block-p-2
()
2714 (python-tests-with-temp-buffer
2716 if width == 0 and height == 0 and \\\\
2717 color == 'red' and emphasis == 'strong' or \\\\
2719 raise ValueError('sorry, you lose')
2721 (python-tests-look-at "if width == 0 and")
2722 (should (python-info-statement-starts-block-p))
2723 (python-tests-look-at "raise ValueError(")
2724 (python-util-forward-comment -
1)
2725 (should (python-info-statement-starts-block-p))))
2727 (ert-deftest python-info-statement-ends-block-p-1
()
2728 (python-tests-with-temp-buffer
2730 def long_function_name(
2731 var_one, var_two, var_three,
2735 (python-tests-look-at "print (var_one)")
2736 (should (python-info-statement-ends-block-p))))
2738 (ert-deftest python-info-statement-ends-block-p-2
()
2739 (python-tests-with-temp-buffer
2741 if width == 0 and height == 0 and \\\\
2742 color == 'red' and emphasis == 'strong' or \\\\
2749 (python-tests-look-at "raise ValueError(")
2750 (should (python-info-statement-ends-block-p))))
2752 (ert-deftest python-info-beginning-of-statement-p-1
()
2753 (python-tests-with-temp-buffer
2755 def long_function_name(
2756 var_one, var_two, var_three,
2760 (python-tests-look-at "def long_function_name")
2761 (should (python-info-beginning-of-statement-p))
2763 (should (not (python-info-beginning-of-statement-p)))
2764 (python-tests-look-at "print (var_one)")
2765 (should (python-info-beginning-of-statement-p))
2766 (goto-char (line-beginning-position))
2767 (should (not (python-info-beginning-of-statement-p)))))
2769 (ert-deftest python-info-beginning-of-statement-p-2
()
2770 (python-tests-with-temp-buffer
2772 if width == 0 and height == 0 and \\\\
2773 color == 'red' and emphasis == 'strong' or \\\\
2780 (python-tests-look-at "if width == 0 and")
2781 (should (python-info-beginning-of-statement-p))
2783 (should (not (python-info-beginning-of-statement-p)))
2784 (python-tests-look-at "raise ValueError(")
2785 (should (python-info-beginning-of-statement-p))
2786 (goto-char (line-beginning-position))
2787 (should (not (python-info-beginning-of-statement-p)))))
2789 (ert-deftest python-info-end-of-statement-p-1
()
2790 (python-tests-with-temp-buffer
2792 def long_function_name(
2793 var_one, var_two, var_three,
2797 (python-tests-look-at "def long_function_name")
2798 (should (not (python-info-end-of-statement-p)))
2800 (should (not (python-info-end-of-statement-p)))
2801 (python-tests-look-at "print (var_one)")
2802 (python-util-forward-comment -
1)
2803 (should (python-info-end-of-statement-p))
2804 (python-tests-look-at "print (var_one)")
2805 (should (not (python-info-end-of-statement-p)))
2807 (should (python-info-end-of-statement-p))))
2809 (ert-deftest python-info-end-of-statement-p-2
()
2810 (python-tests-with-temp-buffer
2812 if width == 0 and height == 0 and \\\\
2813 color == 'red' and emphasis == 'strong' or \\\\
2820 (python-tests-look-at "if width == 0 and")
2821 (should (not (python-info-end-of-statement-p)))
2823 (should (not (python-info-end-of-statement-p)))
2824 (python-tests-look-at "raise ValueError(")
2825 (python-util-forward-comment -
1)
2826 (should (python-info-end-of-statement-p))
2827 (python-tests-look-at "raise ValueError(")
2828 (should (not (python-info-end-of-statement-p)))
2830 (should (not (python-info-end-of-statement-p)))
2831 (goto-char (point-max))
2832 (python-util-forward-comment -
1)
2833 (should (python-info-end-of-statement-p))))
2835 (ert-deftest python-info-beginning-of-block-p-1
()
2836 (python-tests-with-temp-buffer
2838 def long_function_name(
2839 var_one, var_two, var_three,
2843 (python-tests-look-at "def long_function_name")
2844 (should (python-info-beginning-of-block-p))
2845 (python-tests-look-at "var_one, var_two, var_three,")
2846 (should (not (python-info-beginning-of-block-p)))
2847 (python-tests-look-at "print (var_one)")
2848 (should (not (python-info-beginning-of-block-p)))))
2850 (ert-deftest python-info-beginning-of-block-p-2
()
2851 (python-tests-with-temp-buffer
2853 if width == 0 and height == 0 and \\\\
2854 color == 'red' and emphasis == 'strong' or \\\\
2861 (python-tests-look-at "if width == 0 and")
2862 (should (python-info-beginning-of-block-p))
2863 (python-tests-look-at "color == 'red' and emphasis")
2864 (should (not (python-info-beginning-of-block-p)))
2865 (python-tests-look-at "raise ValueError(")
2866 (should (not (python-info-beginning-of-block-p)))))
2868 (ert-deftest python-info-end-of-block-p-1
()
2869 (python-tests-with-temp-buffer
2871 def long_function_name(
2872 var_one, var_two, var_three,
2876 (python-tests-look-at "def long_function_name")
2877 (should (not (python-info-end-of-block-p)))
2878 (python-tests-look-at "var_one, var_two, var_three,")
2879 (should (not (python-info-end-of-block-p)))
2880 (python-tests-look-at "var_four):")
2882 (should (not (python-info-end-of-block-p)))
2883 (python-tests-look-at "print (var_one)")
2884 (should (not (python-info-end-of-block-p)))
2886 (should (python-info-end-of-block-p))))
2888 (ert-deftest python-info-end-of-block-p-2
()
2889 (python-tests-with-temp-buffer
2891 if width == 0 and height == 0 and \\\\
2892 color == 'red' and emphasis == 'strong' or \\\\
2899 (python-tests-look-at "if width == 0 and")
2900 (should (not (python-info-end-of-block-p)))
2901 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2902 (should (not (python-info-end-of-block-p)))
2903 (python-tests-look-at "highlight > 100:")
2905 (should (not (python-info-end-of-block-p)))
2906 (python-tests-look-at "raise ValueError(")
2907 (should (not (python-info-end-of-block-p)))
2909 (should (not (python-info-end-of-block-p)))
2910 (goto-char (point-max))
2911 (python-util-forward-comment -
1)
2912 (should (python-info-end-of-block-p))))
2914 (ert-deftest python-info-dedenter-opening-block-position-1
()
2915 (python-tests-with-temp-buffer
2917 if request.user.is_authenticated():
2919 profile = request.user.get_profile()
2920 except Profile.DoesNotExist:
2921 profile = Profile.objects.create(user=request.user)
2924 profile.recalculate_stats()
2926 profile.clear_stats()
2931 (python-tests-look-at "try:")
2932 (should (not (python-info-dedenter-opening-block-position)))
2933 (python-tests-look-at "except Profile.DoesNotExist:")
2934 (should (= (python-tests-look-at "try:" -
1 t
)
2935 (python-info-dedenter-opening-block-position)))
2936 (python-tests-look-at "else:")
2937 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -
1 t
)
2938 (python-info-dedenter-opening-block-position)))
2939 (python-tests-look-at "if profile.stats:")
2940 (should (not (python-info-dedenter-opening-block-position)))
2941 (python-tests-look-at "else:")
2942 (should (= (python-tests-look-at "if profile.stats:" -
1 t
)
2943 (python-info-dedenter-opening-block-position)))
2944 (python-tests-look-at "finally:")
2945 (should (= (python-tests-look-at "else:" -
2 t
)
2946 (python-info-dedenter-opening-block-position)))))
2948 (ert-deftest python-info-dedenter-opening-block-position-2
()
2949 (python-tests-with-temp-buffer
2951 if request.user.is_authenticated():
2952 profile = Profile.objects.get_or_create(user=request.user)
2954 profile.recalculate_stats()
2961 (python-tests-look-at "'else': 'do it'")
2962 (should (not (python-info-dedenter-opening-block-position)))
2963 (python-tests-look-at "'else'")
2964 (should (not (python-info-dedenter-opening-block-position)))))
2966 (ert-deftest python-info-dedenter-opening-block-position-3
()
2967 (python-tests-with-temp-buffer
2973 msg = 'Error saving to disk'
2975 logger.exception(msg)
2978 logger.exception('Unhandled exception')
2983 (python-tests-look-at "try:")
2984 (should (not (python-info-dedenter-opening-block-position)))
2986 (python-tests-look-at "except IOError:")
2987 (should (= (python-tests-look-at "try:" -
1 t
)
2988 (python-info-dedenter-opening-block-position)))
2990 (python-tests-look-at "except Exception:")
2991 (should (= (python-tests-look-at "except IOError:" -
1 t
)
2992 (python-info-dedenter-opening-block-position)))
2994 (python-tests-look-at "if hide_details:")
2995 (should (not (python-info-dedenter-opening-block-position)))
2997 ;; check indentation modifies the detected opening block
2998 (python-tests-look-at "else")
2999 (should (= (python-tests-look-at "if hide_details:" -
1 t
)
3000 (python-info-dedenter-opening-block-position)))
3003 (should (= (python-tests-look-at "if hide_details:" -
1 t
)
3004 (python-info-dedenter-opening-block-position)))
3007 (should (= (python-tests-look-at "except Exception:" -
1 t
)
3008 (python-info-dedenter-opening-block-position)))
3011 (should (= (python-tests-look-at "if save:" -
1 t
)
3012 (python-info-dedenter-opening-block-position)))))
3014 (ert-deftest python-info-dedenter-opening-block-positions-1
()
3015 (python-tests-with-temp-buffer
3021 msg = 'Error saving to disk'
3023 logger.exception(msg)
3026 logger.exception('Unhandled exception')
3031 (python-tests-look-at "try:")
3032 (should (not (python-info-dedenter-opening-block-positions)))
3034 (python-tests-look-at "except IOError:")
3037 (python-tests-look-at "try:" -
1 t
))
3038 (python-info-dedenter-opening-block-positions)))
3040 (python-tests-look-at "except Exception:")
3043 (python-tests-look-at "except IOError:" -
1 t
))
3044 (python-info-dedenter-opening-block-positions)))
3046 (python-tests-look-at "if hide_details:")
3047 (should (not (python-info-dedenter-opening-block-positions)))
3049 ;; check indentation does not modify the detected opening blocks
3050 (python-tests-look-at "else")
3053 (python-tests-look-at "if hide_details:" -
1 t
)
3054 (python-tests-look-at "except Exception:" -
1 t
)
3055 (python-tests-look-at "if save:" -
1 t
))
3056 (python-info-dedenter-opening-block-positions)))
3061 (python-tests-look-at "if hide_details:" -
1 t
)
3062 (python-tests-look-at "except Exception:" -
1 t
)
3063 (python-tests-look-at "if save:" -
1 t
))
3064 (python-info-dedenter-opening-block-positions)))
3069 (python-tests-look-at "if hide_details:" -
1 t
)
3070 (python-tests-look-at "except Exception:" -
1 t
)
3071 (python-tests-look-at "if save:" -
1 t
))
3072 (python-info-dedenter-opening-block-positions)))
3077 (python-tests-look-at "if hide_details:" -
1 t
)
3078 (python-tests-look-at "except Exception:" -
1 t
)
3079 (python-tests-look-at "if save:" -
1 t
))
3080 (python-info-dedenter-opening-block-positions)))))
3082 (ert-deftest python-info-dedenter-opening-block-positions-2
()
3083 "Test detection of opening blocks for elif."
3084 (python-tests-with-temp-buffer
3093 (python-tests-look-at "elif var3:")
3096 (python-tests-look-at "if var2:" -
1 t
)
3097 (python-tests-look-at "if var:" -
1 t
))
3098 (python-info-dedenter-opening-block-positions)))
3100 (python-tests-look-at "elif\n")
3103 (python-tests-look-at "elif var3:" -
1 t
)
3104 (python-tests-look-at "if var:" -
1 t
))
3105 (python-info-dedenter-opening-block-positions)))))
3107 (ert-deftest python-info-dedenter-opening-block-positions-3
()
3108 "Test detection of opening blocks for else."
3109 (python-tests-with-temp-buffer
3131 (python-tests-look-at "else\n")
3134 (python-tests-look-at "elif var3:" -
1 t
)
3135 (python-tests-look-at "if var:" -
1 t
)
3136 (python-tests-look-at "except:" -
1 t
))
3137 (python-info-dedenter-opening-block-positions)))
3139 (python-tests-look-at "else\n")
3142 (python-tests-look-at "while var5:" -
1 t
)
3143 (python-tests-look-at "if var4:" -
1 t
))
3144 (python-info-dedenter-opening-block-positions)))
3146 (python-tests-look-at "else\n")
3149 (python-tests-look-at "if value > 0:" -
1 t
)
3150 (python-tests-look-at "for value in var6:" -
1 t
)
3151 (python-tests-look-at "if var4:" -
1 t
))
3152 (python-info-dedenter-opening-block-positions)))))
3154 (ert-deftest python-info-dedenter-opening-block-positions-4
()
3155 "Test detection of opening blocks for except."
3156 (python-tests-with-temp-buffer
3164 (python-tests-look-at "except ValueError:")
3166 (equal (list (python-tests-look-at "try:" -
1 t
))
3167 (python-info-dedenter-opening-block-positions)))
3169 (python-tests-look-at "except\n")
3171 (equal (list (python-tests-look-at "except ValueError:" -
1 t
))
3172 (python-info-dedenter-opening-block-positions)))))
3174 (ert-deftest python-info-dedenter-opening-block-positions-5
()
3175 "Test detection of opening blocks for finally."
3176 (python-tests-with-temp-buffer
3185 logger.exception('something went wrong')
3189 something_else_else()
3191 logger.exception('something else went wrong')
3196 (python-tests-look-at "finally\n")
3198 (equal (list (python-tests-look-at "try:" -
1 t
))
3199 (python-info-dedenter-opening-block-positions)))
3201 (python-tests-look-at "finally\n")
3203 (equal (list (python-tests-look-at "except:" -
1 t
))
3204 (python-info-dedenter-opening-block-positions)))
3206 (python-tests-look-at "finally\n")
3208 (equal (list (python-tests-look-at "else:" -
1 t
))
3209 (python-info-dedenter-opening-block-positions)))))
3211 (ert-deftest python-info-dedenter-opening-block-message-1
()
3212 "Test dedenters inside strings are ignored."
3213 (python-tests-with-temp-buffer
3218 logger.exception('something went wrong')
3221 (python-tests-look-at "except\n")
3222 (should (not (python-info-dedenter-opening-block-message)))))
3224 (ert-deftest python-info-dedenter-opening-block-message-2
()
3225 "Test except keyword."
3226 (python-tests-with-temp-buffer
3231 logger.exception('something went wrong')
3233 (python-tests-look-at "except:")
3236 (substring-no-properties
3237 (python-info-dedenter-opening-block-message))))
3241 (substring-no-properties
3242 (python-info-dedenter-opening-block-message))))))
3244 (ert-deftest python-info-dedenter-opening-block-message-3
()
3245 "Test else keyword."
3246 (python-tests-with-temp-buffer
3251 logger.exception('something went wrong')
3253 logger.debug('all good')
3255 (python-tests-look-at "else:")
3258 (substring-no-properties
3259 (python-info-dedenter-opening-block-message))))
3263 (substring-no-properties
3264 (python-info-dedenter-opening-block-message))))))
3266 (ert-deftest python-info-dedenter-opening-block-message-4
()
3267 "Test finally keyword."
3268 (python-tests-with-temp-buffer
3273 logger.exception('something went wrong')
3275 logger.debug('all good')
3279 (python-tests-look-at "finally:")
3282 (substring-no-properties
3283 (python-info-dedenter-opening-block-message))))
3287 (substring-no-properties
3288 (python-info-dedenter-opening-block-message))))))
3290 (ert-deftest python-info-dedenter-opening-block-message-5
()
3291 "Test elif keyword."
3292 (python-tests-with-temp-buffer
3298 (python-tests-look-at "elif b:")
3301 (substring-no-properties
3302 (python-info-dedenter-opening-block-message))))
3306 (substring-no-properties
3307 (python-info-dedenter-opening-block-message))))))
3310 (ert-deftest python-info-dedenter-statement-p-1
()
3311 "Test dedenters inside strings are ignored."
3312 (python-tests-with-temp-buffer
3317 logger.exception('something went wrong')
3320 (python-tests-look-at "except\n")
3321 (should (not (python-info-dedenter-statement-p)))))
3323 (ert-deftest python-info-dedenter-statement-p-2
()
3324 "Test except keyword."
3325 (python-tests-with-temp-buffer
3330 logger.exception('something went wrong')
3332 (python-tests-look-at "except:")
3333 (should (= (point) (python-info-dedenter-statement-p)))
3335 (should (= (save-excursion
3336 (back-to-indentation)
3338 (python-info-dedenter-statement-p)))))
3340 (ert-deftest python-info-dedenter-statement-p-3
()
3341 "Test else keyword."
3342 (python-tests-with-temp-buffer
3347 logger.exception('something went wrong')
3349 logger.debug('all good')
3351 (python-tests-look-at "else:")
3352 (should (= (point) (python-info-dedenter-statement-p)))
3354 (should (= (save-excursion
3355 (back-to-indentation)
3357 (python-info-dedenter-statement-p)))))
3359 (ert-deftest python-info-dedenter-statement-p-4
()
3360 "Test finally keyword."
3361 (python-tests-with-temp-buffer
3366 logger.exception('something went wrong')
3368 logger.debug('all good')
3372 (python-tests-look-at "finally:")
3373 (should (= (point) (python-info-dedenter-statement-p)))
3375 (should (= (save-excursion
3376 (back-to-indentation)
3378 (python-info-dedenter-statement-p)))))
3380 (ert-deftest python-info-dedenter-statement-p-5
()
3381 "Test elif keyword."
3382 (python-tests-with-temp-buffer
3388 (python-tests-look-at "elif b:")
3389 (should (= (point) (python-info-dedenter-statement-p)))
3391 (should (= (save-excursion
3392 (back-to-indentation)
3394 (python-info-dedenter-statement-p)))))
3396 (ert-deftest python-info-line-ends-backslash-p-1
()
3397 (python-tests-with-temp-buffer
3399 objects = Thing.objects.all() \\\\
3409 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
3410 (should (python-info-line-ends-backslash-p 3))
3411 (should (python-info-line-ends-backslash-p 4))
3412 (should (python-info-line-ends-backslash-p 5))
3413 (should (python-info-line-ends-backslash-p 6)) ; ) \...
3414 (should (python-info-line-ends-backslash-p 7))
3415 (should (python-info-line-ends-backslash-p 8))
3416 (should (python-info-line-ends-backslash-p 9))
3417 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
3419 (ert-deftest python-info-beginning-of-backslash-1
()
3420 (python-tests-with-temp-buffer
3422 objects = Thing.objects.all() \\\\
3433 (second (python-tests-look-at ".filter("))
3434 (third (python-tests-look-at ".aggregate(")))
3435 (should (= first
(python-info-beginning-of-backslash 2)))
3436 (should (= second
(python-info-beginning-of-backslash 3)))
3437 (should (= second
(python-info-beginning-of-backslash 4)))
3438 (should (= second
(python-info-beginning-of-backslash 5)))
3439 (should (= second
(python-info-beginning-of-backslash 6)))
3440 (should (= third
(python-info-beginning-of-backslash 7)))
3441 (should (= third
(python-info-beginning-of-backslash 8)))
3442 (should (= third
(python-info-beginning-of-backslash 9)))
3443 (should (not (python-info-beginning-of-backslash 10))))))
3445 (ert-deftest python-info-continuation-line-p-1
()
3446 (python-tests-with-temp-buffer
3448 if width == 0 and height == 0 and \\\\
3449 color == 'red' and emphasis == 'strong' or \\\\
3456 (python-tests-look-at "if width == 0 and height == 0 and")
3457 (should (not (python-info-continuation-line-p)))
3458 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3459 (should (python-info-continuation-line-p))
3460 (python-tests-look-at "highlight > 100:")
3461 (should (python-info-continuation-line-p))
3462 (python-tests-look-at "raise ValueError(")
3463 (should (not (python-info-continuation-line-p)))
3464 (python-tests-look-at "'sorry, you lose'")
3465 (should (python-info-continuation-line-p))
3467 (should (python-info-continuation-line-p))
3468 (python-tests-look-at ")")
3469 (should (python-info-continuation-line-p))
3471 (should (not (python-info-continuation-line-p)))))
3473 (ert-deftest python-info-block-continuation-line-p-1
()
3474 (python-tests-with-temp-buffer
3476 if width == 0 and height == 0 and \\\\
3477 color == 'red' and emphasis == 'strong' or \\\\
3484 (python-tests-look-at "if width == 0 and")
3485 (should (not (python-info-block-continuation-line-p)))
3486 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3487 (should (= (python-info-block-continuation-line-p)
3488 (python-tests-look-at "if width == 0 and" -
1 t
)))
3489 (python-tests-look-at "highlight > 100:")
3490 (should (not (python-info-block-continuation-line-p)))))
3492 (ert-deftest python-info-block-continuation-line-p-2
()
3493 (python-tests-with-temp-buffer
3500 (python-tests-look-at "def foo(a,")
3501 (should (not (python-info-block-continuation-line-p)))
3502 (python-tests-look-at "b,")
3503 (should (= (python-info-block-continuation-line-p)
3504 (python-tests-look-at "def foo(a," -
1 t
)))
3505 (python-tests-look-at "c):")
3506 (should (not (python-info-block-continuation-line-p)))))
3508 (ert-deftest python-info-assignment-continuation-line-p-1
()
3509 (python-tests-with-temp-buffer
3511 data = foo(), bar() \\\\
3515 (python-tests-look-at "data = foo(), bar()")
3516 (should (not (python-info-assignment-continuation-line-p)))
3517 (python-tests-look-at "baz(), 4")
3518 (should (= (python-info-assignment-continuation-line-p)
3519 (python-tests-look-at "foo()," -
1 t
)))
3520 (python-tests-look-at "5, 6")
3521 (should (not (python-info-assignment-continuation-line-p)))))
3523 (ert-deftest python-info-assignment-continuation-line-p-2
()
3524 (python-tests-with-temp-buffer
3526 data = (foo(), bar()
3530 (python-tests-look-at "data = (foo(), bar()")
3531 (should (not (python-info-assignment-continuation-line-p)))
3532 (python-tests-look-at "baz(), 4")
3533 (should (= (python-info-assignment-continuation-line-p)
3534 (python-tests-look-at "(foo()," -
1 t
)))
3535 (python-tests-look-at "5, 6)")
3536 (should (not (python-info-assignment-continuation-line-p)))))
3538 (ert-deftest python-info-looking-at-beginning-of-defun-1
()
3539 (python-tests-with-temp-buffer
3541 def decorat0r(deff):
3552 (python-tests-look-at "def decorat0r(deff):")
3553 (should (python-info-looking-at-beginning-of-defun))
3554 (python-tests-look-at "def foo(arg):")
3555 (should (not (python-info-looking-at-beginning-of-defun)))
3556 (python-tests-look-at "def wrap():")
3557 (should (python-info-looking-at-beginning-of-defun))
3558 (python-tests-look-at "deff()")
3559 (should (not (python-info-looking-at-beginning-of-defun)))))
3561 (ert-deftest python-info-current-line-comment-p-1
()
3562 (python-tests-with-temp-buffer
3565 foo = True # another comment
3569 print ('bar') # print bar
3571 (python-tests-look-at "# this is a comment")
3572 (should (python-info-current-line-comment-p))
3573 (python-tests-look-at "foo = True # another comment")
3574 (should (not (python-info-current-line-comment-p)))
3575 (python-tests-look-at "'#this is a string'")
3576 (should (not (python-info-current-line-comment-p)))
3577 (python-tests-look-at "# more comments")
3578 (should (python-info-current-line-comment-p))
3579 (python-tests-look-at "print ('bar') # print bar")
3580 (should (not (python-info-current-line-comment-p)))))
3582 (ert-deftest python-info-current-line-empty-p
()
3583 (python-tests-with-temp-buffer
3587 foo = True # another comment
3589 (should (python-info-current-line-empty-p))
3590 (python-tests-look-at "# this is a comment")
3591 (should (not (python-info-current-line-empty-p)))
3593 (should (python-info-current-line-empty-p))))
3596 ;;; Utility functions
3598 (ert-deftest python-util-goto-line-1
()
3599 (python-tests-with-temp-buffer
3604 pass" (make-string 20 ?
\n))
3605 (python-util-goto-line 10)
3606 (should (= (line-number-at-pos) 10))
3607 (python-util-goto-line 20)
3608 (should (= (line-number-at-pos) 20))))
3610 (ert-deftest python-util-clone-local-variables-1
()
3611 (let ((buffer (generate-new-buffer
3612 "python-util-clone-local-variables-1"))
3614 '((python-fill-docstring-style . django
)
3615 (python-shell-interpreter .
"python")
3616 (python-shell-interpreter-args .
"manage.py shell")
3617 (python-shell-prompt-regexp .
"In \\[[0-9]+\\]: ")
3618 (python-shell-prompt-output-regexp .
"Out\\[[0-9]+\\]: ")
3619 (python-shell-extra-pythonpaths "/home/user/pylib/")
3620 (python-shell-completion-setup-code
3621 .
"from IPython.core.completerlib import module_completion")
3622 (python-shell-completion-string-code
3623 .
"';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
3624 (python-shell-virtualenv-path
3625 .
"/home/user/.virtualenvs/project"))))
3626 (with-current-buffer buffer
3627 (kill-all-local-variables)
3628 (dolist (ccons varcons
)
3629 (set (make-local-variable (car ccons
)) (cdr ccons
))))
3630 (python-tests-with-temp-buffer
3632 (python-util-clone-local-variables buffer
)
3633 (dolist (ccons varcons
)
3635 (equal (symbol-value (car ccons
)) (cdr ccons
)))))
3636 (kill-buffer buffer
)))
3638 (ert-deftest python-util-strip-string-1
()
3639 (should (string= (python-util-strip-string "\t\r\n str") "str"))
3640 (should (string= (python-util-strip-string "str \n\r") "str"))
3641 (should (string= (python-util-strip-string "\t\r\n str \n\r ") "str"))
3643 (string= (python-util-strip-string "\n str \nin \tg \n\r") "str \nin \tg"))
3644 (should (string= (python-util-strip-string "\n \t \n\r ") ""))
3645 (should (string= (python-util-strip-string "") "")))
3647 (ert-deftest python-util-forward-comment-1
()
3648 (python-tests-with-temp-buffer
3652 # bad indented comment
3653 # more comments" (make-string 9999 ?
\n))
3654 (python-util-forward-comment 1)
3655 (should (= (point) (point-max)))
3656 (python-util-forward-comment -
1)
3657 (should (= (point) (point-min)))))
3659 (ert-deftest python-util-valid-regexp-p-1
()
3660 (should (python-util-valid-regexp-p ""))
3661 (should (python-util-valid-regexp-p python-shell-prompt-regexp
))
3662 (should (not (python-util-valid-regexp-p "\\("))))
3667 (ert-deftest python-parens-electric-indent-1
()
3669 (let ((eim electric-indent-mode
))
3672 (python-tests-with-temp-buffer
3674 from django.conf.urls import patterns, include, url
3676 from django.contrib import admin
3678 from myapp import views
3681 urlpatterns = patterns('',
3682 url(r'^$', views.index
3685 (electric-indent-mode 1)
3686 (python-tests-look-at "views.index")
3689 ;; Inserting commas within the same line should leave
3690 ;; indentation unchanged.
3691 (python-tests-self-insert ",")
3692 (should (= (current-indentation) 4))
3694 ;; As well as any other input happening within the same
3696 (python-tests-self-insert " name='index')")
3697 (should (= (current-indentation) 4))
3699 ;; But a comma outside it, should trigger indentation.
3700 (python-tests-self-insert ",")
3701 (should (= (current-indentation) 23))
3703 ;; Newline indents to the first argument column
3704 (python-tests-self-insert "\n")
3705 (should (= (current-indentation) 23))
3707 ;; All this input must not change indentation
3709 (python-tests-self-insert "url(r'^/login$', views.login)")
3710 (should (= (current-indentation) 4))
3712 ;; But this comma does
3713 (python-tests-self-insert ",")
3714 (should (= (current-indentation) 23))))
3715 (or eim
(electric-indent-mode -
1)))))
3717 (ert-deftest python-triple-quote-pairing
()
3719 (let ((epm electric-pair-mode
))
3722 (python-tests-with-temp-buffer
3724 (or epm
(electric-pair-mode 1))
3725 (goto-char (1- (point-max)))
3726 (python-tests-self-insert ?
\")
3727 (should (string= (buffer-string)
3729 (should (= (point) 4)))
3730 (python-tests-with-temp-buffer
3732 (python-tests-self-insert (list ?
\" ?
\" ?
\"))
3733 (should (string= (buffer-string)
3735 (should (= (point) 4)))
3736 (python-tests-with-temp-buffer
3738 (goto-char (1- (point-max)))
3739 (python-tests-self-insert ?
\")
3740 (should (= (point) (1- (point-max))))
3741 (should (string= (buffer-string)
3743 (or epm
(electric-pair-mode -
1)))))
3746 (provide 'python-tests
)
3750 ;; indent-tabs-mode: nil
3753 ;;; python-tests.el ends here