1 ;;; python-tests.el --- Test suite for python.el
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (defmacro python-tests-with-temp-buffer
(contents &rest body
)
28 "Create a `python-mode' enabled temp buffer with CONTENTS.
29 BODY is code to be executed within the temp buffer. Point is
30 always located at the beginning of buffer."
31 (declare (indent 1) (debug t
))
35 (goto-char (point-min))
38 (defmacro python-tests-with-temp-file
(contents &rest body
)
39 "Create a `python-mode' enabled file with CONTENTS.
40 BODY is code to be executed within the temp buffer. Point is
41 always located at the beginning of buffer."
42 (declare (indent 1) (debug t
))
43 ;; temp-file never actually used for anything?
44 `(let* ((temp-file (make-temp-file "python-tests" nil
".py"))
45 (buffer (find-file-noselect temp-file
)))
47 (with-current-buffer buffer
50 (goto-char (point-min))
52 (and buffer
(kill-buffer buffer
))
53 (delete-file temp-file
))))
55 (defun python-tests-look-at (string &optional num restore-point
)
56 "Move point at beginning of STRING in the current buffer.
57 Optional argument NUM defaults to 1 and is an integer indicating
58 how many occurrences must be found, when positive the search is
59 done forwards, otherwise backwards. When RESTORE-POINT is
60 non-nil the point is not moved but the position found is still
61 returned. When searching forward and point is already looking at
62 STRING, it is skipped so the next STRING occurrence is selected."
63 (let* ((num (or num
1))
64 (starting-point (point))
65 (string (regexp-quote string
))
66 (search-fn (if (> num
0) #'re-search-forward
#'re-search-backward
))
67 (deinc-fn (if (> num
0) #'1-
#'1+))
71 (while (not (= num
0))
74 ;; Moving forward and already looking at STRING, skip it.
75 (forward-char (length (match-string-no-properties 0))))
76 (and (not (funcall search-fn string nil t
))
79 ;; `re-search-forward' leaves point at the end of the
80 ;; occurrence, move back so point is at the beginning
82 (forward-char (- (length (match-string-no-properties 0)))))
84 num
(funcall deinc-fn num
)
85 found-point
(point))))
87 (and restore-point
(goto-char starting-point
)))))
89 (defun python-tests-self-insert (char-or-str)
90 "Call `self-insert-command' for chars in CHAR-OR-STR."
93 ((characterp char-or-str
)
95 ((stringp char-or-str
)
96 (string-to-list char-or-str
))
98 (cl-remove-if #'characterp char-or-str
))
100 (t (error "CHAR-OR-STR must be a char, string, or list of char")))))
103 (let ((last-command-event char
))
104 (call-interactively 'self-insert-command
)))
108 ;;; Tests for your tests, so you can test while you test.
110 (ert-deftest python-tests-look-at-1
()
111 "Test forward movement."
112 (python-tests-with-temp-buffer
113 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
114 sed do eiusmod tempor incididunt ut labore et dolore magna
116 (let ((expected (save-excursion
118 (re-search-forward "et" nil t
))
121 (should (= (python-tests-look-at "et" 3 t
) expected
))
122 ;; Even if NUM is bigger than found occurrences the point of last
123 ;; one should be returned.
124 (should (= (python-tests-look-at "et" 6 t
) expected
))
125 ;; If already looking at STRING, it should skip it.
126 (dotimes (i 2) (re-search-forward "et"))
128 (should (= (python-tests-look-at "et") expected
)))))
130 (ert-deftest python-tests-look-at-2
()
131 "Test backward movement."
132 (python-tests-with-temp-buffer
133 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
134 sed do eiusmod tempor incididunt ut labore et dolore magna
138 (re-search-forward "et" nil t
)
142 (re-search-forward "et" nil t
))
143 (should (= (python-tests-look-at "et" -
3 t
) expected
))
144 (should (= (python-tests-look-at "et" -
6 t
) expected
)))))
150 ;;; Python specialized rx
153 ;;; Font-lock and syntax
155 (ert-deftest python-syntax-after-python-backspace
()
156 ;; `python-indent-dedent-line-backspace' garbles syntax
157 :expected-result
:failed
158 (python-tests-with-temp-buffer
160 (goto-char (point-max))
161 (python-indent-dedent-line-backspace 1)
162 (should (string= (buffer-string) "\"\""))
163 (should (null (nth 3 (syntax-ppss))))))
168 ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
170 (ert-deftest python-indent-pep8-1
()
172 (python-tests-with-temp-buffer
173 "# Aligned with opening delimiter
174 foo = long_function_name(var_one, var_two,
177 (should (eq (car (python-indent-context)) 'no-indent
))
178 (should (= (python-indent-calculate-indentation) 0))
179 (python-tests-look-at "foo = long_function_name(var_one, var_two,")
180 (should (eq (car (python-indent-context)) 'after-line
))
181 (should (= (python-indent-calculate-indentation) 0))
182 (python-tests-look-at "var_three, var_four)")
183 (should (eq (car (python-indent-context)) 'inside-paren
))
184 (should (= (python-indent-calculate-indentation) 25))))
186 (ert-deftest python-indent-pep8-2
()
188 (python-tests-with-temp-buffer
189 "# More indentation included to distinguish this from the rest.
190 def long_function_name(
191 var_one, var_two, var_three,
195 (should (eq (car (python-indent-context)) 'no-indent
))
196 (should (= (python-indent-calculate-indentation) 0))
197 (python-tests-look-at "def long_function_name(")
198 (should (eq (car (python-indent-context)) 'after-line
))
199 (should (= (python-indent-calculate-indentation) 0))
200 (python-tests-look-at "var_one, var_two, var_three,")
201 (should (eq (car (python-indent-context)) 'inside-paren
))
202 (should (= (python-indent-calculate-indentation) 8))
203 (python-tests-look-at "var_four):")
204 (should (eq (car (python-indent-context)) 'inside-paren
))
205 (should (= (python-indent-calculate-indentation) 8))
206 (python-tests-look-at "print (var_one)")
207 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
208 (should (= (python-indent-calculate-indentation) 4))))
210 (ert-deftest python-indent-pep8-3
()
212 (python-tests-with-temp-buffer
213 "# Extra indentation is not necessary.
214 foo = long_function_name(
218 (should (eq (car (python-indent-context)) 'no-indent
))
219 (should (= (python-indent-calculate-indentation) 0))
220 (python-tests-look-at "foo = long_function_name(")
221 (should (eq (car (python-indent-context)) 'after-line
))
222 (should (= (python-indent-calculate-indentation) 0))
223 (python-tests-look-at "var_one, var_two,")
224 (should (eq (car (python-indent-context)) 'inside-paren
))
225 (should (= (python-indent-calculate-indentation) 4))
226 (python-tests-look-at "var_three, var_four)")
227 (should (eq (car (python-indent-context)) 'inside-paren
))
228 (should (= (python-indent-calculate-indentation) 4))))
230 (ert-deftest python-indent-after-comment-1
()
231 "The most simple after-comment case that shouldn't fail."
232 (python-tests-with-temp-buffer
233 "# Contents will be modified to correct indentation
235 def _on_child_complete(self, child_future):
236 if self.in_terminal_state():
238 # We only complete when all our async children have entered a
239 # terminal state. At that point, if any child failed, we fail
240 # with the exception with which the first child failed.
242 (python-tests-look-at "# We only complete")
243 (should (eq (car (python-indent-context)) 'after-line
))
244 (should (= (python-indent-calculate-indentation) 8))
245 (python-tests-look-at "# terminal state")
246 (should (eq (car (python-indent-context)) 'after-comment
))
247 (should (= (python-indent-calculate-indentation) 8))
248 (python-tests-look-at "# with the exception")
249 (should (eq (car (python-indent-context)) 'after-comment
))
250 ;; This one indents relative to previous block, even given the fact
251 ;; that it was under-indented.
252 (should (= (python-indent-calculate-indentation) 4))
253 (python-tests-look-at "# terminal state" -
1)
254 ;; It doesn't hurt to check again.
255 (should (eq (car (python-indent-context)) 'after-comment
))
257 (should (= (current-indentation) 8))
258 (python-tests-look-at "# with the exception")
259 (should (eq (car (python-indent-context)) 'after-comment
))
260 ;; Now everything should be lined up.
261 (should (= (python-indent-calculate-indentation) 8))))
263 (ert-deftest python-indent-after-comment-2
()
264 "Test after-comment in weird cases."
265 (python-tests-with-temp-buffer
266 "# Contents will be modified to correct indentation
270 # This comment is badly indented just because.
271 # But we won't mess with the user in this line.
273 now_we_do_mess_cause_this_is_not_a_comment = 1
277 (python-tests-look-at "# I don't do much")
278 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
279 (should (= (python-indent-calculate-indentation) 4))
280 (python-tests-look-at "return arg")
281 ;; Comment here just gets ignored, this line is not a comment so
282 ;; the rules won't apply here.
283 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
284 (should (= (python-indent-calculate-indentation) 4))
285 (python-tests-look-at "# This comment is badly")
286 (should (eq (car (python-indent-context)) 'after-line
))
287 ;; The return keyword moves indentation backwards 4 spaces, but
288 ;; let's assume this comment was placed there because the user
289 ;; wanted to (manually adding spaces or whatever).
290 (should (= (python-indent-calculate-indentation) 0))
291 (python-tests-look-at "# but we won't mess")
292 (should (eq (car (python-indent-context)) 'after-comment
))
293 (should (= (python-indent-calculate-indentation) 4))
294 ;; Behave the same for blank lines: potentially a comment.
296 (should (eq (car (python-indent-context)) 'after-comment
))
297 (should (= (python-indent-calculate-indentation) 4))
298 (python-tests-look-at "now_we_do_mess")
299 ;; Here is where comment indentation starts to get ignored and
300 ;; where the user can't freely indent anymore.
301 (should (eq (car (python-indent-context)) 'after-line
))
302 (should (= (python-indent-calculate-indentation) 0))
303 (python-tests-look-at "# yeah, that.")
304 (should (eq (car (python-indent-context)) 'after-line
))
305 (should (= (python-indent-calculate-indentation) 0))))
307 (ert-deftest python-indent-inside-paren-1
()
308 "The most simple inside-paren case that shouldn't fail."
309 (python-tests-with-temp-buffer
327 (python-tests-look-at "data = {")
328 (should (eq (car (python-indent-context)) 'after-line
))
329 (should (= (python-indent-calculate-indentation) 0))
330 (python-tests-look-at "'key':")
331 (should (eq (car (python-indent-context)) 'inside-paren
))
332 (should (= (python-indent-calculate-indentation) 4))
333 (python-tests-look-at "{")
334 (should (eq (car (python-indent-context)) 'inside-paren
))
335 (should (= (python-indent-calculate-indentation) 4))
336 (python-tests-look-at "'objlist': [")
337 (should (eq (car (python-indent-context)) 'inside-paren
))
338 (should (= (python-indent-calculate-indentation) 8))
339 (python-tests-look-at "{")
340 (should (eq (car (python-indent-context)) 'inside-paren
))
341 (should (= (python-indent-calculate-indentation) 12))
342 (python-tests-look-at "'pk': 1,")
343 (should (eq (car (python-indent-context)) 'inside-paren
))
344 (should (= (python-indent-calculate-indentation) 16))
345 (python-tests-look-at "'name': 'first',")
346 (should (eq (car (python-indent-context)) 'inside-paren
))
347 (should (= (python-indent-calculate-indentation) 16))
348 (python-tests-look-at "},")
349 (should (eq (car (python-indent-context)) 'inside-paren
))
350 (should (= (python-indent-calculate-indentation) 12))
351 (python-tests-look-at "{")
352 (should (eq (car (python-indent-context)) 'inside-paren
))
353 (should (= (python-indent-calculate-indentation) 12))
354 (python-tests-look-at "'pk': 2,")
355 (should (eq (car (python-indent-context)) 'inside-paren
))
356 (should (= (python-indent-calculate-indentation) 16))
357 (python-tests-look-at "'name': 'second',")
358 (should (eq (car (python-indent-context)) 'inside-paren
))
359 (should (= (python-indent-calculate-indentation) 16))
360 (python-tests-look-at "}")
361 (should (eq (car (python-indent-context)) 'inside-paren
))
362 (should (= (python-indent-calculate-indentation) 12))
363 (python-tests-look-at "]")
364 (should (eq (car (python-indent-context)) 'inside-paren
))
365 (should (= (python-indent-calculate-indentation) 8))
366 (python-tests-look-at "}")
367 (should (eq (car (python-indent-context)) 'inside-paren
))
368 (should (= (python-indent-calculate-indentation) 4))
369 (python-tests-look-at "}")
370 (should (eq (car (python-indent-context)) 'inside-paren
))
371 (should (= (python-indent-calculate-indentation) 0))))
373 (ert-deftest python-indent-inside-paren-2
()
374 "Another more compact paren group style."
375 (python-tests-with-temp-buffer
386 (python-tests-look-at "data = {")
387 (should (eq (car (python-indent-context)) 'after-line
))
388 (should (= (python-indent-calculate-indentation) 0))
389 (python-tests-look-at "'objlist': [")
390 (should (eq (car (python-indent-context)) 'inside-paren
))
391 (should (= (python-indent-calculate-indentation) 4))
392 (python-tests-look-at "{'pk': 1,")
393 (should (eq (car (python-indent-context)) 'inside-paren
))
394 (should (= (python-indent-calculate-indentation) 8))
395 (python-tests-look-at "'name': 'first'},")
396 (should (eq (car (python-indent-context)) 'inside-paren
))
397 (should (= (python-indent-calculate-indentation) 9))
398 (python-tests-look-at "{'pk': 2,")
399 (should (eq (car (python-indent-context)) 'inside-paren
))
400 (should (= (python-indent-calculate-indentation) 8))
401 (python-tests-look-at "'name': 'second'}")
402 (should (eq (car (python-indent-context)) 'inside-paren
))
403 (should (= (python-indent-calculate-indentation) 9))
404 (python-tests-look-at "]")
405 (should (eq (car (python-indent-context)) 'inside-paren
))
406 (should (= (python-indent-calculate-indentation) 4))
407 (python-tests-look-at "}}")
408 (should (eq (car (python-indent-context)) 'inside-paren
))
409 (should (= (python-indent-calculate-indentation) 0))
410 (python-tests-look-at "}")
411 (should (eq (car (python-indent-context)) 'inside-paren
))
412 (should (= (python-indent-calculate-indentation) 0))))
414 (ert-deftest python-indent-after-block-1
()
415 "The most simple after-block case that shouldn't fail."
416 (python-tests-with-temp-buffer
418 def foo(a, b, c=True):
420 (should (eq (car (python-indent-context)) 'no-indent
))
421 (should (= (python-indent-calculate-indentation) 0))
422 (goto-char (point-max))
423 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
424 (should (= (python-indent-calculate-indentation) 4))))
426 (ert-deftest python-indent-after-block-2
()
427 "A weird (malformed) multiline block statement."
428 (python-tests-with-temp-buffer
434 (goto-char (point-max))
435 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
436 (should (= (python-indent-calculate-indentation) 4))))
438 (ert-deftest python-indent-after-backslash-1
()
439 "The most common case."
440 (python-tests-with-temp-buffer
442 from foo.bar.baz import something, something_1 \\\\
443 something_2 something_3, \\\\
444 something_4, something_5
446 (python-tests-look-at "from foo.bar.baz import something, something_1")
447 (should (eq (car (python-indent-context)) 'after-line
))
448 (should (= (python-indent-calculate-indentation) 0))
449 (python-tests-look-at "something_2 something_3,")
450 (should (eq (car (python-indent-context)) 'after-backslash
))
451 (should (= (python-indent-calculate-indentation) 4))
452 (python-tests-look-at "something_4, something_5")
453 (should (eq (car (python-indent-context)) 'after-backslash
))
454 (should (= (python-indent-calculate-indentation) 4))
455 (goto-char (point-max))
456 (should (eq (car (python-indent-context)) 'after-line
))
457 (should (= (python-indent-calculate-indentation) 0))))
459 (ert-deftest python-indent-after-backslash-2
()
460 "A pretty extreme complicated case."
461 (python-tests-with-temp-buffer
463 objects = Thing.objects.all() \\\\
473 (python-tests-look-at "objects = Thing.objects.all()")
474 (should (eq (car (python-indent-context)) 'after-line
))
475 (should (= (python-indent-calculate-indentation) 0))
476 (python-tests-look-at ".filter(")
477 (should (eq (car (python-indent-context)) 'after-backslash
))
478 (should (= (python-indent-calculate-indentation) 23))
479 (python-tests-look-at "type='toy',")
480 (should (eq (car (python-indent-context)) 'inside-paren
))
481 (should (= (python-indent-calculate-indentation) 27))
482 (python-tests-look-at "status='bought'")
483 (should (eq (car (python-indent-context)) 'inside-paren
))
484 (should (= (python-indent-calculate-indentation) 27))
485 (python-tests-look-at ") \\\\")
486 (should (eq (car (python-indent-context)) 'inside-paren
))
487 (should (= (python-indent-calculate-indentation) 23))
488 (python-tests-look-at ".aggregate(")
489 (should (eq (car (python-indent-context)) 'after-backslash
))
490 (should (= (python-indent-calculate-indentation) 23))
491 (python-tests-look-at "Sum('amount')")
492 (should (eq (car (python-indent-context)) 'inside-paren
))
493 (should (= (python-indent-calculate-indentation) 27))
494 (python-tests-look-at ") \\\\")
495 (should (eq (car (python-indent-context)) 'inside-paren
))
496 (should (= (python-indent-calculate-indentation) 23))
497 (python-tests-look-at ".values_list()")
498 (should (eq (car (python-indent-context)) 'after-backslash
))
499 (should (= (python-indent-calculate-indentation) 23))
501 (should (eq (car (python-indent-context)) 'after-line
))
502 (should (= (python-indent-calculate-indentation) 0))))
504 (ert-deftest python-indent-block-enders-1
()
505 "Test de-indentation for pass keyword."
506 (python-tests-with-temp-buffer
519 (python-tests-look-at "3)")
521 (should (= (python-indent-calculate-indentation) 8))
522 (python-tests-look-at "pass")
524 (should (= (python-indent-calculate-indentation) 8))))
526 (ert-deftest python-indent-block-enders-2
()
527 "Test de-indentation for return keyword."
528 (python-tests-with-temp-buffer
531 '''raise lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
533 eiusmod tempor incididunt ut labore et dolore magna aliqua.
536 \"return (1, 2, 3).\"
542 (python-tests-look-at "def")
543 (should (= (python-indent-calculate-indentation) 4))
544 (python-tests-look-at "if")
545 (should (= (python-indent-calculate-indentation) 8))
546 (python-tests-look-at "return")
547 (should (= (python-indent-calculate-indentation) 12))
548 (goto-char (point-max))
549 (should (= (python-indent-calculate-indentation) 8))))
551 (ert-deftest python-indent-block-enders-3
()
552 "Test de-indentation for continue keyword."
553 (python-tests-with-temp-buffer
559 (python-tests-look-at "if")
560 (should (= (python-indent-calculate-indentation) 4))
561 (python-tests-look-at "continue")
562 (should (= (python-indent-calculate-indentation) 8))
564 (should (= (python-indent-calculate-indentation) 4))))
566 (ert-deftest python-indent-block-enders-4
()
567 "Test de-indentation for break keyword."
568 (python-tests-with-temp-buffer
574 (python-tests-look-at "if")
575 (should (= (python-indent-calculate-indentation) 4))
576 (python-tests-look-at "break")
577 (should (= (python-indent-calculate-indentation) 8))
579 (should (= (python-indent-calculate-indentation) 4))))
581 (ert-deftest python-indent-block-enders-5
()
582 "Test de-indentation for raise keyword."
583 (python-tests-with-temp-buffer
587 raise ValueError('Element cannot be None')
589 (python-tests-look-at "if")
590 (should (= (python-indent-calculate-indentation) 4))
591 (python-tests-look-at "raise")
592 (should (= (python-indent-calculate-indentation) 8))
594 (should (= (python-indent-calculate-indentation) 4))))
596 (ert-deftest python-indent-dedenters-1
()
597 "Test de-indentation for the elif keyword."
598 (python-tests-with-temp-buffer
607 (python-tests-look-at "elif\n")
608 (should (eq (car (python-indent-context)) 'dedenter-statement
))
609 (should (= (python-indent-calculate-indentation) 0))
610 (should (equal (python-indent-calculate-levels) '(0)))))
612 (ert-deftest python-indent-dedenters-2
()
613 "Test de-indentation for the else keyword."
614 (python-tests-with-temp-buffer
620 msg = 'Error saving to disk'
622 logger.exception(msg)
625 logger.exception('Unhandled exception')
630 (python-tests-look-at "else\n")
631 (should (eq (car (python-indent-context)) 'dedenter-statement
))
632 (should (= (python-indent-calculate-indentation) 8))
633 (should (equal (python-indent-calculate-levels) '(0 4 8)))))
635 (ert-deftest python-indent-dedenters-3
()
636 "Test de-indentation for the except keyword."
637 (python-tests-with-temp-buffer
644 (python-tests-look-at "except\n")
645 (should (eq (car (python-indent-context)) 'dedenter-statement
))
646 (should (= (python-indent-calculate-indentation) 4))
647 (should (equal (python-indent-calculate-levels) '(4)))))
649 (ert-deftest python-indent-dedenters-4
()
650 "Test de-indentation for the finally keyword."
651 (python-tests-with-temp-buffer
658 (python-tests-look-at "finally\n")
659 (should (eq (car (python-indent-context)) 'dedenter-statement
))
660 (should (= (python-indent-calculate-indentation) 4))
661 (should (equal (python-indent-calculate-levels) '(4)))))
663 (ert-deftest python-indent-dedenters-5
()
664 "Test invalid levels are skipped in a complex example."
665 (python-tests-with-temp-buffer
671 msg = 'Error saving to disk'
673 logger.exception(msg)
679 (python-tests-look-at "else\n")
680 (should (eq (car (python-indent-context)) 'dedenter-statement
))
681 (should (= (python-indent-calculate-indentation) 8))
682 (should (equal (python-indent-calculate-levels) '(0 8)))))
684 (ert-deftest python-indent-dedenters-6
()
685 "Test indentation is zero when no opening block for dedenter."
686 (python-tests-with-temp-buffer
693 (python-tests-look-at "else\n")
694 (should (eq (car (python-indent-context)) 'dedenter-statement
))
695 (should (= (python-indent-calculate-indentation) 0))
696 (should (equal (python-indent-calculate-levels) '(0)))))
698 (ert-deftest python-indent-dedenters-7
()
699 "Test indentation case from Bug#15163."
700 (python-tests-with-temp-buffer
709 (python-tests-look-at "else:" 2)
710 (should (eq (car (python-indent-context)) 'dedenter-statement
))
711 (should (= (python-indent-calculate-indentation) 0))
712 (should (equal (python-indent-calculate-levels) '(0)))))
714 (ert-deftest python-indent-dedenters-8
()
715 "Test indentation for Bug#18432."
716 (python-tests-with-temp-buffer
724 (python-tests-look-at "a == 4):\n")
725 (should (eq (car (python-indent-context)) 'inside-paren
))
726 (should (= (python-indent-calculate-indentation) 6))
727 (should (equal (python-indent-calculate-levels) '(0 4 6)))))
729 (ert-deftest python-indent-electric-colon-1
()
730 "Test indentation case from Bug#18228."
731 (python-tests-with-temp-buffer
738 (python-tests-look-at "def b()")
739 (goto-char (line-end-position))
740 (python-tests-self-insert ":")
741 (should (= (current-indentation) 0))))
743 (ert-deftest python-indent-electric-colon-2
()
744 "Test indentation case for dedenter."
745 (python-tests-with-temp-buffer
751 (python-tests-look-at "else")
752 (goto-char (line-end-position))
753 (python-tests-self-insert ":")
754 (should (= (current-indentation) 0))))
756 (ert-deftest python-indent-electric-colon-3
()
757 "Test indentation case for multi-line dedenter."
758 (python-tests-with-temp-buffer
766 (python-tests-look-at "that)")
767 (goto-char (line-end-position))
768 (python-tests-self-insert ":")
769 (python-tests-look-at "elif" -
1)
770 (should (= (current-indentation) 0))
771 (python-tests-look-at "and")
772 (should (= (current-indentation) 6))
773 (python-tests-look-at "that)")
774 (should (= (current-indentation) 6))))
776 (ert-deftest python-indent-region-1
()
777 "Test indentation case from Bug#18843."
785 (python-tests-with-temp-buffer
787 (python-indent-region (point-min) (point-max))
788 (should (string= (buffer-substring-no-properties (point-min) (point-max))
791 (ert-deftest python-indent-region-2
()
792 "Test region indentation on comments."
802 (python-tests-with-temp-buffer
804 (python-indent-region (point-min) (point-max))
805 (should (string= (buffer-substring-no-properties (point-min) (point-max))
808 (ert-deftest python-indent-region-3
()
809 "Test region indentation on comments."
826 (python-tests-with-temp-buffer
828 (python-indent-region (point-min) (point-max))
829 (should (string= (buffer-substring-no-properties (point-min) (point-max))
832 (ert-deftest python-indent-region-4
()
833 "Test region indentation block starts, dedenders and enders."
850 (python-tests-with-temp-buffer
852 (python-indent-region (point-min) (point-max))
853 (should (string= (buffer-substring-no-properties (point-min) (point-max))
856 (ert-deftest python-indent-region-5
()
857 "Test region indentation leaves strings untouched (start delimiter)."
874 (python-tests-with-temp-buffer
876 (python-indent-region (point-min) (point-max))
877 (should (string= (buffer-substring-no-properties (point-min) (point-max))
883 (ert-deftest python-nav-beginning-of-defun-1
()
884 (python-tests-with-temp-buffer
886 def decoratorFunctionWithArguments(arg1, arg2, arg3):
887 '''print decorated function call data to stdout.
891 @decoratorFunctionWithArguments('arg1', 'arg2')
892 def func(a, b, c=True):
897 print 'Inside wwrap()'
898 def wrapped_f(*args):
899 print 'Inside wrapped_f()'
900 print 'Decorator arguments:', arg1, arg2, arg3
902 print 'After f(*args)'
906 (python-tests-look-at "return wrap")
907 (should (= (save-excursion
908 (python-nav-beginning-of-defun)
911 (python-tests-look-at "def wrapped_f(*args):" -
1)
914 (python-tests-look-at "def wrapped_f(*args):" -
1)
915 (should (= (save-excursion
916 (python-nav-beginning-of-defun)
919 (python-tests-look-at "def wwrap(f):" -
1)
922 (python-tests-look-at "def wwrap(f):" -
1)
923 (should (= (save-excursion
924 (python-nav-beginning-of-defun)
927 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)
931 (ert-deftest python-nav-beginning-of-defun-2
()
932 (python-tests-with-temp-buffer
948 ;; Nested defuns, are handled with care.
949 (python-tests-look-at "def c(self):")
950 (should (= (save-excursion
951 (python-nav-beginning-of-defun)
954 (python-tests-look-at "def m(self):" -
1)
957 ;; Defuns on same levels should be respected.
958 (python-tests-look-at "def a():" -
1)
959 (should (= (save-excursion
960 (python-nav-beginning-of-defun)
963 (python-tests-look-at "def b():" -
1)
966 ;; Jump to a top level defun.
967 (python-tests-look-at "def b():" -
1)
968 (should (= (save-excursion
969 (python-nav-beginning-of-defun)
972 (python-tests-look-at "def m(self):" -
1)
975 ;; Jump to a top level defun again.
976 (python-tests-look-at "def m(self):" -
1)
977 (should (= (save-excursion
978 (python-nav-beginning-of-defun)
981 (python-tests-look-at "class C(object):" -
1)
985 (ert-deftest python-nav-end-of-defun-1
()
986 (python-tests-with-temp-buffer
1002 (should (= (save-excursion
1003 (python-tests-look-at "class C(object):")
1004 (python-nav-end-of-defun)
1008 (should (= (save-excursion
1009 (python-tests-look-at "def m(self):")
1010 (python-nav-end-of-defun)
1013 (python-tests-look-at "def c(self):")
1016 (should (= (save-excursion
1017 (python-tests-look-at "def b():")
1018 (python-nav-end-of-defun)
1021 (python-tests-look-at "def b():")
1024 (should (= (save-excursion
1025 (python-tests-look-at "def c(self):")
1026 (python-nav-end-of-defun)
1031 (ert-deftest python-nav-end-of-defun-2
()
1032 (python-tests-with-temp-buffer
1034 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1035 '''print decorated function call data to stdout.
1039 @decoratorFunctionWithArguments('arg1', 'arg2')
1040 def func(a, b, c=True):
1045 print 'Inside wwrap()'
1046 def wrapped_f(*args):
1047 print 'Inside wrapped_f()'
1048 print 'Decorator arguments:', arg1, arg2, arg3
1050 print 'After f(*args)'
1054 (should (= (save-excursion
1055 (python-tests-look-at "def decoratorFunctionWithArguments")
1056 (python-nav-end-of-defun)
1060 (should (= (save-excursion
1061 (python-tests-look-at "@decoratorFunctionWithArguments")
1062 (python-nav-end-of-defun)
1066 (should (= (save-excursion
1067 (python-tests-look-at "def wwrap(f):")
1068 (python-nav-end-of-defun)
1071 (python-tests-look-at "return wwrap")
1072 (line-beginning-position))))
1073 (should (= (save-excursion
1074 (python-tests-look-at "def wrapped_f(*args):")
1075 (python-nav-end-of-defun)
1078 (python-tests-look-at "return wrapped_f")
1079 (line-beginning-position))))
1080 (should (= (save-excursion
1081 (python-tests-look-at "f(*args)")
1082 (python-nav-end-of-defun)
1085 (python-tests-look-at "return wrapped_f")
1086 (line-beginning-position))))))
1088 (ert-deftest python-nav-backward-defun-1
()
1089 (python-tests-with-temp-buffer
1091 class A(object): # A
1099 class B(object): # B
1101 class C(object): # C
1115 (goto-char (point-max))
1116 (should (= (save-excursion (python-nav-backward-defun))
1117 (python-tests-look-at " def c(self): # c" -
1)))
1118 (should (= (save-excursion (python-nav-backward-defun))
1119 (python-tests-look-at " def d(self): # d" -
1)))
1120 (should (= (save-excursion (python-nav-backward-defun))
1121 (python-tests-look-at " class C(object): # C" -
1)))
1122 (should (= (save-excursion (python-nav-backward-defun))
1123 (python-tests-look-at " class B(object): # B" -
1)))
1124 (should (= (save-excursion (python-nav-backward-defun))
1125 (python-tests-look-at " def b(self): # b" -
1)))
1126 (should (= (save-excursion (python-nav-backward-defun))
1127 (python-tests-look-at " def a(self): # a" -
1)))
1128 (should (= (save-excursion (python-nav-backward-defun))
1129 (python-tests-look-at "class A(object): # A" -
1)))
1130 (should (not (python-nav-backward-defun)))))
1132 (ert-deftest python-nav-backward-defun-2
()
1133 (python-tests-with-temp-buffer
1135 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1136 '''print decorated function call data to stdout.
1140 @decoratorFunctionWithArguments('arg1', 'arg2')
1141 def func(a, b, c=True):
1146 print 'Inside wwrap()'
1147 def wrapped_f(*args):
1148 print 'Inside wrapped_f()'
1149 print 'Decorator arguments:', arg1, arg2, arg3
1151 print 'After f(*args)'
1155 (goto-char (point-max))
1156 (should (= (save-excursion (python-nav-backward-defun))
1157 (python-tests-look-at " def wrapped_f(*args):" -
1)))
1158 (should (= (save-excursion (python-nav-backward-defun))
1159 (python-tests-look-at " def wwrap(f):" -
1)))
1160 (should (= (save-excursion (python-nav-backward-defun))
1161 (python-tests-look-at "def decoratorFunctionWithArguments(arg1, arg2, arg3):" -
1)))
1162 (should (not (python-nav-backward-defun)))))
1164 (ert-deftest python-nav-backward-defun-3
()
1165 (python-tests-with-temp-buffer
1181 (goto-char (point-min))
1182 (let ((point (python-tests-look-at "class A(object):")))
1183 (should (not (python-nav-backward-defun)))
1184 (should (= point
(point))))))
1186 (ert-deftest python-nav-forward-defun-1
()
1187 (python-tests-with-temp-buffer
1189 class A(object): # A
1197 class B(object): # B
1199 class C(object): # C
1213 (goto-char (point-min))
1214 (should (= (save-excursion (python-nav-forward-defun))
1215 (python-tests-look-at "(object): # A")))
1216 (should (= (save-excursion (python-nav-forward-defun))
1217 (python-tests-look-at "(self): # a")))
1218 (should (= (save-excursion (python-nav-forward-defun))
1219 (python-tests-look-at "(self): # b")))
1220 (should (= (save-excursion (python-nav-forward-defun))
1221 (python-tests-look-at "(object): # B")))
1222 (should (= (save-excursion (python-nav-forward-defun))
1223 (python-tests-look-at "(object): # C")))
1224 (should (= (save-excursion (python-nav-forward-defun))
1225 (python-tests-look-at "(self): # d")))
1226 (should (= (save-excursion (python-nav-forward-defun))
1227 (python-tests-look-at "(self): # c")))
1228 (should (not (python-nav-forward-defun)))))
1230 (ert-deftest python-nav-forward-defun-2
()
1231 (python-tests-with-temp-buffer
1233 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1234 '''print decorated function call data to stdout.
1238 @decoratorFunctionWithArguments('arg1', 'arg2')
1239 def func(a, b, c=True):
1244 print 'Inside wwrap()'
1245 def wrapped_f(*args):
1246 print 'Inside wrapped_f()'
1247 print 'Decorator arguments:', arg1, arg2, arg3
1249 print 'After f(*args)'
1253 (goto-char (point-min))
1254 (should (= (save-excursion (python-nav-forward-defun))
1255 (python-tests-look-at "(arg1, arg2, arg3):")))
1256 (should (= (save-excursion (python-nav-forward-defun))
1257 (python-tests-look-at "(f):")))
1258 (should (= (save-excursion (python-nav-forward-defun))
1259 (python-tests-look-at "(*args):")))
1260 (should (not (python-nav-forward-defun)))))
1262 (ert-deftest python-nav-forward-defun-3
()
1263 (python-tests-with-temp-buffer
1279 (goto-char (point-min))
1280 (let ((point (python-tests-look-at "(object):")))
1281 (should (not (python-nav-forward-defun)))
1282 (should (= point
(point))))))
1284 (ert-deftest python-nav-beginning-of-statement-1
()
1285 (python-tests-with-temp-buffer
1295 v3 = ('this is a string'
1300 # this is a comment, yo
1301 'continue previous line')
1307 (python-tests-look-at "v2 =")
1308 (python-util-forward-comment -
1)
1309 (should (= (save-excursion
1310 (python-nav-beginning-of-statement)
1312 (python-tests-look-at "v1 =" -
1 t
)))
1313 (python-tests-look-at "v3 =")
1314 (python-util-forward-comment -
1)
1315 (should (= (save-excursion
1316 (python-nav-beginning-of-statement)
1318 (python-tests-look-at "v2 =" -
1 t
)))
1319 (python-tests-look-at "v4 =")
1320 (python-util-forward-comment -
1)
1321 (should (= (save-excursion
1322 (python-nav-beginning-of-statement)
1324 (python-tests-look-at "v3 =" -
1 t
)))
1325 (goto-char (point-max))
1326 (python-util-forward-comment -
1)
1327 (should (= (save-excursion
1328 (python-nav-beginning-of-statement)
1330 (python-tests-look-at "v4 =" -
1 t
)))))
1332 (ert-deftest python-nav-end-of-statement-1
()
1333 (python-tests-with-temp-buffer
1343 v3 = ('this is a string'
1348 # this is a comment, yo
1349 'continue previous line')
1355 (python-tests-look-at "v1 =")
1356 (should (= (save-excursion
1357 (python-nav-end-of-statement)
1360 (python-tests-look-at "789")
1361 (line-end-position))))
1362 (python-tests-look-at "v2 =")
1363 (should (= (save-excursion
1364 (python-nav-end-of-statement)
1367 (python-tests-look-at "value4)")
1368 (line-end-position))))
1369 (python-tests-look-at "v3 =")
1370 (should (= (save-excursion
1371 (python-nav-end-of-statement)
1374 (python-tests-look-at
1375 "'continue previous line')")
1376 (line-end-position))))
1377 (python-tests-look-at "v4 =")
1378 (should (= (save-excursion
1379 (python-nav-end-of-statement)
1382 (goto-char (point-max))
1383 (python-util-forward-comment -
1)
1386 (ert-deftest python-nav-forward-statement-1
()
1387 (python-tests-with-temp-buffer
1397 v3 = ('this is a string'
1402 # this is a comment, yo
1403 'continue previous line')
1409 (python-tests-look-at "v1 =")
1410 (should (= (save-excursion
1411 (python-nav-forward-statement)
1413 (python-tests-look-at "v2 =")))
1414 (should (= (save-excursion
1415 (python-nav-forward-statement)
1417 (python-tests-look-at "v3 =")))
1418 (should (= (save-excursion
1419 (python-nav-forward-statement)
1421 (python-tests-look-at "v4 =")))
1422 (should (= (save-excursion
1423 (python-nav-forward-statement)
1427 (ert-deftest python-nav-backward-statement-1
()
1428 (python-tests-with-temp-buffer
1438 v3 = ('this is a string'
1443 # this is a comment, yo
1444 'continue previous line')
1450 (goto-char (point-max))
1451 (should (= (save-excursion
1452 (python-nav-backward-statement)
1454 (python-tests-look-at "v4 =" -
1)))
1455 (should (= (save-excursion
1456 (python-nav-backward-statement)
1458 (python-tests-look-at "v3 =" -
1)))
1459 (should (= (save-excursion
1460 (python-nav-backward-statement)
1462 (python-tests-look-at "v2 =" -
1)))
1463 (should (= (save-excursion
1464 (python-nav-backward-statement)
1466 (python-tests-look-at "v1 =" -
1)))))
1468 (ert-deftest python-nav-backward-statement-2
()
1469 :expected-result
:failed
1470 (python-tests-with-temp-buffer
1481 ;; FIXME: For some reason `python-nav-backward-statement' is moving
1482 ;; back two sentences when starting from 'value4)'.
1483 (goto-char (point-max))
1484 (python-util-forward-comment -
1)
1485 (should (= (save-excursion
1486 (python-nav-backward-statement)
1488 (python-tests-look-at "v2 =" -
1 t
)))))
1490 (ert-deftest python-nav-beginning-of-block-1
()
1491 (python-tests-with-temp-buffer
1493 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1494 '''print decorated function call data to stdout.
1498 @decoratorFunctionWithArguments('arg1', 'arg2')
1499 def func(a, b, c=True):
1504 print 'Inside wwrap()'
1505 def wrapped_f(*args):
1506 print 'Inside wrapped_f()'
1507 print 'Decorator arguments:', arg1, arg2, arg3
1509 print 'After f(*args)'
1513 (python-tests-look-at "return wwrap")
1514 (should (= (save-excursion
1515 (python-nav-beginning-of-block)
1517 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)))
1518 (python-tests-look-at "print 'Inside wwrap()'")
1519 (should (= (save-excursion
1520 (python-nav-beginning-of-block)
1522 (python-tests-look-at "def wwrap(f):" -
1)))
1523 (python-tests-look-at "print 'After f(*args)'")
1525 (should (= (save-excursion
1526 (python-nav-beginning-of-block)
1528 (python-tests-look-at "def wrapped_f(*args):" -
1)))
1529 (python-tests-look-at "return wrapped_f")
1530 (should (= (save-excursion
1531 (python-nav-beginning-of-block)
1533 (python-tests-look-at "def wwrap(f):" -
1)))))
1535 (ert-deftest python-nav-end-of-block-1
()
1536 (python-tests-with-temp-buffer
1538 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1539 '''print decorated function call data to stdout.
1543 @decoratorFunctionWithArguments('arg1', 'arg2')
1544 def func(a, b, c=True):
1549 print 'Inside wwrap()'
1550 def wrapped_f(*args):
1551 print 'Inside wrapped_f()'
1552 print 'Decorator arguments:', arg1, arg2, arg3
1554 print 'After f(*args)'
1558 (python-tests-look-at "def decoratorFunctionWithArguments")
1559 (should (= (save-excursion
1560 (python-nav-end-of-block)
1563 (goto-char (point-max))
1564 (python-util-forward-comment -
1)
1566 (python-tests-look-at "def wwrap(f):")
1567 (should (= (save-excursion
1568 (python-nav-end-of-block)
1571 (python-tests-look-at "return wrapped_f")
1572 (line-end-position))))
1574 (should (= (save-excursion
1575 (python-nav-end-of-block)
1578 (python-tests-look-at "return wrapped_f")
1579 (line-end-position))))
1580 (python-tests-look-at "f(*args)")
1581 (should (= (save-excursion
1582 (python-nav-end-of-block)
1585 (python-tests-look-at "print 'After f(*args)'")
1586 (line-end-position))))))
1588 (ert-deftest python-nav-forward-block-1
()
1589 "This also accounts as a test for `python-nav-backward-block'."
1590 (python-tests-with-temp-buffer
1592 if request.user.is_authenticated():
1596 profile = request.user.get_profile()
1597 except Profile.DoesNotExist:
1598 profile = Profile.objects.create(user=request.user)
1601 profile.recalculate_stats()
1603 profile.clear_stats()
1608 (should (= (save-excursion (python-nav-forward-block))
1609 (python-tests-look-at "if request.user.is_authenticated():")))
1610 (should (= (save-excursion (python-nav-forward-block))
1611 (python-tests-look-at "try:")))
1612 (should (= (save-excursion (python-nav-forward-block))
1613 (python-tests-look-at "except Profile.DoesNotExist:")))
1614 (should (= (save-excursion (python-nav-forward-block))
1615 (python-tests-look-at "else:")))
1616 (should (= (save-excursion (python-nav-forward-block))
1617 (python-tests-look-at "if profile.stats:")))
1618 (should (= (save-excursion (python-nav-forward-block))
1619 (python-tests-look-at "else:")))
1620 (should (= (save-excursion (python-nav-forward-block))
1621 (python-tests-look-at "finally:")))
1622 ;; When point is at the last block, leave it there and return nil
1623 (should (not (save-excursion (python-nav-forward-block))))
1624 ;; Move backwards, and even if the number of moves is less than the
1625 ;; provided argument return the point.
1626 (should (= (save-excursion (python-nav-forward-block -
10))
1627 (python-tests-look-at
1628 "if request.user.is_authenticated():" -
1)))))
1630 (ert-deftest python-nav-forward-sexp-1
()
1631 (python-tests-with-temp-buffer
1637 (python-tests-look-at "a()")
1638 (python-nav-forward-sexp)
1639 (should (looking-at "$"))
1640 (should (save-excursion
1642 (looking-at "a()")))
1643 (python-nav-forward-sexp)
1644 (should (looking-at "$"))
1645 (should (save-excursion
1647 (looking-at "b()")))
1648 (python-nav-forward-sexp)
1649 (should (looking-at "$"))
1650 (should (save-excursion
1652 (looking-at "c()")))
1653 ;; Movement next to a paren should do what lisp does and
1654 ;; unfortunately It can't change, because otherwise
1655 ;; `blink-matching-open' breaks.
1656 (python-nav-forward-sexp -
1)
1657 (should (looking-at "()"))
1658 (should (save-excursion
1660 (looking-at "c()")))
1661 (python-nav-forward-sexp -
1)
1662 (should (looking-at "c()"))
1663 (python-nav-forward-sexp -
1)
1664 (should (looking-at "b()"))
1665 (python-nav-forward-sexp -
1)
1666 (should (looking-at "a()"))))
1668 (ert-deftest python-nav-forward-sexp-2
()
1669 (python-tests-with-temp-buffer
1678 (python-tests-look-at "aa =")
1679 (python-nav-forward-sexp)
1680 (should (looking-at " = bbb"))
1681 (python-nav-forward-sexp)
1682 (should (looking-at "$"))
1683 (should (save-excursion
1684 (back-to-indentation)
1685 (looking-at "aaa = bbb")))
1686 (python-nav-forward-sexp)
1687 (should (looking-at "$"))
1688 (should (save-excursion
1689 (back-to-indentation)
1690 (looking-at "ccc = ddd")))
1691 (python-nav-forward-sexp)
1692 (should (looking-at "$"))
1693 (should (save-excursion
1694 (back-to-indentation)
1695 (looking-at "eee = fff")))
1696 (python-nav-forward-sexp)
1697 (should (looking-at "$"))
1698 (should (save-excursion
1699 (back-to-indentation)
1700 (looking-at "return ggg")))
1701 (python-nav-forward-sexp -
1)
1702 (should (looking-at "def func():"))))
1704 (ert-deftest python-nav-forward-sexp-3
()
1705 (python-tests-with-temp-buffer
1707 from some_module import some_sub_module
1708 from another_module import another_sub_module
1710 def another_statement():
1713 (python-tests-look-at "some_module")
1714 (python-nav-forward-sexp)
1715 (should (looking-at " import"))
1716 (python-nav-forward-sexp)
1717 (should (looking-at " some_sub_module"))
1718 (python-nav-forward-sexp)
1719 (should (looking-at "$"))
1722 (back-to-indentation)
1724 "from some_module import some_sub_module")))
1725 (python-nav-forward-sexp)
1726 (should (looking-at "$"))
1729 (back-to-indentation)
1731 "from another_module import another_sub_module")))
1732 (python-nav-forward-sexp)
1733 (should (looking-at "$"))
1736 (back-to-indentation)
1739 (python-nav-forward-sexp -
1)
1740 (should (looking-at "def another_statement():"))
1741 (python-nav-forward-sexp -
1)
1742 (should (looking-at "from another_module import another_sub_module"))
1743 (python-nav-forward-sexp -
1)
1744 (should (looking-at "from some_module import some_sub_module"))))
1746 (ert-deftest python-nav-forward-sexp-safe-1
()
1747 (python-tests-with-temp-buffer
1749 profile = Profile.objects.create(user=request.user)
1752 (python-tests-look-at "profile =")
1753 (python-nav-forward-sexp-safe 1)
1754 (should (looking-at "$"))
1755 (beginning-of-line 1)
1756 (python-tests-look-at "user=request.user")
1757 (python-nav-forward-sexp-safe -
1)
1758 (should (looking-at "(user=request.user)"))
1759 (python-nav-forward-sexp-safe -
4)
1760 (should (looking-at "profile ="))
1761 (python-tests-look-at "user=request.user")
1762 (python-nav-forward-sexp-safe 3)
1763 (should (looking-at ")"))
1764 (python-nav-forward-sexp-safe 1)
1765 (should (looking-at "$"))
1766 (python-nav-forward-sexp-safe 1)
1767 (should (looking-at "$"))))
1769 (ert-deftest python-nav-up-list-1
()
1770 (python-tests-with-temp-buffer
1774 return [i for i in range(3)]
1776 (python-tests-look-at "3)]")
1777 (python-nav-up-list)
1778 (should (looking-at "]"))
1779 (python-nav-up-list)
1780 (should (looking-at "$"))))
1782 (ert-deftest python-nav-backward-up-list-1
()
1783 :expected-result
:failed
1784 (python-tests-with-temp-buffer
1788 return [i for i in range(3)]
1790 (python-tests-look-at "3)]")
1791 (python-nav-backward-up-list)
1792 (should (looking-at "(3)\\]"))
1793 (python-nav-backward-up-list)
1795 "\\[i for i in range(3)\\]"))
1796 ;; FIXME: Need to move to beginning-of-statement.
1797 (python-nav-backward-up-list)
1799 "return \\[i for i in range(3)\\]"))
1800 (python-nav-backward-up-list)
1801 (should (looking-at "if True:"))
1802 (python-nav-backward-up-list)
1803 (should (looking-at "def f():"))))
1806 ;;; Shell integration
1808 (defvar python-tests-shell-interpreter
"python")
1810 (ert-deftest python-shell-get-process-name-1
()
1811 "Check process name calculation on different scenarios."
1812 (python-tests-with-temp-buffer
1814 (should (string= (python-shell-get-process-name nil
)
1815 python-shell-buffer-name
))
1816 ;; When the `current-buffer' doesn't have `buffer-file-name', even
1817 ;; if dedicated flag is non-nil should not include its name.
1818 (should (string= (python-shell-get-process-name t
)
1819 python-shell-buffer-name
)))
1820 (python-tests-with-temp-file
1822 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1823 ;; should be respected.
1824 (should (string= (python-shell-get-process-name nil
)
1825 python-shell-buffer-name
))
1827 (python-shell-get-process-name t
)
1828 (format "%s[%s]" python-shell-buffer-name buffer-file-name
)))))
1830 (ert-deftest python-shell-internal-get-process-name-1
()
1831 "Check the internal process name is config-unique."
1832 (let* ((python-shell-interpreter python-tests-shell-interpreter
)
1833 (python-shell-interpreter-args "")
1834 (python-shell-prompt-regexp ">>> ")
1835 (python-shell-prompt-block-regexp "[.][.][.] ")
1836 (python-shell-setup-codes "")
1837 (python-shell-process-environment "")
1838 (python-shell-extra-pythonpaths "")
1839 (python-shell-exec-path "")
1840 (python-shell-virtualenv-path "")
1841 (expected (python-tests-with-temp-buffer
1842 "" (python-shell-internal-get-process-name))))
1843 ;; Same configurations should match.
1846 (python-tests-with-temp-buffer
1847 "" (python-shell-internal-get-process-name))))
1848 (let ((python-shell-interpreter-args "-B"))
1849 ;; A minimal change should generate different names.
1853 (python-tests-with-temp-buffer
1854 "" (python-shell-internal-get-process-name))))))))
1856 (ert-deftest python-shell-parse-command-1
()
1857 "Check the command to execute is calculated correctly.
1858 Using `python-shell-interpreter' and
1859 `python-shell-interpreter-args'."
1860 (skip-unless (executable-find python-tests-shell-interpreter
))
1861 (let ((python-shell-interpreter (executable-find
1862 python-tests-shell-interpreter
))
1863 (python-shell-interpreter-args "-B"))
1866 python-shell-interpreter
1867 python-shell-interpreter-args
)
1868 (python-shell-parse-command)))))
1870 (ert-deftest python-shell-calculate-process-environment-1
()
1871 "Test `python-shell-process-environment' modification."
1872 (let* ((original-process-environment process-environment
)
1873 (python-shell-process-environment
1874 '("TESTVAR1=value1" "TESTVAR2=value2"))
1875 (process-environment
1876 (python-shell-calculate-process-environment)))
1877 (should (equal (getenv "TESTVAR1") "value1"))
1878 (should (equal (getenv "TESTVAR2") "value2"))))
1880 (ert-deftest python-shell-calculate-process-environment-2
()
1881 "Test `python-shell-extra-pythonpaths' modification."
1882 (let* ((original-process-environment process-environment
)
1883 (original-pythonpath (getenv "PYTHONPATH"))
1884 (paths '("path1" "path2"))
1885 (python-shell-extra-pythonpaths paths
)
1886 (process-environment
1887 (python-shell-calculate-process-environment)))
1888 (should (equal (getenv "PYTHONPATH")
1890 (mapconcat 'identity paths path-separator
)
1891 path-separator original-pythonpath
)))))
1893 (ert-deftest python-shell-calculate-process-environment-3
()
1894 "Test `python-shell-virtualenv-path' modification."
1895 (let* ((original-process-environment process-environment
)
1896 (original-path (or (getenv "PATH") ""))
1897 (python-shell-virtualenv-path
1898 (directory-file-name user-emacs-directory
))
1899 (process-environment
1900 (python-shell-calculate-process-environment)))
1901 (should (not (getenv "PYTHONHOME")))
1902 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-path
))
1903 (should (equal (getenv "PATH")
1904 (format "%s/bin%s%s"
1905 python-shell-virtualenv-path
1906 path-separator original-path
)))))
1908 (ert-deftest python-shell-calculate-process-environment-4
()
1909 "Test `python-shell-unbuffered' modification."
1910 (setenv "PYTHONUNBUFFERED")
1911 (let* ((process-environment
1912 (python-shell-calculate-process-environment)))
1914 (should python-shell-unbuffered
)
1915 (should (string= (getenv "PYTHONUNBUFFERED") "1"))))
1917 (ert-deftest python-shell-calculate-process-environment-5
()
1918 (setenv "PYTHONUNBUFFERED")
1919 "Test `python-shell-unbuffered' modification."
1920 (let* ((python-shell-unbuffered nil
)
1921 (process-environment
1922 (python-shell-calculate-process-environment)))
1923 (should (not (getenv "PYTHONUNBUFFERED")))))
1925 (ert-deftest python-shell-calculate-exec-path-1
()
1926 "Test `python-shell-exec-path' modification."
1927 (let* ((original-exec-path exec-path
)
1928 (python-shell-exec-path '("path1" "path2"))
1929 (exec-path (python-shell-calculate-exec-path)))
1932 (append python-shell-exec-path
1933 original-exec-path
)))))
1935 (ert-deftest python-shell-calculate-exec-path-2
()
1936 "Test `python-shell-exec-path' modification."
1937 (let* ((original-exec-path exec-path
)
1938 (python-shell-virtualenv-path
1939 (directory-file-name (expand-file-name user-emacs-directory
)))
1940 (exec-path (python-shell-calculate-exec-path)))
1944 (format "%s/bin" python-shell-virtualenv-path
)
1945 original-exec-path
))))))
1947 (ert-deftest python-shell-make-comint-1
()
1948 "Check comint creation for global shell buffer."
1949 (skip-unless (executable-find python-tests-shell-interpreter
))
1950 ;; The interpreter can get killed too quickly to allow it to clean
1951 ;; up the tempfiles that the default python-shell-setup-codes create,
1952 ;; so it leaves tempfiles behind, which is a minor irritation.
1953 (let* ((python-shell-setup-codes nil
)
1954 (python-shell-interpreter
1955 (executable-find python-tests-shell-interpreter
))
1956 (proc-name (python-shell-get-process-name nil
))
1958 (python-tests-with-temp-buffer
1959 "" (python-shell-make-comint
1960 (python-shell-parse-command) proc-name
)))
1961 (process (get-buffer-process shell-buffer
)))
1964 (set-process-query-on-exit-flag process nil
)
1965 (should (process-live-p process
))
1966 (with-current-buffer shell-buffer
1967 (should (eq major-mode
'inferior-python-mode
))
1968 (should (string= (buffer-name) (format "*%s*" proc-name
)))))
1969 (kill-buffer shell-buffer
))))
1971 (ert-deftest python-shell-make-comint-2
()
1972 "Check comint creation for internal shell buffer."
1973 (skip-unless (executable-find python-tests-shell-interpreter
))
1974 (let* ((python-shell-setup-codes nil
)
1975 (python-shell-interpreter
1976 (executable-find python-tests-shell-interpreter
))
1977 (proc-name (python-shell-internal-get-process-name))
1979 (python-tests-with-temp-buffer
1980 "" (python-shell-make-comint
1981 (python-shell-parse-command) proc-name nil t
)))
1982 (process (get-buffer-process shell-buffer
)))
1985 (set-process-query-on-exit-flag process nil
)
1986 (should (process-live-p process
))
1987 (with-current-buffer shell-buffer
1988 (should (eq major-mode
'inferior-python-mode
))
1989 (should (string= (buffer-name) (format " *%s*" proc-name
)))))
1990 (kill-buffer shell-buffer
))))
1992 (ert-deftest python-shell-make-comint-3
()
1993 "Check comint creation with overridden python interpreter and args.
1994 The command passed to `python-shell-make-comint' as argument must
1995 locally override global values set in `python-shell-interpreter'
1996 and `python-shell-interpreter-args' in the new shell buffer."
1997 (skip-unless (executable-find python-tests-shell-interpreter
))
1998 (let* ((python-shell-setup-codes nil
)
1999 (python-shell-interpreter "interpreter")
2000 (python-shell-interpreter-args "--some-args")
2001 (proc-name (python-shell-get-process-name nil
))
2002 (interpreter-override
2003 (concat (executable-find python-tests-shell-interpreter
) " " "-i"))
2005 (python-tests-with-temp-buffer
2006 "" (python-shell-make-comint interpreter-override proc-name nil
)))
2007 (process (get-buffer-process shell-buffer
)))
2010 (set-process-query-on-exit-flag process nil
)
2011 (should (process-live-p process
))
2012 (with-current-buffer shell-buffer
2013 (should (eq major-mode
'inferior-python-mode
))
2014 (should (file-equal-p
2015 python-shell-interpreter
2016 (executable-find python-tests-shell-interpreter
)))
2017 (should (string= python-shell-interpreter-args
"-i"))))
2018 (kill-buffer shell-buffer
))))
2020 (ert-deftest python-shell-make-comint-4
()
2021 "Check shell calculated prompts regexps are set."
2022 (skip-unless (executable-find python-tests-shell-interpreter
))
2023 (let* ((process-environment process-environment
)
2024 (python-shell-setup-codes nil
)
2025 (python-shell-interpreter
2026 (executable-find python-tests-shell-interpreter
))
2027 (python-shell-interpreter-args "-i")
2028 (python-shell--prompt-calculated-input-regexp nil
)
2029 (python-shell--prompt-calculated-output-regexp nil
)
2030 (python-shell-prompt-detect-enabled t
)
2031 (python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2032 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2033 (python-shell-prompt-regexp "in")
2034 (python-shell-prompt-block-regexp "block")
2035 (python-shell-prompt-pdb-regexp "pdf")
2036 (python-shell-prompt-output-regexp "output")
2037 (startup-code (concat "import sys\n"
2038 "sys.ps1 = 'py> '\n"
2039 "sys.ps2 = '..> '\n"
2040 "sys.ps3 = 'out '\n"))
2041 (startup-file (python-shell--save-temp-file startup-code
))
2042 (proc-name (python-shell-get-process-name nil
))
2045 (setenv "PYTHONSTARTUP" startup-file
)
2046 (python-tests-with-temp-buffer
2047 "" (python-shell-make-comint
2048 (python-shell-parse-command) proc-name nil
))))
2049 (process (get-buffer-process shell-buffer
)))
2052 (set-process-query-on-exit-flag process nil
)
2053 (should (process-live-p process
))
2054 (with-current-buffer shell-buffer
2055 (should (eq major-mode
'inferior-python-mode
))
2057 python-shell--prompt-calculated-input-regexp
2058 (concat "^\\(extralargeinputprompt\\|\\.\\.> \\|"
2059 "block\\|py> \\|pdf\\|sml\\|in\\)")))
2061 python-shell--prompt-calculated-output-regexp
2062 "^\\(extralargeoutputprompt\\|output\\|out \\|sml\\)"))))
2063 (delete-file startup-file
)
2064 (kill-buffer shell-buffer
))))
2066 (ert-deftest python-shell-get-process-1
()
2067 "Check dedicated shell process preference over global."
2068 (skip-unless (executable-find python-tests-shell-interpreter
))
2069 (python-tests-with-temp-file
2071 (let* ((python-shell-setup-codes nil
)
2072 (python-shell-interpreter
2073 (executable-find python-tests-shell-interpreter
))
2074 (global-proc-name (python-shell-get-process-name nil
))
2075 (dedicated-proc-name (python-shell-get-process-name t
))
2076 (global-shell-buffer
2077 (python-shell-make-comint
2078 (python-shell-parse-command) global-proc-name
))
2079 (dedicated-shell-buffer
2080 (python-shell-make-comint
2081 (python-shell-parse-command) dedicated-proc-name
))
2082 (global-process (get-buffer-process global-shell-buffer
))
2083 (dedicated-process (get-buffer-process dedicated-shell-buffer
)))
2086 (set-process-query-on-exit-flag global-process nil
)
2087 (set-process-query-on-exit-flag dedicated-process nil
)
2088 ;; Prefer dedicated if global also exists.
2089 (should (equal (python-shell-get-process) dedicated-process
))
2090 (kill-buffer dedicated-shell-buffer
)
2091 ;; If there's only global, use it.
2092 (should (equal (python-shell-get-process) global-process
))
2093 (kill-buffer global-shell-buffer
)
2094 ;; No buffer available.
2095 (should (not (python-shell-get-process))))
2096 (ignore-errors (kill-buffer global-shell-buffer
))
2097 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
2099 (ert-deftest python-shell-get-or-create-process-1
()
2100 "Check shell dedicated process creation."
2101 (skip-unless (executable-find python-tests-shell-interpreter
))
2102 (python-tests-with-temp-file
2105 (concat (executable-find python-tests-shell-interpreter
) " -i"))
2107 (dedicated-process-name (python-shell-get-process-name t
))
2108 (dedicated-process (python-shell-get-or-create-process cmd t
))
2109 (dedicated-shell-buffer (process-buffer dedicated-process
)))
2112 (set-process-query-on-exit-flag dedicated-process nil
)
2113 ;; should be dedicated.
2114 (should (equal (process-name dedicated-process
)
2115 dedicated-process-name
))
2116 (kill-buffer dedicated-shell-buffer
)
2117 ;; Check there are no processes for current buffer.
2118 (should (not (python-shell-get-process))))
2119 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
2121 (ert-deftest python-shell-get-or-create-process-2
()
2122 "Check shell global process creation."
2123 (skip-unless (executable-find python-tests-shell-interpreter
))
2124 (python-tests-with-temp-file
2127 (concat (executable-find python-tests-shell-interpreter
) " -i"))
2129 (process-name (python-shell-get-process-name nil
))
2130 (process (python-shell-get-or-create-process cmd
))
2131 (shell-buffer (process-buffer process
)))
2134 (set-process-query-on-exit-flag process nil
)
2135 ;; should be global.
2136 (should (equal (process-name process
) process-name
))
2137 (kill-buffer shell-buffer
)
2138 ;; Check there are no processes for current buffer.
2139 (should (not (python-shell-get-process))))
2140 (ignore-errors (kill-buffer shell-buffer
))))))
2142 (ert-deftest python-shell-get-or-create-process-3
()
2143 "Check shell dedicated/global process preference."
2144 (skip-unless (executable-find python-tests-shell-interpreter
))
2145 (python-tests-with-temp-file
2148 (concat (executable-find python-tests-shell-interpreter
) " -i"))
2149 (python-shell-interpreter python-tests-shell-interpreter
)
2151 (dedicated-process-name (python-shell-get-process-name t
))
2153 (dedicated-process))
2155 ;; Create global process
2156 (run-python cmd nil
)
2157 (setq global-process
(get-buffer-process "*Python*"))
2158 (should global-process
)
2159 (set-process-query-on-exit-flag global-process nil
)
2160 ;; Create dedicated process
2162 (setq dedicated-process
(get-process dedicated-process-name
))
2163 (should dedicated-process
)
2164 (set-process-query-on-exit-flag dedicated-process nil
)
2165 ;; Prefer dedicated.
2166 (should (equal (python-shell-get-or-create-process)
2168 ;; Kill the dedicated so the global takes over.
2169 (kill-buffer (process-buffer dedicated-process
))
2171 (should (equal (python-shell-get-or-create-process) global-process
))
2173 (kill-buffer (process-buffer global-process
))
2174 ;; Check there are no processes for current buffer.
2175 (should (not (python-shell-get-process)))))))
2177 (ert-deftest python-shell-internal-get-or-create-process-1
()
2178 "Check internal shell process creation fallback."
2179 (skip-unless (executable-find python-tests-shell-interpreter
))
2180 (python-tests-with-temp-file
2182 (should (not (process-live-p (python-shell-internal-get-process-name))))
2183 (let* ((python-shell-interpreter
2184 (executable-find python-tests-shell-interpreter
))
2185 (internal-process-name (python-shell-internal-get-process-name))
2186 (internal-process (python-shell-internal-get-or-create-process))
2187 (internal-shell-buffer (process-buffer internal-process
)))
2190 (set-process-query-on-exit-flag internal-process nil
)
2191 (should (equal (process-name internal-process
)
2192 internal-process-name
))
2193 (should (equal internal-process
2194 (python-shell-internal-get-or-create-process)))
2195 ;; Assert the internal process is not a user process
2196 (should (not (python-shell-get-process)))
2197 (kill-buffer internal-shell-buffer
))
2198 (ignore-errors (kill-buffer internal-shell-buffer
))))))
2200 (ert-deftest python-shell-prompt-detect-1
()
2201 "Check prompt autodetection."
2202 (skip-unless (executable-find python-tests-shell-interpreter
))
2203 (let ((process-environment process-environment
))
2204 ;; Ensure no startup file is enabled
2205 (setenv "PYTHONSTARTUP" "")
2206 (should python-shell-prompt-detect-enabled
)
2207 (should (equal (python-shell-prompt-detect) '(">>> " "... " "")))))
2209 (ert-deftest python-shell-prompt-detect-2
()
2210 "Check prompt autodetection with startup file. Bug#17370."
2211 (skip-unless (executable-find python-tests-shell-interpreter
))
2212 (let* ((process-environment process-environment
)
2213 (startup-code (concat "import sys\n"
2214 "sys.ps1 = 'py> '\n"
2215 "sys.ps2 = '..> '\n"
2216 "sys.ps3 = 'out '\n"))
2217 (startup-file (python-shell--save-temp-file startup-code
)))
2220 ;; Ensure startup file is enabled
2221 (setenv "PYTHONSTARTUP" startup-file
)
2222 (should python-shell-prompt-detect-enabled
)
2223 (should (equal (python-shell-prompt-detect) '("py> " "..> " "out "))))
2224 (ignore-errors (delete-file startup-file
)))))
2226 (ert-deftest python-shell-prompt-detect-3
()
2227 "Check prompts are not autodetected when feature is disabled."
2228 (skip-unless (executable-find python-tests-shell-interpreter
))
2229 (let ((process-environment process-environment
)
2230 (python-shell-prompt-detect-enabled nil
))
2231 ;; Ensure no startup file is enabled
2232 (should (not python-shell-prompt-detect-enabled
))
2233 (should (not (python-shell-prompt-detect)))))
2235 (ert-deftest python-shell-prompt-detect-4
()
2236 "Check warning is shown when detection fails."
2237 (skip-unless (executable-find python-tests-shell-interpreter
))
2238 (let* ((process-environment process-environment
)
2239 ;; Trigger failure by removing prompts in the startup file
2240 (startup-code (concat "import sys\n"
2244 (startup-file (python-shell--save-temp-file startup-code
)))
2247 (kill-buffer (get-buffer-create "*Warnings*"))
2248 (should (not (get-buffer "*Warnings*")))
2249 (setenv "PYTHONSTARTUP" startup-file
)
2250 (should python-shell-prompt-detect-failure-warning
)
2251 (should python-shell-prompt-detect-enabled
)
2252 (should (not (python-shell-prompt-detect)))
2253 (should (get-buffer "*Warnings*")))
2254 (ignore-errors (delete-file startup-file
)))))
2256 (ert-deftest python-shell-prompt-detect-5
()
2257 "Check disabled warnings are not shown when detection fails."
2258 (skip-unless (executable-find python-tests-shell-interpreter
))
2259 (let* ((process-environment process-environment
)
2260 (startup-code (concat "import sys\n"
2264 (startup-file (python-shell--save-temp-file startup-code
))
2265 (python-shell-prompt-detect-failure-warning nil
))
2268 (kill-buffer (get-buffer-create "*Warnings*"))
2269 (should (not (get-buffer "*Warnings*")))
2270 (setenv "PYTHONSTARTUP" startup-file
)
2271 (should (not python-shell-prompt-detect-failure-warning
))
2272 (should python-shell-prompt-detect-enabled
)
2273 (should (not (python-shell-prompt-detect)))
2274 (should (not (get-buffer "*Warnings*"))))
2275 (ignore-errors (delete-file startup-file
)))))
2277 (ert-deftest python-shell-prompt-detect-6
()
2278 "Warnings are not shown when detection is disabled."
2279 (skip-unless (executable-find python-tests-shell-interpreter
))
2280 (let* ((process-environment process-environment
)
2281 (startup-code (concat "import sys\n"
2285 (startup-file (python-shell--save-temp-file startup-code
))
2286 (python-shell-prompt-detect-failure-warning t
)
2287 (python-shell-prompt-detect-enabled nil
))
2290 (kill-buffer (get-buffer-create "*Warnings*"))
2291 (should (not (get-buffer "*Warnings*")))
2292 (setenv "PYTHONSTARTUP" startup-file
)
2293 (should python-shell-prompt-detect-failure-warning
)
2294 (should (not python-shell-prompt-detect-enabled
))
2295 (should (not (python-shell-prompt-detect)))
2296 (should (not (get-buffer "*Warnings*"))))
2297 (ignore-errors (delete-file startup-file
)))))
2299 (ert-deftest python-shell-prompt-validate-regexps-1
()
2300 "Check `python-shell-prompt-input-regexps' are validated."
2301 (let* ((python-shell-prompt-input-regexps '("\\("))
2302 (error-data (should-error (python-shell-prompt-validate-regexps)
2303 :type
'user-error
)))
2305 (string= (cadr error-data
)
2306 "Invalid regexp \\( in `python-shell-prompt-input-regexps'"))))
2308 (ert-deftest python-shell-prompt-validate-regexps-2
()
2309 "Check `python-shell-prompt-output-regexps' are validated."
2310 (let* ((python-shell-prompt-output-regexps '("\\("))
2311 (error-data (should-error (python-shell-prompt-validate-regexps)
2312 :type
'user-error
)))
2314 (string= (cadr error-data
)
2315 "Invalid regexp \\( in `python-shell-prompt-output-regexps'"))))
2317 (ert-deftest python-shell-prompt-validate-regexps-3
()
2318 "Check `python-shell-prompt-regexp' is validated."
2319 (let* ((python-shell-prompt-regexp "\\(")
2320 (error-data (should-error (python-shell-prompt-validate-regexps)
2321 :type
'user-error
)))
2323 (string= (cadr error-data
)
2324 "Invalid regexp \\( in `python-shell-prompt-regexp'"))))
2326 (ert-deftest python-shell-prompt-validate-regexps-4
()
2327 "Check `python-shell-prompt-block-regexp' is validated."
2328 (let* ((python-shell-prompt-block-regexp "\\(")
2329 (error-data (should-error (python-shell-prompt-validate-regexps)
2330 :type
'user-error
)))
2332 (string= (cadr error-data
)
2333 "Invalid regexp \\( in `python-shell-prompt-block-regexp'"))))
2335 (ert-deftest python-shell-prompt-validate-regexps-5
()
2336 "Check `python-shell-prompt-pdb-regexp' is validated."
2337 (let* ((python-shell-prompt-pdb-regexp "\\(")
2338 (error-data (should-error (python-shell-prompt-validate-regexps)
2339 :type
'user-error
)))
2341 (string= (cadr error-data
)
2342 "Invalid regexp \\( in `python-shell-prompt-pdb-regexp'"))))
2344 (ert-deftest python-shell-prompt-validate-regexps-6
()
2345 "Check `python-shell-prompt-output-regexp' is validated."
2346 (let* ((python-shell-prompt-output-regexp "\\(")
2347 (error-data (should-error (python-shell-prompt-validate-regexps)
2348 :type
'user-error
)))
2350 (string= (cadr error-data
)
2351 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2353 (ert-deftest python-shell-prompt-validate-regexps-7
()
2354 "Check default regexps are valid."
2355 ;; should not signal error
2356 (python-shell-prompt-validate-regexps))
2358 (ert-deftest python-shell-prompt-set-calculated-regexps-1
()
2359 "Check regexps are validated."
2360 (let* ((python-shell-prompt-output-regexp '("\\("))
2361 (python-shell--prompt-calculated-input-regexp nil
)
2362 (python-shell--prompt-calculated-output-regexp nil
)
2363 (python-shell-prompt-detect-enabled nil
)
2364 (error-data (should-error (python-shell-prompt-set-calculated-regexps)
2365 :type
'user-error
)))
2367 (string= (cadr error-data
)
2368 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2370 (ert-deftest python-shell-prompt-set-calculated-regexps-2
()
2371 "Check `python-shell-prompt-input-regexps' are set."
2372 (let* ((python-shell-prompt-input-regexps '("my" "prompt"))
2373 (python-shell-prompt-output-regexps '(""))
2374 (python-shell-prompt-regexp "")
2375 (python-shell-prompt-block-regexp "")
2376 (python-shell-prompt-pdb-regexp "")
2377 (python-shell-prompt-output-regexp "")
2378 (python-shell--prompt-calculated-input-regexp nil
)
2379 (python-shell--prompt-calculated-output-regexp nil
)
2380 (python-shell-prompt-detect-enabled nil
))
2381 (python-shell-prompt-set-calculated-regexps)
2382 (should (string= python-shell--prompt-calculated-input-regexp
2383 "^\\(prompt\\|my\\|\\)"))))
2385 (ert-deftest python-shell-prompt-set-calculated-regexps-3
()
2386 "Check `python-shell-prompt-output-regexps' are set."
2387 (let* ((python-shell-prompt-input-regexps '(""))
2388 (python-shell-prompt-output-regexps '("my" "prompt"))
2389 (python-shell-prompt-regexp "")
2390 (python-shell-prompt-block-regexp "")
2391 (python-shell-prompt-pdb-regexp "")
2392 (python-shell-prompt-output-regexp "")
2393 (python-shell--prompt-calculated-input-regexp nil
)
2394 (python-shell--prompt-calculated-output-regexp nil
)
2395 (python-shell-prompt-detect-enabled nil
))
2396 (python-shell-prompt-set-calculated-regexps)
2397 (should (string= python-shell--prompt-calculated-output-regexp
2398 "^\\(prompt\\|my\\|\\)"))))
2400 (ert-deftest python-shell-prompt-set-calculated-regexps-4
()
2401 "Check user defined prompts are set."
2402 (let* ((python-shell-prompt-input-regexps '(""))
2403 (python-shell-prompt-output-regexps '(""))
2404 (python-shell-prompt-regexp "prompt")
2405 (python-shell-prompt-block-regexp "block")
2406 (python-shell-prompt-pdb-regexp "pdb")
2407 (python-shell-prompt-output-regexp "output")
2408 (python-shell--prompt-calculated-input-regexp nil
)
2409 (python-shell--prompt-calculated-output-regexp nil
)
2410 (python-shell-prompt-detect-enabled nil
))
2411 (python-shell-prompt-set-calculated-regexps)
2412 (should (string= python-shell--prompt-calculated-input-regexp
2413 "^\\(prompt\\|block\\|pdb\\|\\)"))
2414 (should (string= python-shell--prompt-calculated-output-regexp
2415 "^\\(output\\|\\)"))))
2417 (ert-deftest python-shell-prompt-set-calculated-regexps-5
()
2418 "Check order of regexps (larger first)."
2419 (let* ((python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2420 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2421 (python-shell-prompt-regexp "in")
2422 (python-shell-prompt-block-regexp "block")
2423 (python-shell-prompt-pdb-regexp "pdf")
2424 (python-shell-prompt-output-regexp "output")
2425 (python-shell--prompt-calculated-input-regexp nil
)
2426 (python-shell--prompt-calculated-output-regexp nil
)
2427 (python-shell-prompt-detect-enabled nil
))
2428 (python-shell-prompt-set-calculated-regexps)
2429 (should (string= python-shell--prompt-calculated-input-regexp
2430 "^\\(extralargeinputprompt\\|block\\|pdf\\|sml\\|in\\)"))
2431 (should (string= python-shell--prompt-calculated-output-regexp
2432 "^\\(extralargeoutputprompt\\|output\\|sml\\)"))))
2434 (ert-deftest python-shell-prompt-set-calculated-regexps-6
()
2435 "Check detected prompts are included `regexp-quote'd."
2436 (skip-unless (executable-find python-tests-shell-interpreter
))
2437 (let* ((python-shell-prompt-input-regexps '(""))
2438 (python-shell-prompt-output-regexps '(""))
2439 (python-shell-prompt-regexp "")
2440 (python-shell-prompt-block-regexp "")
2441 (python-shell-prompt-pdb-regexp "")
2442 (python-shell-prompt-output-regexp "")
2443 (python-shell--prompt-calculated-input-regexp nil
)
2444 (python-shell--prompt-calculated-output-regexp nil
)
2445 (python-shell-prompt-detect-enabled t
)
2446 (process-environment process-environment
)
2447 (startup-code (concat "import sys\n"
2448 "sys.ps1 = 'p.> '\n"
2449 "sys.ps2 = '..> '\n"
2450 "sys.ps3 = 'o.t '\n"))
2451 (startup-file (python-shell--save-temp-file startup-code
)))
2454 (setenv "PYTHONSTARTUP" startup-file
)
2455 (python-shell-prompt-set-calculated-regexps)
2456 (should (string= python-shell--prompt-calculated-input-regexp
2457 "^\\(\\.\\.> \\|p\\.> \\|\\)"))
2458 (should (string= python-shell--prompt-calculated-output-regexp
2459 "^\\(o\\.t \\|\\)")))
2460 (ignore-errors (delete-file startup-file
)))))
2462 (ert-deftest python-shell-buffer-substring-1
()
2463 "Selecting a substring of the whole buffer must match its contents."
2464 (python-tests-with-temp-buffer
2466 class Foo(models.Model):
2470 class Bar(models.Model):
2473 (should (string= (buffer-string)
2474 (python-shell-buffer-substring (point-min) (point-max))))))
2476 (ert-deftest python-shell-buffer-substring-2
()
2477 "Main block should be removed if NOMAIN is non-nil."
2478 (python-tests-with-temp-buffer
2480 class Foo(models.Model):
2483 class Bar(models.Model):
2486 if __name__ == \"__main__\":
2490 (should (string= (python-shell-buffer-substring (point-min) (point-max) t
)
2492 class Foo(models.Model):
2495 class Bar(models.Model):
2503 (ert-deftest python-shell-buffer-substring-3
()
2504 "Main block should be removed if NOMAIN is non-nil."
2505 (python-tests-with-temp-buffer
2507 class Foo(models.Model):
2510 if __name__ == \"__main__\":
2514 class Bar(models.Model):
2517 (should (string= (python-shell-buffer-substring (point-min) (point-max) t
)
2519 class Foo(models.Model):
2526 class Bar(models.Model):
2530 (ert-deftest python-shell-buffer-substring-4
()
2531 "Coding cookie should be added for substrings."
2532 (python-tests-with-temp-buffer
2535 class Foo(models.Model):
2538 if __name__ == \"__main__\":
2542 class Bar(models.Model):
2545 (should (string= (python-shell-buffer-substring
2546 (python-tests-look-at "class Foo(models.Model):")
2547 (progn (python-nav-forward-sexp) (point)))
2548 "# -*- coding: latin-1 -*-
2550 class Foo(models.Model):
2553 (ert-deftest python-shell-buffer-substring-5
()
2554 "The proper amount of blank lines is added for a substring."
2555 (python-tests-with-temp-buffer
2558 class Foo(models.Model):
2561 if __name__ == \"__main__\":
2565 class Bar(models.Model):
2568 (should (string= (python-shell-buffer-substring
2569 (python-tests-look-at "class Bar(models.Model):")
2570 (progn (python-nav-forward-sexp) (point)))
2571 "# -*- coding: latin-1 -*-
2580 class Bar(models.Model):
2583 (ert-deftest python-shell-buffer-substring-6
()
2584 "Handle substring with coding cookie in the second line."
2585 (python-tests-with-temp-buffer
2589 class Foo(models.Model):
2592 if __name__ == \"__main__\":
2596 class Bar(models.Model):
2599 (should (string= (python-shell-buffer-substring
2600 (python-tests-look-at "# coding: latin-1")
2601 (python-tests-look-at "if __name__ == \"__main__\":"))
2602 "# -*- coding: latin-1 -*-
2605 class Foo(models.Model):
2610 (ert-deftest python-shell-buffer-substring-7
()
2611 "Ensure first coding cookie gets precedence."
2612 (python-tests-with-temp-buffer
2616 class Foo(models.Model):
2619 if __name__ == \"__main__\":
2623 class Bar(models.Model):
2626 (should (string= (python-shell-buffer-substring
2627 (python-tests-look-at "# coding: latin-1")
2628 (python-tests-look-at "if __name__ == \"__main__\":"))
2629 "# -*- coding: utf-8 -*-
2632 class Foo(models.Model):
2637 (ert-deftest python-shell-buffer-substring-8
()
2638 "Ensure first coding cookie gets precedence when sending whole buffer."
2639 (python-tests-with-temp-buffer
2643 class Foo(models.Model):
2646 (should (string= (python-shell-buffer-substring (point-min) (point-max))
2650 class Foo(models.Model):
2655 ;;; Shell completion
2658 ;;; PDB Track integration
2661 ;;; Symbol completion
2681 (ert-deftest python-imenu-create-index-1
()
2682 (python-tests-with-temp-buffer
2684 class Foo(models.Model):
2688 class Bar(models.Model):
2692 def decorator(arg1, arg2, arg3):
2693 '''print decorated function call data to stdout.
2697 @decorator('arg1', 'arg2')
2698 def func(a, b, c=True):
2704 def wrapped_f(*args):
2706 print ('Decorator arguments:', arg1, arg2, arg3)
2708 print ('called f(*args)')
2726 (goto-char (point-max))
2729 (cons "Foo (class)" (copy-marker 2))
2730 (cons "Bar (class)" (copy-marker 38))
2733 (cons "*function definition*" (copy-marker 74))
2736 (cons "*function definition*" (copy-marker 254))
2737 (cons "wrapped_f (def)" (copy-marker 294))))
2740 (cons "*class definition*" (copy-marker 519))
2741 (cons "a (def)" (copy-marker 539))
2742 (cons "b (def)" (copy-marker 570))
2745 (cons "*class definition*" (copy-marker 601))
2746 (cons "c (def)" (copy-marker 626)))))
2747 (python-imenu-create-index)))))
2749 (ert-deftest python-imenu-create-index-2
()
2750 (python-tests-with-temp-buffer
2760 (goto-char (point-max))
2765 (cons "*class definition*" (copy-marker 2))
2768 (cons "*function definition*" (copy-marker 21))
2769 (cons "foo1 (def)" (copy-marker 40)))
2770 (cons "foobar (def)" (copy-marker 78))))
2771 (python-imenu-create-index)))))
2773 (ert-deftest python-imenu-create-index-3
()
2774 (python-tests-with-temp-buffer
2783 (goto-char (point-max))
2788 (cons "*class definition*" (copy-marker 2))
2791 (cons "*function definition*" (copy-marker 21))
2792 (cons "foo1 (def)" (copy-marker 40))
2793 (cons "foo2 (def)" (copy-marker 77)))))
2794 (python-imenu-create-index)))))
2796 (ert-deftest python-imenu-create-index-4
()
2797 (python-tests-with-temp-buffer
2810 (goto-char (point-max))
2815 (cons "*class definition*" (copy-marker 2))
2818 (cons "*class definition*" (copy-marker 21))
2819 (cons "__init__ (def)" (copy-marker 44))
2820 (cons "__str__ (def)" (copy-marker 90)))
2821 (cons "__init__ (def)" (copy-marker 135))))
2822 (python-imenu-create-index)))))
2824 (ert-deftest python-imenu-create-flat-index-1
()
2825 (python-tests-with-temp-buffer
2827 class Foo(models.Model):
2831 class Bar(models.Model):
2835 def decorator(arg1, arg2, arg3):
2836 '''print decorated function call data to stdout.
2840 @decorator('arg1', 'arg2')
2841 def func(a, b, c=True):
2847 def wrapped_f(*args):
2849 print ('Decorator arguments:', arg1, arg2, arg3)
2851 print ('called f(*args)')
2869 (goto-char (point-max))
2871 (list (cons "Foo" (copy-marker 2))
2872 (cons "Bar" (copy-marker 38))
2873 (cons "decorator" (copy-marker 74))
2874 (cons "decorator.wrap" (copy-marker 254))
2875 (cons "decorator.wrap.wrapped_f" (copy-marker 294))
2876 (cons "Baz" (copy-marker 519))
2877 (cons "Baz.a" (copy-marker 539))
2878 (cons "Baz.b" (copy-marker 570))
2879 (cons "Baz.Frob" (copy-marker 601))
2880 (cons "Baz.Frob.c" (copy-marker 626)))
2881 (python-imenu-create-flat-index)))))
2883 (ert-deftest python-imenu-create-flat-index-2
()
2884 (python-tests-with-temp-buffer
2897 (goto-char (point-max))
2900 (cons "Foo" (copy-marker 2))
2901 (cons "Foo.Bar" (copy-marker 21))
2902 (cons "Foo.Bar.__init__" (copy-marker 44))
2903 (cons "Foo.Bar.__str__" (copy-marker 90))
2904 (cons "Foo.__init__" (copy-marker 135)))
2905 (python-imenu-create-flat-index)))))
2910 (ert-deftest python-info-current-defun-1
()
2911 (python-tests-with-temp-buffer
2916 (should (string= "foo" (python-info-current-defun)))
2917 (should (string= "def foo" (python-info-current-defun t)))
2919 (should (not (python-info-current-defun)))
2920 (indent-for-tab-command)
2921 (should (string= "foo" (python-info-current-defun)))
2922 (should (string= "def foo" (python-info-current-defun t)))))
2924 (ert-deftest python-info-current-defun-2
()
2925 (python-tests-with-temp-buffer
2931 return [i for i in range(3)]
2945 (should (string= "C" (python-info-current-defun)))
2946 (should (string= "class C" (python-info-current-defun t)))
2947 (python-tests-look-at "return [i for ")
2948 (should (string= "C.m" (python-info-current-defun)))
2949 (should (string= "def C.m" (python-info-current-defun t)))
2950 (python-tests-look-at "def b():")
2951 (should (string= "C.m.b" (python-info-current-defun)))
2952 (should (string= "def C.m.b" (python-info-current-defun t)))
2954 (indent-for-tab-command)
2955 (python-indent-dedent-line-backspace 1)
2956 (should (string= "C.m" (python-info-current-defun)))
2957 (should (string= "def C.m" (python-info-current-defun t)))
2958 (python-tests-look-at "def c(self):")
2960 (indent-for-tab-command)
2961 (should (string= "C.m.a" (python-info-current-defun)))
2962 (should (string= "def C.m.a" (python-info-current-defun t)))
2963 (python-indent-dedent-line-backspace 1)
2964 (should (string= "C.m" (python-info-current-defun)))
2965 (should (string= "def C.m" (python-info-current-defun t)))
2966 (python-indent-dedent-line-backspace 1)
2967 (should (string= "C" (python-info-current-defun)))
2968 (should (string= "class C" (python-info-current-defun t)))
2969 (python-tests-look-at "def c(self):")
2970 (should (string= "C.c" (python-info-current-defun)))
2971 (should (string= "def C.c" (python-info-current-defun t)))
2972 (python-tests-look-at "do_c()")
2973 (should (string= "C.c" (python-info-current-defun)))
2974 (should (string= "def C.c" (python-info-current-defun t)))))
2976 (ert-deftest python-info-current-defun-3
()
2977 (python-tests-with-temp-buffer
2979 def decoratorFunctionWithArguments(arg1, arg2, arg3):
2980 '''print decorated function call data to stdout.
2984 @decoratorFunctionWithArguments('arg1', 'arg2')
2985 def func(a, b, c=True):
2990 print 'Inside wwrap()'
2991 def wrapped_f(*args):
2992 print 'Inside wrapped_f()'
2993 print 'Decorator arguments:', arg1, arg2, arg3
2995 print 'After f(*args)'
2999 (python-tests-look-at "def wwrap(f):")
3001 (should (not (python-info-current-defun)))
3002 (indent-for-tab-command 1)
3003 (should (string= (python-info-current-defun)
3004 "decoratorFunctionWithArguments"))
3005 (should (string= (python-info-current-defun t)
3006 "def decoratorFunctionWithArguments"))
3007 (python-tests-look-at "def wrapped_f(*args):")
3008 (should (string= (python-info-current-defun)
3009 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
3010 (should (string= (python-info-current-defun t)
3011 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
3012 (python-tests-look-at "return wrapped_f")
3013 (should (string= (python-info-current-defun)
3014 "decoratorFunctionWithArguments.wwrap"))
3015 (should (string= (python-info-current-defun t)
3016 "def decoratorFunctionWithArguments.wwrap"))
3018 (python-tests-look-at "return wwrap")
3019 (should (string= (python-info-current-defun)
3020 "decoratorFunctionWithArguments"))
3021 (should (string= (python-info-current-defun t)
3022 "def decoratorFunctionWithArguments"))))
3024 (ert-deftest python-info-current-symbol-1
()
3025 (python-tests-with-temp-buffer
3035 (python-tests-look-at "self.c()")
3036 (should (string= "self.c" (python-info-current-symbol)))
3037 (should (string= "C.c" (python-info-current-symbol t
)))))
3039 (ert-deftest python-info-current-symbol-2
()
3040 (python-tests-with-temp-buffer
3052 (python-tests-look-at "self.c()")
3053 (should (string= "self.c" (python-info-current-symbol)))
3054 (should (string= "C.M.c" (python-info-current-symbol t
)))))
3056 (ert-deftest python-info-current-symbol-3
()
3057 "Keywords should not be considered symbols."
3058 :expected-result
:failed
3059 (python-tests-with-temp-buffer
3064 ;; FIXME: keywords are not symbols.
3065 (python-tests-look-at "class C")
3066 (should (not (python-info-current-symbol)))
3067 (should (not (python-info-current-symbol t
)))
3068 (python-tests-look-at "C(object)")
3069 (should (string= "C" (python-info-current-symbol)))
3070 (should (string= "class C" (python-info-current-symbol t
)))))
3072 (ert-deftest python-info-statement-starts-block-p-1
()
3073 (python-tests-with-temp-buffer
3075 def long_function_name(
3076 var_one, var_two, var_three,
3080 (python-tests-look-at "def long_function_name")
3081 (should (python-info-statement-starts-block-p))
3082 (python-tests-look-at "print (var_one)")
3083 (python-util-forward-comment -
1)
3084 (should (python-info-statement-starts-block-p))))
3086 (ert-deftest python-info-statement-starts-block-p-2
()
3087 (python-tests-with-temp-buffer
3089 if width == 0 and height == 0 and \\\\
3090 color == 'red' and emphasis == 'strong' or \\\\
3092 raise ValueError('sorry, you lose')
3094 (python-tests-look-at "if width == 0 and")
3095 (should (python-info-statement-starts-block-p))
3096 (python-tests-look-at "raise ValueError(")
3097 (python-util-forward-comment -
1)
3098 (should (python-info-statement-starts-block-p))))
3100 (ert-deftest python-info-statement-ends-block-p-1
()
3101 (python-tests-with-temp-buffer
3103 def long_function_name(
3104 var_one, var_two, var_three,
3108 (python-tests-look-at "print (var_one)")
3109 (should (python-info-statement-ends-block-p))))
3111 (ert-deftest python-info-statement-ends-block-p-2
()
3112 (python-tests-with-temp-buffer
3114 if width == 0 and height == 0 and \\\\
3115 color == 'red' and emphasis == 'strong' or \\\\
3122 (python-tests-look-at "raise ValueError(")
3123 (should (python-info-statement-ends-block-p))))
3125 (ert-deftest python-info-beginning-of-statement-p-1
()
3126 (python-tests-with-temp-buffer
3128 def long_function_name(
3129 var_one, var_two, var_three,
3133 (python-tests-look-at "def long_function_name")
3134 (should (python-info-beginning-of-statement-p))
3136 (should (not (python-info-beginning-of-statement-p)))
3137 (python-tests-look-at "print (var_one)")
3138 (should (python-info-beginning-of-statement-p))
3139 (goto-char (line-beginning-position))
3140 (should (not (python-info-beginning-of-statement-p)))))
3142 (ert-deftest python-info-beginning-of-statement-p-2
()
3143 (python-tests-with-temp-buffer
3145 if width == 0 and height == 0 and \\\\
3146 color == 'red' and emphasis == 'strong' or \\\\
3153 (python-tests-look-at "if width == 0 and")
3154 (should (python-info-beginning-of-statement-p))
3156 (should (not (python-info-beginning-of-statement-p)))
3157 (python-tests-look-at "raise ValueError(")
3158 (should (python-info-beginning-of-statement-p))
3159 (goto-char (line-beginning-position))
3160 (should (not (python-info-beginning-of-statement-p)))))
3162 (ert-deftest python-info-end-of-statement-p-1
()
3163 (python-tests-with-temp-buffer
3165 def long_function_name(
3166 var_one, var_two, var_three,
3170 (python-tests-look-at "def long_function_name")
3171 (should (not (python-info-end-of-statement-p)))
3173 (should (not (python-info-end-of-statement-p)))
3174 (python-tests-look-at "print (var_one)")
3175 (python-util-forward-comment -
1)
3176 (should (python-info-end-of-statement-p))
3177 (python-tests-look-at "print (var_one)")
3178 (should (not (python-info-end-of-statement-p)))
3180 (should (python-info-end-of-statement-p))))
3182 (ert-deftest python-info-end-of-statement-p-2
()
3183 (python-tests-with-temp-buffer
3185 if width == 0 and height == 0 and \\\\
3186 color == 'red' and emphasis == 'strong' or \\\\
3193 (python-tests-look-at "if width == 0 and")
3194 (should (not (python-info-end-of-statement-p)))
3196 (should (not (python-info-end-of-statement-p)))
3197 (python-tests-look-at "raise ValueError(")
3198 (python-util-forward-comment -
1)
3199 (should (python-info-end-of-statement-p))
3200 (python-tests-look-at "raise ValueError(")
3201 (should (not (python-info-end-of-statement-p)))
3203 (should (not (python-info-end-of-statement-p)))
3204 (goto-char (point-max))
3205 (python-util-forward-comment -
1)
3206 (should (python-info-end-of-statement-p))))
3208 (ert-deftest python-info-beginning-of-block-p-1
()
3209 (python-tests-with-temp-buffer
3211 def long_function_name(
3212 var_one, var_two, var_three,
3216 (python-tests-look-at "def long_function_name")
3217 (should (python-info-beginning-of-block-p))
3218 (python-tests-look-at "var_one, var_two, var_three,")
3219 (should (not (python-info-beginning-of-block-p)))
3220 (python-tests-look-at "print (var_one)")
3221 (should (not (python-info-beginning-of-block-p)))))
3223 (ert-deftest python-info-beginning-of-block-p-2
()
3224 (python-tests-with-temp-buffer
3226 if width == 0 and height == 0 and \\\\
3227 color == 'red' and emphasis == 'strong' or \\\\
3234 (python-tests-look-at "if width == 0 and")
3235 (should (python-info-beginning-of-block-p))
3236 (python-tests-look-at "color == 'red' and emphasis")
3237 (should (not (python-info-beginning-of-block-p)))
3238 (python-tests-look-at "raise ValueError(")
3239 (should (not (python-info-beginning-of-block-p)))))
3241 (ert-deftest python-info-end-of-block-p-1
()
3242 (python-tests-with-temp-buffer
3244 def long_function_name(
3245 var_one, var_two, var_three,
3249 (python-tests-look-at "def long_function_name")
3250 (should (not (python-info-end-of-block-p)))
3251 (python-tests-look-at "var_one, var_two, var_three,")
3252 (should (not (python-info-end-of-block-p)))
3253 (python-tests-look-at "var_four):")
3255 (should (not (python-info-end-of-block-p)))
3256 (python-tests-look-at "print (var_one)")
3257 (should (not (python-info-end-of-block-p)))
3259 (should (python-info-end-of-block-p))))
3261 (ert-deftest python-info-end-of-block-p-2
()
3262 (python-tests-with-temp-buffer
3264 if width == 0 and height == 0 and \\\\
3265 color == 'red' and emphasis == 'strong' or \\\\
3272 (python-tests-look-at "if width == 0 and")
3273 (should (not (python-info-end-of-block-p)))
3274 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3275 (should (not (python-info-end-of-block-p)))
3276 (python-tests-look-at "highlight > 100:")
3278 (should (not (python-info-end-of-block-p)))
3279 (python-tests-look-at "raise ValueError(")
3280 (should (not (python-info-end-of-block-p)))
3282 (should (not (python-info-end-of-block-p)))
3283 (goto-char (point-max))
3284 (python-util-forward-comment -
1)
3285 (should (python-info-end-of-block-p))))
3287 (ert-deftest python-info-dedenter-opening-block-position-1
()
3288 (python-tests-with-temp-buffer
3290 if request.user.is_authenticated():
3292 profile = request.user.get_profile()
3293 except Profile.DoesNotExist:
3294 profile = Profile.objects.create(user=request.user)
3297 profile.recalculate_stats()
3299 profile.clear_stats()
3304 (python-tests-look-at "try:")
3305 (should (not (python-info-dedenter-opening-block-position)))
3306 (python-tests-look-at "except Profile.DoesNotExist:")
3307 (should (= (python-tests-look-at "try:" -
1 t
)
3308 (python-info-dedenter-opening-block-position)))
3309 (python-tests-look-at "else:")
3310 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -
1 t
)
3311 (python-info-dedenter-opening-block-position)))
3312 (python-tests-look-at "if profile.stats:")
3313 (should (not (python-info-dedenter-opening-block-position)))
3314 (python-tests-look-at "else:")
3315 (should (= (python-tests-look-at "if profile.stats:" -
1 t
)
3316 (python-info-dedenter-opening-block-position)))
3317 (python-tests-look-at "finally:")
3318 (should (= (python-tests-look-at "else:" -
2 t
)
3319 (python-info-dedenter-opening-block-position)))))
3321 (ert-deftest python-info-dedenter-opening-block-position-2
()
3322 (python-tests-with-temp-buffer
3324 if request.user.is_authenticated():
3325 profile = Profile.objects.get_or_create(user=request.user)
3327 profile.recalculate_stats()
3334 (python-tests-look-at "'else': 'do it'")
3335 (should (not (python-info-dedenter-opening-block-position)))
3336 (python-tests-look-at "'else'")
3337 (should (not (python-info-dedenter-opening-block-position)))))
3339 (ert-deftest python-info-dedenter-opening-block-position-3
()
3340 (python-tests-with-temp-buffer
3346 msg = 'Error saving to disk'
3348 logger.exception(msg)
3351 logger.exception('Unhandled exception')
3356 (python-tests-look-at "try:")
3357 (should (not (python-info-dedenter-opening-block-position)))
3359 (python-tests-look-at "except IOError:")
3360 (should (= (python-tests-look-at "try:" -
1 t
)
3361 (python-info-dedenter-opening-block-position)))
3363 (python-tests-look-at "except Exception:")
3364 (should (= (python-tests-look-at "except IOError:" -
1 t
)
3365 (python-info-dedenter-opening-block-position)))
3367 (python-tests-look-at "if hide_details:")
3368 (should (not (python-info-dedenter-opening-block-position)))
3370 ;; check indentation modifies the detected opening block
3371 (python-tests-look-at "else")
3372 (should (= (python-tests-look-at "if hide_details:" -
1 t
)
3373 (python-info-dedenter-opening-block-position)))
3376 (should (= (python-tests-look-at "if hide_details:" -
1 t
)
3377 (python-info-dedenter-opening-block-position)))
3380 (should (= (python-tests-look-at "except Exception:" -
1 t
)
3381 (python-info-dedenter-opening-block-position)))
3384 (should (= (python-tests-look-at "if save:" -
1 t
)
3385 (python-info-dedenter-opening-block-position)))))
3387 (ert-deftest python-info-dedenter-opening-block-positions-1
()
3388 (python-tests-with-temp-buffer
3394 msg = 'Error saving to disk'
3396 logger.exception(msg)
3399 logger.exception('Unhandled exception')
3404 (python-tests-look-at "try:")
3405 (should (not (python-info-dedenter-opening-block-positions)))
3407 (python-tests-look-at "except IOError:")
3410 (python-tests-look-at "try:" -
1 t
))
3411 (python-info-dedenter-opening-block-positions)))
3413 (python-tests-look-at "except Exception:")
3416 (python-tests-look-at "except IOError:" -
1 t
))
3417 (python-info-dedenter-opening-block-positions)))
3419 (python-tests-look-at "if hide_details:")
3420 (should (not (python-info-dedenter-opening-block-positions)))
3422 ;; check indentation does not modify the detected opening blocks
3423 (python-tests-look-at "else")
3426 (python-tests-look-at "if hide_details:" -
1 t
)
3427 (python-tests-look-at "except Exception:" -
1 t
)
3428 (python-tests-look-at "if save:" -
1 t
))
3429 (python-info-dedenter-opening-block-positions)))
3434 (python-tests-look-at "if hide_details:" -
1 t
)
3435 (python-tests-look-at "except Exception:" -
1 t
)
3436 (python-tests-look-at "if save:" -
1 t
))
3437 (python-info-dedenter-opening-block-positions)))
3442 (python-tests-look-at "if hide_details:" -
1 t
)
3443 (python-tests-look-at "except Exception:" -
1 t
)
3444 (python-tests-look-at "if save:" -
1 t
))
3445 (python-info-dedenter-opening-block-positions)))
3450 (python-tests-look-at "if hide_details:" -
1 t
)
3451 (python-tests-look-at "except Exception:" -
1 t
)
3452 (python-tests-look-at "if save:" -
1 t
))
3453 (python-info-dedenter-opening-block-positions)))))
3455 (ert-deftest python-info-dedenter-opening-block-positions-2
()
3456 "Test detection of opening blocks for elif."
3457 (python-tests-with-temp-buffer
3466 (python-tests-look-at "elif var3:")
3469 (python-tests-look-at "if var2:" -
1 t
)
3470 (python-tests-look-at "if var:" -
1 t
))
3471 (python-info-dedenter-opening-block-positions)))
3473 (python-tests-look-at "elif\n")
3476 (python-tests-look-at "elif var3:" -
1 t
)
3477 (python-tests-look-at "if var:" -
1 t
))
3478 (python-info-dedenter-opening-block-positions)))))
3480 (ert-deftest python-info-dedenter-opening-block-positions-3
()
3481 "Test detection of opening blocks for else."
3482 (python-tests-with-temp-buffer
3504 (python-tests-look-at "else\n")
3507 (python-tests-look-at "elif var3:" -
1 t
)
3508 (python-tests-look-at "if var:" -
1 t
)
3509 (python-tests-look-at "except:" -
1 t
))
3510 (python-info-dedenter-opening-block-positions)))
3512 (python-tests-look-at "else\n")
3515 (python-tests-look-at "while var5:" -
1 t
)
3516 (python-tests-look-at "if var4:" -
1 t
))
3517 (python-info-dedenter-opening-block-positions)))
3519 (python-tests-look-at "else\n")
3522 (python-tests-look-at "if value > 0:" -
1 t
)
3523 (python-tests-look-at "for value in var6:" -
1 t
)
3524 (python-tests-look-at "if var4:" -
1 t
))
3525 (python-info-dedenter-opening-block-positions)))))
3527 (ert-deftest python-info-dedenter-opening-block-positions-4
()
3528 "Test detection of opening blocks for except."
3529 (python-tests-with-temp-buffer
3537 (python-tests-look-at "except ValueError:")
3539 (equal (list (python-tests-look-at "try:" -
1 t
))
3540 (python-info-dedenter-opening-block-positions)))
3542 (python-tests-look-at "except\n")
3544 (equal (list (python-tests-look-at "except ValueError:" -
1 t
))
3545 (python-info-dedenter-opening-block-positions)))))
3547 (ert-deftest python-info-dedenter-opening-block-positions-5
()
3548 "Test detection of opening blocks for finally."
3549 (python-tests-with-temp-buffer
3558 logger.exception('something went wrong')
3562 something_else_else()
3564 logger.exception('something else went wrong')
3569 (python-tests-look-at "finally\n")
3571 (equal (list (python-tests-look-at "try:" -
1 t
))
3572 (python-info-dedenter-opening-block-positions)))
3574 (python-tests-look-at "finally\n")
3576 (equal (list (python-tests-look-at "except:" -
1 t
))
3577 (python-info-dedenter-opening-block-positions)))
3579 (python-tests-look-at "finally\n")
3581 (equal (list (python-tests-look-at "else:" -
1 t
))
3582 (python-info-dedenter-opening-block-positions)))))
3584 (ert-deftest python-info-dedenter-opening-block-message-1
()
3585 "Test dedenters inside strings are ignored."
3586 (python-tests-with-temp-buffer
3591 logger.exception('something went wrong')
3594 (python-tests-look-at "except\n")
3595 (should (not (python-info-dedenter-opening-block-message)))))
3597 (ert-deftest python-info-dedenter-opening-block-message-2
()
3598 "Test except keyword."
3599 (python-tests-with-temp-buffer
3604 logger.exception('something went wrong')
3606 (python-tests-look-at "except:")
3609 (substring-no-properties
3610 (python-info-dedenter-opening-block-message))))
3614 (substring-no-properties
3615 (python-info-dedenter-opening-block-message))))))
3617 (ert-deftest python-info-dedenter-opening-block-message-3
()
3618 "Test else keyword."
3619 (python-tests-with-temp-buffer
3624 logger.exception('something went wrong')
3626 logger.debug('all good')
3628 (python-tests-look-at "else:")
3631 (substring-no-properties
3632 (python-info-dedenter-opening-block-message))))
3636 (substring-no-properties
3637 (python-info-dedenter-opening-block-message))))))
3639 (ert-deftest python-info-dedenter-opening-block-message-4
()
3640 "Test finally keyword."
3641 (python-tests-with-temp-buffer
3646 logger.exception('something went wrong')
3648 logger.debug('all good')
3652 (python-tests-look-at "finally:")
3655 (substring-no-properties
3656 (python-info-dedenter-opening-block-message))))
3660 (substring-no-properties
3661 (python-info-dedenter-opening-block-message))))))
3663 (ert-deftest python-info-dedenter-opening-block-message-5
()
3664 "Test elif keyword."
3665 (python-tests-with-temp-buffer
3671 (python-tests-look-at "elif b:")
3674 (substring-no-properties
3675 (python-info-dedenter-opening-block-message))))
3679 (substring-no-properties
3680 (python-info-dedenter-opening-block-message))))))
3683 (ert-deftest python-info-dedenter-statement-p-1
()
3684 "Test dedenters inside strings are ignored."
3685 (python-tests-with-temp-buffer
3690 logger.exception('something went wrong')
3693 (python-tests-look-at "except\n")
3694 (should (not (python-info-dedenter-statement-p)))))
3696 (ert-deftest python-info-dedenter-statement-p-2
()
3697 "Test except keyword."
3698 (python-tests-with-temp-buffer
3703 logger.exception('something went wrong')
3705 (python-tests-look-at "except:")
3706 (should (= (point) (python-info-dedenter-statement-p)))
3708 (should (= (save-excursion
3709 (back-to-indentation)
3711 (python-info-dedenter-statement-p)))))
3713 (ert-deftest python-info-dedenter-statement-p-3
()
3714 "Test else keyword."
3715 (python-tests-with-temp-buffer
3720 logger.exception('something went wrong')
3722 logger.debug('all good')
3724 (python-tests-look-at "else:")
3725 (should (= (point) (python-info-dedenter-statement-p)))
3727 (should (= (save-excursion
3728 (back-to-indentation)
3730 (python-info-dedenter-statement-p)))))
3732 (ert-deftest python-info-dedenter-statement-p-4
()
3733 "Test finally keyword."
3734 (python-tests-with-temp-buffer
3739 logger.exception('something went wrong')
3741 logger.debug('all good')
3745 (python-tests-look-at "finally:")
3746 (should (= (point) (python-info-dedenter-statement-p)))
3748 (should (= (save-excursion
3749 (back-to-indentation)
3751 (python-info-dedenter-statement-p)))))
3753 (ert-deftest python-info-dedenter-statement-p-5
()
3754 "Test elif keyword."
3755 (python-tests-with-temp-buffer
3761 (python-tests-look-at "elif b:")
3762 (should (= (point) (python-info-dedenter-statement-p)))
3764 (should (= (save-excursion
3765 (back-to-indentation)
3767 (python-info-dedenter-statement-p)))))
3769 (ert-deftest python-info-line-ends-backslash-p-1
()
3770 (python-tests-with-temp-buffer
3772 objects = Thing.objects.all() \\\\
3782 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
3783 (should (python-info-line-ends-backslash-p 3))
3784 (should (python-info-line-ends-backslash-p 4))
3785 (should (python-info-line-ends-backslash-p 5))
3786 (should (python-info-line-ends-backslash-p 6)) ; ) \...
3787 (should (python-info-line-ends-backslash-p 7))
3788 (should (python-info-line-ends-backslash-p 8))
3789 (should (python-info-line-ends-backslash-p 9))
3790 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
3792 (ert-deftest python-info-beginning-of-backslash-1
()
3793 (python-tests-with-temp-buffer
3795 objects = Thing.objects.all() \\\\
3806 (second (python-tests-look-at ".filter("))
3807 (third (python-tests-look-at ".aggregate(")))
3808 (should (= first
(python-info-beginning-of-backslash 2)))
3809 (should (= second
(python-info-beginning-of-backslash 3)))
3810 (should (= second
(python-info-beginning-of-backslash 4)))
3811 (should (= second
(python-info-beginning-of-backslash 5)))
3812 (should (= second
(python-info-beginning-of-backslash 6)))
3813 (should (= third
(python-info-beginning-of-backslash 7)))
3814 (should (= third
(python-info-beginning-of-backslash 8)))
3815 (should (= third
(python-info-beginning-of-backslash 9)))
3816 (should (not (python-info-beginning-of-backslash 10))))))
3818 (ert-deftest python-info-continuation-line-p-1
()
3819 (python-tests-with-temp-buffer
3821 if width == 0 and height == 0 and \\\\
3822 color == 'red' and emphasis == 'strong' or \\\\
3829 (python-tests-look-at "if width == 0 and height == 0 and")
3830 (should (not (python-info-continuation-line-p)))
3831 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3832 (should (python-info-continuation-line-p))
3833 (python-tests-look-at "highlight > 100:")
3834 (should (python-info-continuation-line-p))
3835 (python-tests-look-at "raise ValueError(")
3836 (should (not (python-info-continuation-line-p)))
3837 (python-tests-look-at "'sorry, you lose'")
3838 (should (python-info-continuation-line-p))
3840 (should (python-info-continuation-line-p))
3841 (python-tests-look-at ")")
3842 (should (python-info-continuation-line-p))
3844 (should (not (python-info-continuation-line-p)))))
3846 (ert-deftest python-info-block-continuation-line-p-1
()
3847 (python-tests-with-temp-buffer
3849 if width == 0 and height == 0 and \\\\
3850 color == 'red' and emphasis == 'strong' or \\\\
3857 (python-tests-look-at "if width == 0 and")
3858 (should (not (python-info-block-continuation-line-p)))
3859 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3860 (should (= (python-info-block-continuation-line-p)
3861 (python-tests-look-at "if width == 0 and" -
1 t
)))
3862 (python-tests-look-at "highlight > 100:")
3863 (should (not (python-info-block-continuation-line-p)))))
3865 (ert-deftest python-info-block-continuation-line-p-2
()
3866 (python-tests-with-temp-buffer
3873 (python-tests-look-at "def foo(a,")
3874 (should (not (python-info-block-continuation-line-p)))
3875 (python-tests-look-at "b,")
3876 (should (= (python-info-block-continuation-line-p)
3877 (python-tests-look-at "def foo(a," -
1 t
)))
3878 (python-tests-look-at "c):")
3879 (should (not (python-info-block-continuation-line-p)))))
3881 (ert-deftest python-info-assignment-continuation-line-p-1
()
3882 (python-tests-with-temp-buffer
3884 data = foo(), bar() \\\\
3888 (python-tests-look-at "data = foo(), bar()")
3889 (should (not (python-info-assignment-continuation-line-p)))
3890 (python-tests-look-at "baz(), 4")
3891 (should (= (python-info-assignment-continuation-line-p)
3892 (python-tests-look-at "foo()," -
1 t
)))
3893 (python-tests-look-at "5, 6")
3894 (should (not (python-info-assignment-continuation-line-p)))))
3896 (ert-deftest python-info-assignment-continuation-line-p-2
()
3897 (python-tests-with-temp-buffer
3899 data = (foo(), bar()
3903 (python-tests-look-at "data = (foo(), bar()")
3904 (should (not (python-info-assignment-continuation-line-p)))
3905 (python-tests-look-at "baz(), 4")
3906 (should (= (python-info-assignment-continuation-line-p)
3907 (python-tests-look-at "(foo()," -
1 t
)))
3908 (python-tests-look-at "5, 6)")
3909 (should (not (python-info-assignment-continuation-line-p)))))
3911 (ert-deftest python-info-looking-at-beginning-of-defun-1
()
3912 (python-tests-with-temp-buffer
3914 def decorat0r(deff):
3925 (python-tests-look-at "def decorat0r(deff):")
3926 (should (python-info-looking-at-beginning-of-defun))
3927 (python-tests-look-at "def foo(arg):")
3928 (should (not (python-info-looking-at-beginning-of-defun)))
3929 (python-tests-look-at "def wrap():")
3930 (should (python-info-looking-at-beginning-of-defun))
3931 (python-tests-look-at "deff()")
3932 (should (not (python-info-looking-at-beginning-of-defun)))))
3934 (ert-deftest python-info-current-line-comment-p-1
()
3935 (python-tests-with-temp-buffer
3938 foo = True # another comment
3942 print ('bar') # print bar
3944 (python-tests-look-at "# this is a comment")
3945 (should (python-info-current-line-comment-p))
3946 (python-tests-look-at "foo = True # another comment")
3947 (should (not (python-info-current-line-comment-p)))
3948 (python-tests-look-at "'#this is a string'")
3949 (should (not (python-info-current-line-comment-p)))
3950 (python-tests-look-at "# more comments")
3951 (should (python-info-current-line-comment-p))
3952 (python-tests-look-at "print ('bar') # print bar")
3953 (should (not (python-info-current-line-comment-p)))))
3955 (ert-deftest python-info-current-line-empty-p
()
3956 (python-tests-with-temp-buffer
3960 foo = True # another comment
3962 (should (python-info-current-line-empty-p))
3963 (python-tests-look-at "# this is a comment")
3964 (should (not (python-info-current-line-empty-p)))
3966 (should (python-info-current-line-empty-p))))
3968 (ert-deftest python-info-encoding-from-cookie-1
()
3969 "Should detect it on first line."
3970 (python-tests-with-temp-buffer
3973 foo = True # another comment
3975 (should (eq (python-info-encoding-from-cookie) 'latin-1
))))
3977 (ert-deftest python-info-encoding-from-cookie-2
()
3978 "Should detect it on second line."
3979 (python-tests-with-temp-buffer
3983 foo = True # another comment
3985 (should (eq (python-info-encoding-from-cookie) 'latin-1
))))
3987 (ert-deftest python-info-encoding-from-cookie-3
()
3988 "Should not be detected on third line (and following ones)."
3989 (python-tests-with-temp-buffer
3993 foo = True # another comment
3995 (should (not (python-info-encoding-from-cookie)))))
3997 (ert-deftest python-info-encoding-from-cookie-4
()
3998 "Should detect Emacs style."
3999 (python-tests-with-temp-buffer
4000 "# -*- coding: latin-1 -*-
4002 foo = True # another comment"
4003 (should (eq (python-info-encoding-from-cookie) 'latin-1
))))
4005 (ert-deftest python-info-encoding-from-cookie-5
()
4006 "Should detect Vim style."
4007 (python-tests-with-temp-buffer
4008 "# vim: set fileencoding=latin-1 :
4010 foo = True # another comment"
4011 (should (eq (python-info-encoding-from-cookie) 'latin-1
))))
4013 (ert-deftest python-info-encoding-from-cookie-6
()
4014 "First cookie wins."
4015 (python-tests-with-temp-buffer
4016 "# -*- coding: iso-8859-1 -*-
4017 # vim: set fileencoding=latin-1 :
4019 foo = True # another comment"
4020 (should (eq (python-info-encoding-from-cookie) 'iso-8859-1
))))
4022 (ert-deftest python-info-encoding-from-cookie-7
()
4023 "First cookie wins."
4024 (python-tests-with-temp-buffer
4025 "# vim: set fileencoding=latin-1 :
4026 # -*- coding: iso-8859-1 -*-
4028 foo = True # another comment"
4029 (should (eq (python-info-encoding-from-cookie) 'latin-1
))))
4031 (ert-deftest python-info-encoding-1
()
4032 "Should return the detected encoding from cookie."
4033 (python-tests-with-temp-buffer
4034 "# vim: set fileencoding=latin-1 :
4036 foo = True # another comment"
4037 (should (eq (python-info-encoding) 'latin-1
))))
4039 (ert-deftest python-info-encoding-2
()
4040 "Should default to utf-8."
4041 (python-tests-with-temp-buffer
4042 "# No encoding for you
4044 foo = True # another comment"
4045 (should (eq (python-info-encoding) 'utf-8
))))
4048 ;;; Utility functions
4050 (ert-deftest python-util-goto-line-1
()
4051 (python-tests-with-temp-buffer
4056 pass" (make-string 20 ?
\n))
4057 (python-util-goto-line 10)
4058 (should (= (line-number-at-pos) 10))
4059 (python-util-goto-line 20)
4060 (should (= (line-number-at-pos) 20))))
4062 (ert-deftest python-util-clone-local-variables-1
()
4063 (let ((buffer (generate-new-buffer
4064 "python-util-clone-local-variables-1"))
4066 '((python-fill-docstring-style . django
)
4067 (python-shell-interpreter .
"python")
4068 (python-shell-interpreter-args .
"manage.py shell")
4069 (python-shell-prompt-regexp .
"In \\[[0-9]+\\]: ")
4070 (python-shell-prompt-output-regexp .
"Out\\[[0-9]+\\]: ")
4071 (python-shell-extra-pythonpaths "/home/user/pylib/")
4072 (python-shell-completion-setup-code
4073 .
"from IPython.core.completerlib import module_completion")
4074 (python-shell-completion-string-code
4075 .
"';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
4076 (python-shell-virtualenv-path
4077 .
"/home/user/.virtualenvs/project"))))
4078 (with-current-buffer buffer
4079 (kill-all-local-variables)
4080 (dolist (ccons varcons
)
4081 (set (make-local-variable (car ccons
)) (cdr ccons
))))
4082 (python-tests-with-temp-buffer
4084 (python-util-clone-local-variables buffer
)
4085 (dolist (ccons varcons
)
4087 (equal (symbol-value (car ccons
)) (cdr ccons
)))))
4088 (kill-buffer buffer
)))
4090 (ert-deftest python-util-strip-string-1
()
4091 (should (string= (python-util-strip-string "\t\r\n str") "str"))
4092 (should (string= (python-util-strip-string "str \n\r") "str"))
4093 (should (string= (python-util-strip-string "\t\r\n str \n\r ") "str"))
4095 (string= (python-util-strip-string "\n str \nin \tg \n\r") "str \nin \tg"))
4096 (should (string= (python-util-strip-string "\n \t \n\r ") ""))
4097 (should (string= (python-util-strip-string "") "")))
4099 (ert-deftest python-util-forward-comment-1
()
4100 (python-tests-with-temp-buffer
4104 # bad indented comment
4105 # more comments" (make-string 9999 ?
\n))
4106 (python-util-forward-comment 1)
4107 (should (= (point) (point-max)))
4108 (python-util-forward-comment -
1)
4109 (should (= (point) (point-min)))))
4111 (ert-deftest python-util-valid-regexp-p-1
()
4112 (should (python-util-valid-regexp-p ""))
4113 (should (python-util-valid-regexp-p python-shell-prompt-regexp
))
4114 (should (not (python-util-valid-regexp-p "\\("))))
4119 (ert-deftest python-parens-electric-indent-1
()
4121 (let ((eim electric-indent-mode
))
4124 (python-tests-with-temp-buffer
4126 from django.conf.urls import patterns, include, url
4128 from django.contrib import admin
4130 from myapp import views
4133 urlpatterns = patterns('',
4134 url(r'^$', views.index
4137 (electric-indent-mode 1)
4138 (python-tests-look-at "views.index")
4141 ;; Inserting commas within the same line should leave
4142 ;; indentation unchanged.
4143 (python-tests-self-insert ",")
4144 (should (= (current-indentation) 4))
4146 ;; As well as any other input happening within the same
4148 (python-tests-self-insert " name='index')")
4149 (should (= (current-indentation) 4))
4151 ;; But a comma outside it, should trigger indentation.
4152 (python-tests-self-insert ",")
4153 (should (= (current-indentation) 23))
4155 ;; Newline indents to the first argument column
4156 (python-tests-self-insert "\n")
4157 (should (= (current-indentation) 23))
4159 ;; All this input must not change indentation
4161 (python-tests-self-insert "url(r'^/login$', views.login)")
4162 (should (= (current-indentation) 4))
4164 ;; But this comma does
4165 (python-tests-self-insert ",")
4166 (should (= (current-indentation) 23))))
4167 (or eim
(electric-indent-mode -
1)))))
4169 (ert-deftest python-triple-quote-pairing
()
4171 (let ((epm electric-pair-mode
))
4174 (python-tests-with-temp-buffer
4176 (or epm
(electric-pair-mode 1))
4177 (goto-char (1- (point-max)))
4178 (python-tests-self-insert ?
\")
4179 (should (string= (buffer-string)
4181 (should (= (point) 4)))
4182 (python-tests-with-temp-buffer
4184 (python-tests-self-insert (list ?
\" ?
\" ?
\"))
4185 (should (string= (buffer-string)
4187 (should (= (point) 4)))
4188 (python-tests-with-temp-buffer
4190 (goto-char (1- (point-max)))
4191 (python-tests-self-insert ?
\")
4192 (should (= (point) (1- (point-max))))
4193 (should (string= (buffer-string)
4195 (or epm
(electric-pair-mode -
1)))))
4198 (provide 'python-tests
)
4202 ;; indent-tabs-mode: nil
4205 ;;; python-tests.el ends here