Make python.el work with IPython automatically.
[emacs.git] / test / automated / python-tests.el
blob81f4567091e242ca16c580666a009e575275be0e
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/>.
20 ;;; Commentary:
22 ;;; Code:
24 (require 'ert)
25 (require 'python)
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))
32 `(with-temp-buffer
33 (python-mode)
34 (insert ,contents)
35 (goto-char (point-min))
36 ,@body))
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)))
46 (unwind-protect
47 (with-current-buffer buffer
48 (python-mode)
49 (insert ,contents)
50 (goto-char (point-min))
51 ,@body)
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+))
68 (found-point))
69 (prog2
70 (catch 'exit
71 (while (not (= num 0))
72 (when (and (> num 0)
73 (looking-at string))
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))
77 (throw 'exit t))
78 (when (> num 0)
79 ;; `re-search-forward' leaves point at the end of the
80 ;; occurrence, move back so point is at the beginning
81 ;; instead.
82 (forward-char (- (length (match-string-no-properties 0)))))
83 (setq
84 num (funcall deinc-fn num)
85 found-point (point))))
86 found-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."
91 (let ((chars
92 (cond
93 ((characterp char-or-str)
94 (list char-or-str))
95 ((stringp char-or-str)
96 (string-to-list char-or-str))
97 ((not
98 (cl-remove-if #'characterp char-or-str))
99 char-or-str)
100 (t (error "CHAR-OR-STR must be a char, string, or list of char")))))
101 (mapc
102 (lambda (char)
103 (let ((last-command-event char))
104 (call-interactively 'self-insert-command)))
105 chars)))
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
115 aliqua."
116 (let ((expected (save-excursion
117 (dotimes (i 3)
118 (re-search-forward "et" nil t))
119 (forward-char -2)
120 (point))))
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"))
127 (forward-char -2)
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
135 aliqua."
136 (let ((expected
137 (save-excursion
138 (re-search-forward "et" nil t)
139 (forward-char -2)
140 (point))))
141 (dotimes (i 3)
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)))))
147 ;;; Bindings
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
159 "\"\"\""
160 (goto-char (point-max))
161 (python-indent-dedent-line-backspace 1)
162 (should (string= (buffer-string) "\"\""))
163 (should (null (nth 3 (syntax-ppss))))))
166 ;;; Indentation
168 ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
170 (ert-deftest python-indent-pep8-1 ()
171 "First pep8 case."
172 (python-tests-with-temp-buffer
173 "# Aligned with opening delimiter
174 foo = long_function_name(var_one, var_two,
175 var_three, var_four)
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 ()
187 "Second pep8 case."
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,
192 var_four):
193 print (var_one)
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 ()
211 "Third pep8 case."
212 (python-tests-with-temp-buffer
213 "# Extra indentation is not necessary.
214 foo = long_function_name(
215 var_one, var_two,
216 var_three, var_four)
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
234 class Blag(object):
235 def _on_child_complete(self, child_future):
236 if self.in_terminal_state():
237 pass
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))
256 (python-indent-line)
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
267 def func(arg):
268 # I don't do much
269 return arg
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
275 # yeah, that.
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.
295 (forward-line 1)
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
311 data = {
312 'key':
314 'objlist': [
316 'pk': 1,
317 'name': 'first',
320 'pk': 2,
321 'name': 'second',
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
377 data = {'key': {
378 'objlist': [
379 {'pk': 1,
380 'name': 'first'},
381 {'pk': 2,
382 'name': 'second'}
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
430 def foo(a, b, c={
431 'a':
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() \\\\
464 .filter(
465 type='toy',
466 status='bought'
467 ) \\\\
468 .aggregate(
469 Sum('amount')
470 ) \\\\
471 .values_list()
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))
500 (forward-line 1)
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
508 Class foo(object):
510 def bar(self):
511 if self.baz:
512 return (1,
516 else:
517 pass
519 (python-tests-look-at "3)")
520 (forward-line 1)
521 (should (= (python-indent-calculate-indentation) 8))
522 (python-tests-look-at "pass")
523 (forward-line 1)
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
530 Class foo(object):
531 '''raise lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
533 eiusmod tempor incididunt ut labore et dolore magna aliqua.
535 def bar(self):
536 \"return (1, 2, 3).\"
537 if self.baz:
538 return (1,
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
555 for element in lst:
556 if element is None:
557 continue
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))
563 (forward-line 1)
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
570 for element in lst:
571 if element is None:
572 break
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))
578 (forward-line 1)
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
585 for element in lst:
586 if element is None:
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))
593 (forward-line 1)
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
600 if save:
601 try:
602 write_to_disk(data)
603 finally:
604 cleanup()
605 elif
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
616 if save:
617 try:
618 write_to_disk(data)
619 except IOError:
620 msg = 'Error saving to disk'
621 message(msg)
622 logger.exception(msg)
623 except Exception:
624 if hide_details:
625 logger.exception('Unhandled exception')
626 else
627 finally:
628 data.free()
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
639 if save:
640 try:
641 write_to_disk(data)
642 except
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
653 if save:
654 try:
655 write_to_disk(data)
656 finally
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
667 if save:
668 try:
669 write_to_disk(data)
670 except IOError:
671 msg = 'Error saving to disk'
672 message(msg)
673 logger.exception(msg)
674 finally:
675 if cleanup:
676 do_cleanup()
677 else
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
688 try:
689 # if save:
690 write_to_disk(data)
691 else
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
702 if a:
703 if b:
704 pass
705 else:
706 pass
707 else:
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)))))
715 ;;; Navigation
717 (ert-deftest python-nav-beginning-of-defun-1 ()
718 (python-tests-with-temp-buffer
720 def decoratorFunctionWithArguments(arg1, arg2, arg3):
721 '''print decorated function call data to stdout.
723 Usage:
725 @decoratorFunctionWithArguments('arg1', 'arg2')
726 def func(a, b, c=True):
727 pass
730 def wwrap(f):
731 print 'Inside wwrap()'
732 def wrapped_f(*args):
733 print 'Inside wrapped_f()'
734 print 'Decorator arguments:', arg1, arg2, arg3
735 f(*args)
736 print 'After f(*args)'
737 return wrapped_f
738 return wwrap
740 (python-tests-look-at "return wrap")
741 (should (= (save-excursion
742 (python-nav-beginning-of-defun)
743 (point))
744 (save-excursion
745 (python-tests-look-at "def wrapped_f(*args):" -1)
746 (beginning-of-line)
747 (point))))
748 (python-tests-look-at "def wrapped_f(*args):" -1)
749 (should (= (save-excursion
750 (python-nav-beginning-of-defun)
751 (point))
752 (save-excursion
753 (python-tests-look-at "def wwrap(f):" -1)
754 (beginning-of-line)
755 (point))))
756 (python-tests-look-at "def wwrap(f):" -1)
757 (should (= (save-excursion
758 (python-nav-beginning-of-defun)
759 (point))
760 (save-excursion
761 (python-tests-look-at "def decoratorFunctionWithArguments" -1)
762 (beginning-of-line)
763 (point))))))
765 (ert-deftest python-nav-beginning-of-defun-2 ()
766 (python-tests-with-temp-buffer
768 class C(object):
770 def m(self):
771 self.c()
773 def b():
774 pass
776 def a():
777 pass
779 def c(self):
780 pass
782 ;; Nested defuns, are handled with care.
783 (python-tests-look-at "def c(self):")
784 (should (= (save-excursion
785 (python-nav-beginning-of-defun)
786 (point))
787 (save-excursion
788 (python-tests-look-at "def m(self):" -1)
789 (beginning-of-line)
790 (point))))
791 ;; Defuns on same levels should be respected.
792 (python-tests-look-at "def a():" -1)
793 (should (= (save-excursion
794 (python-nav-beginning-of-defun)
795 (point))
796 (save-excursion
797 (python-tests-look-at "def b():" -1)
798 (beginning-of-line)
799 (point))))
800 ;; Jump to a top level defun.
801 (python-tests-look-at "def b():" -1)
802 (should (= (save-excursion
803 (python-nav-beginning-of-defun)
804 (point))
805 (save-excursion
806 (python-tests-look-at "def m(self):" -1)
807 (beginning-of-line)
808 (point))))
809 ;; Jump to a top level defun again.
810 (python-tests-look-at "def m(self):" -1)
811 (should (= (save-excursion
812 (python-nav-beginning-of-defun)
813 (point))
814 (save-excursion
815 (python-tests-look-at "class C(object):" -1)
816 (beginning-of-line)
817 (point))))))
819 (ert-deftest python-nav-end-of-defun-1 ()
820 (python-tests-with-temp-buffer
822 class C(object):
824 def m(self):
825 self.c()
827 def b():
828 pass
830 def a():
831 pass
833 def c(self):
834 pass
836 (should (= (save-excursion
837 (python-tests-look-at "class C(object):")
838 (python-nav-end-of-defun)
839 (point))
840 (save-excursion
841 (point-max))))
842 (should (= (save-excursion
843 (python-tests-look-at "def m(self):")
844 (python-nav-end-of-defun)
845 (point))
846 (save-excursion
847 (python-tests-look-at "def c(self):")
848 (forward-line -1)
849 (point))))
850 (should (= (save-excursion
851 (python-tests-look-at "def b():")
852 (python-nav-end-of-defun)
853 (point))
854 (save-excursion
855 (python-tests-look-at "def b():")
856 (forward-line 2)
857 (point))))
858 (should (= (save-excursion
859 (python-tests-look-at "def c(self):")
860 (python-nav-end-of-defun)
861 (point))
862 (save-excursion
863 (point-max))))))
865 (ert-deftest python-nav-end-of-defun-2 ()
866 (python-tests-with-temp-buffer
868 def decoratorFunctionWithArguments(arg1, arg2, arg3):
869 '''print decorated function call data to stdout.
871 Usage:
873 @decoratorFunctionWithArguments('arg1', 'arg2')
874 def func(a, b, c=True):
875 pass
878 def wwrap(f):
879 print 'Inside wwrap()'
880 def wrapped_f(*args):
881 print 'Inside wrapped_f()'
882 print 'Decorator arguments:', arg1, arg2, arg3
883 f(*args)
884 print 'After f(*args)'
885 return wrapped_f
886 return wwrap
888 (should (= (save-excursion
889 (python-tests-look-at "def decoratorFunctionWithArguments")
890 (python-nav-end-of-defun)
891 (point))
892 (save-excursion
893 (point-max))))
894 (should (= (save-excursion
895 (python-tests-look-at "@decoratorFunctionWithArguments")
896 (python-nav-end-of-defun)
897 (point))
898 (save-excursion
899 (point-max))))
900 (should (= (save-excursion
901 (python-tests-look-at "def wwrap(f):")
902 (python-nav-end-of-defun)
903 (point))
904 (save-excursion
905 (python-tests-look-at "return wwrap")
906 (line-beginning-position))))
907 (should (= (save-excursion
908 (python-tests-look-at "def wrapped_f(*args):")
909 (python-nav-end-of-defun)
910 (point))
911 (save-excursion
912 (python-tests-look-at "return wrapped_f")
913 (line-beginning-position))))
914 (should (= (save-excursion
915 (python-tests-look-at "f(*args)")
916 (python-nav-end-of-defun)
917 (point))
918 (save-excursion
919 (python-tests-look-at "return wrapped_f")
920 (line-beginning-position))))))
922 (ert-deftest python-nav-backward-defun-1 ()
923 (python-tests-with-temp-buffer
925 class A(object): # A
927 def a(self): # a
928 pass
930 def b(self): # b
931 pass
933 class B(object): # B
935 class C(object): # C
937 def d(self): # d
938 pass
940 # def e(self): # e
941 # pass
943 def c(self): # c
944 pass
946 # def d(self): # d
947 # pass
949 (goto-char (point-max))
950 (should (= (save-excursion (python-nav-backward-defun))
951 (python-tests-look-at " def c(self): # c" -1)))
952 (should (= (save-excursion (python-nav-backward-defun))
953 (python-tests-look-at " def d(self): # d" -1)))
954 (should (= (save-excursion (python-nav-backward-defun))
955 (python-tests-look-at " class C(object): # C" -1)))
956 (should (= (save-excursion (python-nav-backward-defun))
957 (python-tests-look-at " class B(object): # B" -1)))
958 (should (= (save-excursion (python-nav-backward-defun))
959 (python-tests-look-at " def b(self): # b" -1)))
960 (should (= (save-excursion (python-nav-backward-defun))
961 (python-tests-look-at " def a(self): # a" -1)))
962 (should (= (save-excursion (python-nav-backward-defun))
963 (python-tests-look-at "class A(object): # A" -1)))
964 (should (not (python-nav-backward-defun)))))
966 (ert-deftest python-nav-backward-defun-2 ()
967 (python-tests-with-temp-buffer
969 def decoratorFunctionWithArguments(arg1, arg2, arg3):
970 '''print decorated function call data to stdout.
972 Usage:
974 @decoratorFunctionWithArguments('arg1', 'arg2')
975 def func(a, b, c=True):
976 pass
979 def wwrap(f):
980 print 'Inside wwrap()'
981 def wrapped_f(*args):
982 print 'Inside wrapped_f()'
983 print 'Decorator arguments:', arg1, arg2, arg3
984 f(*args)
985 print 'After f(*args)'
986 return wrapped_f
987 return wwrap
989 (goto-char (point-max))
990 (should (= (save-excursion (python-nav-backward-defun))
991 (python-tests-look-at " def wrapped_f(*args):" -1)))
992 (should (= (save-excursion (python-nav-backward-defun))
993 (python-tests-look-at " def wwrap(f):" -1)))
994 (should (= (save-excursion (python-nav-backward-defun))
995 (python-tests-look-at "def decoratorFunctionWithArguments(arg1, arg2, arg3):" -1)))
996 (should (not (python-nav-backward-defun)))))
998 (ert-deftest python-nav-backward-defun-3 ()
999 (python-tests-with-temp-buffer
1002 def u(self):
1003 pass
1005 def v(self):
1006 pass
1008 def w(self):
1009 pass
1012 class A(object):
1013 pass
1015 (goto-char (point-min))
1016 (let ((point (python-tests-look-at "class A(object):")))
1017 (should (not (python-nav-backward-defun)))
1018 (should (= point (point))))))
1020 (ert-deftest python-nav-forward-defun-1 ()
1021 (python-tests-with-temp-buffer
1023 class A(object): # A
1025 def a(self): # a
1026 pass
1028 def b(self): # b
1029 pass
1031 class B(object): # B
1033 class C(object): # C
1035 def d(self): # d
1036 pass
1038 # def e(self): # e
1039 # pass
1041 def c(self): # c
1042 pass
1044 # def d(self): # d
1045 # pass
1047 (goto-char (point-min))
1048 (should (= (save-excursion (python-nav-forward-defun))
1049 (python-tests-look-at "(object): # A")))
1050 (should (= (save-excursion (python-nav-forward-defun))
1051 (python-tests-look-at "(self): # a")))
1052 (should (= (save-excursion (python-nav-forward-defun))
1053 (python-tests-look-at "(self): # b")))
1054 (should (= (save-excursion (python-nav-forward-defun))
1055 (python-tests-look-at "(object): # B")))
1056 (should (= (save-excursion (python-nav-forward-defun))
1057 (python-tests-look-at "(object): # C")))
1058 (should (= (save-excursion (python-nav-forward-defun))
1059 (python-tests-look-at "(self): # d")))
1060 (should (= (save-excursion (python-nav-forward-defun))
1061 (python-tests-look-at "(self): # c")))
1062 (should (not (python-nav-forward-defun)))))
1064 (ert-deftest python-nav-forward-defun-2 ()
1065 (python-tests-with-temp-buffer
1067 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1068 '''print decorated function call data to stdout.
1070 Usage:
1072 @decoratorFunctionWithArguments('arg1', 'arg2')
1073 def func(a, b, c=True):
1074 pass
1077 def wwrap(f):
1078 print 'Inside wwrap()'
1079 def wrapped_f(*args):
1080 print 'Inside wrapped_f()'
1081 print 'Decorator arguments:', arg1, arg2, arg3
1082 f(*args)
1083 print 'After f(*args)'
1084 return wrapped_f
1085 return wwrap
1087 (goto-char (point-min))
1088 (should (= (save-excursion (python-nav-forward-defun))
1089 (python-tests-look-at "(arg1, arg2, arg3):")))
1090 (should (= (save-excursion (python-nav-forward-defun))
1091 (python-tests-look-at "(f):")))
1092 (should (= (save-excursion (python-nav-forward-defun))
1093 (python-tests-look-at "(*args):")))
1094 (should (not (python-nav-forward-defun)))))
1096 (ert-deftest python-nav-forward-defun-3 ()
1097 (python-tests-with-temp-buffer
1099 class A(object):
1100 pass
1103 def u(self):
1104 pass
1106 def v(self):
1107 pass
1109 def w(self):
1110 pass
1113 (goto-char (point-min))
1114 (let ((point (python-tests-look-at "(object):")))
1115 (should (not (python-nav-forward-defun)))
1116 (should (= point (point))))))
1118 (ert-deftest python-nav-beginning-of-statement-1 ()
1119 (python-tests-with-temp-buffer
1121 v1 = 123 + \
1122 456 + \
1124 v2 = (value1,
1125 value2,
1127 value3,
1128 value4)
1129 v3 = ('this is a string'
1131 'that is continued'
1132 'between lines'
1133 'within a paren',
1134 # this is a comment, yo
1135 'continue previous line')
1136 v4 = '''
1137 a very long
1138 string
1141 (python-tests-look-at "v2 =")
1142 (python-util-forward-comment -1)
1143 (should (= (save-excursion
1144 (python-nav-beginning-of-statement)
1145 (point))
1146 (python-tests-look-at "v1 =" -1 t)))
1147 (python-tests-look-at "v3 =")
1148 (python-util-forward-comment -1)
1149 (should (= (save-excursion
1150 (python-nav-beginning-of-statement)
1151 (point))
1152 (python-tests-look-at "v2 =" -1 t)))
1153 (python-tests-look-at "v4 =")
1154 (python-util-forward-comment -1)
1155 (should (= (save-excursion
1156 (python-nav-beginning-of-statement)
1157 (point))
1158 (python-tests-look-at "v3 =" -1 t)))
1159 (goto-char (point-max))
1160 (python-util-forward-comment -1)
1161 (should (= (save-excursion
1162 (python-nav-beginning-of-statement)
1163 (point))
1164 (python-tests-look-at "v4 =" -1 t)))))
1166 (ert-deftest python-nav-end-of-statement-1 ()
1167 (python-tests-with-temp-buffer
1169 v1 = 123 + \
1170 456 + \
1172 v2 = (value1,
1173 value2,
1175 value3,
1176 value4)
1177 v3 = ('this is a string'
1179 'that is continued'
1180 'between lines'
1181 'within a paren',
1182 # this is a comment, yo
1183 'continue previous line')
1184 v4 = '''
1185 a very long
1186 string
1189 (python-tests-look-at "v1 =")
1190 (should (= (save-excursion
1191 (python-nav-end-of-statement)
1192 (point))
1193 (save-excursion
1194 (python-tests-look-at "789")
1195 (line-end-position))))
1196 (python-tests-look-at "v2 =")
1197 (should (= (save-excursion
1198 (python-nav-end-of-statement)
1199 (point))
1200 (save-excursion
1201 (python-tests-look-at "value4)")
1202 (line-end-position))))
1203 (python-tests-look-at "v3 =")
1204 (should (= (save-excursion
1205 (python-nav-end-of-statement)
1206 (point))
1207 (save-excursion
1208 (python-tests-look-at
1209 "'continue previous line')")
1210 (line-end-position))))
1211 (python-tests-look-at "v4 =")
1212 (should (= (save-excursion
1213 (python-nav-end-of-statement)
1214 (point))
1215 (save-excursion
1216 (goto-char (point-max))
1217 (python-util-forward-comment -1)
1218 (point))))))
1220 (ert-deftest python-nav-forward-statement-1 ()
1221 (python-tests-with-temp-buffer
1223 v1 = 123 + \
1224 456 + \
1226 v2 = (value1,
1227 value2,
1229 value3,
1230 value4)
1231 v3 = ('this is a string'
1233 'that is continued'
1234 'between lines'
1235 'within a paren',
1236 # this is a comment, yo
1237 'continue previous line')
1238 v4 = '''
1239 a very long
1240 string
1243 (python-tests-look-at "v1 =")
1244 (should (= (save-excursion
1245 (python-nav-forward-statement)
1246 (point))
1247 (python-tests-look-at "v2 =")))
1248 (should (= (save-excursion
1249 (python-nav-forward-statement)
1250 (point))
1251 (python-tests-look-at "v3 =")))
1252 (should (= (save-excursion
1253 (python-nav-forward-statement)
1254 (point))
1255 (python-tests-look-at "v4 =")))
1256 (should (= (save-excursion
1257 (python-nav-forward-statement)
1258 (point))
1259 (point-max)))))
1261 (ert-deftest python-nav-backward-statement-1 ()
1262 (python-tests-with-temp-buffer
1264 v1 = 123 + \
1265 456 + \
1267 v2 = (value1,
1268 value2,
1270 value3,
1271 value4)
1272 v3 = ('this is a string'
1274 'that is continued'
1275 'between lines'
1276 'within a paren',
1277 # this is a comment, yo
1278 'continue previous line')
1279 v4 = '''
1280 a very long
1281 string
1284 (goto-char (point-max))
1285 (should (= (save-excursion
1286 (python-nav-backward-statement)
1287 (point))
1288 (python-tests-look-at "v4 =" -1)))
1289 (should (= (save-excursion
1290 (python-nav-backward-statement)
1291 (point))
1292 (python-tests-look-at "v3 =" -1)))
1293 (should (= (save-excursion
1294 (python-nav-backward-statement)
1295 (point))
1296 (python-tests-look-at "v2 =" -1)))
1297 (should (= (save-excursion
1298 (python-nav-backward-statement)
1299 (point))
1300 (python-tests-look-at "v1 =" -1)))))
1302 (ert-deftest python-nav-backward-statement-2 ()
1303 :expected-result :failed
1304 (python-tests-with-temp-buffer
1306 v1 = 123 + \
1307 456 + \
1309 v2 = (value1,
1310 value2,
1312 value3,
1313 value4)
1315 ;; FIXME: For some reason `python-nav-backward-statement' is moving
1316 ;; back two sentences when starting from 'value4)'.
1317 (goto-char (point-max))
1318 (python-util-forward-comment -1)
1319 (should (= (save-excursion
1320 (python-nav-backward-statement)
1321 (point))
1322 (python-tests-look-at "v2 =" -1 t)))))
1324 (ert-deftest python-nav-beginning-of-block-1 ()
1325 (python-tests-with-temp-buffer
1327 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1328 '''print decorated function call data to stdout.
1330 Usage:
1332 @decoratorFunctionWithArguments('arg1', 'arg2')
1333 def func(a, b, c=True):
1334 pass
1337 def wwrap(f):
1338 print 'Inside wwrap()'
1339 def wrapped_f(*args):
1340 print 'Inside wrapped_f()'
1341 print 'Decorator arguments:', arg1, arg2, arg3
1342 f(*args)
1343 print 'After f(*args)'
1344 return wrapped_f
1345 return wwrap
1347 (python-tests-look-at "return wwrap")
1348 (should (= (save-excursion
1349 (python-nav-beginning-of-block)
1350 (point))
1351 (python-tests-look-at "def decoratorFunctionWithArguments" -1)))
1352 (python-tests-look-at "print 'Inside wwrap()'")
1353 (should (= (save-excursion
1354 (python-nav-beginning-of-block)
1355 (point))
1356 (python-tests-look-at "def wwrap(f):" -1)))
1357 (python-tests-look-at "print 'After f(*args)'")
1358 (end-of-line)
1359 (should (= (save-excursion
1360 (python-nav-beginning-of-block)
1361 (point))
1362 (python-tests-look-at "def wrapped_f(*args):" -1)))
1363 (python-tests-look-at "return wrapped_f")
1364 (should (= (save-excursion
1365 (python-nav-beginning-of-block)
1366 (point))
1367 (python-tests-look-at "def wwrap(f):" -1)))))
1369 (ert-deftest python-nav-end-of-block-1 ()
1370 (python-tests-with-temp-buffer
1372 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1373 '''print decorated function call data to stdout.
1375 Usage:
1377 @decoratorFunctionWithArguments('arg1', 'arg2')
1378 def func(a, b, c=True):
1379 pass
1382 def wwrap(f):
1383 print 'Inside wwrap()'
1384 def wrapped_f(*args):
1385 print 'Inside wrapped_f()'
1386 print 'Decorator arguments:', arg1, arg2, arg3
1387 f(*args)
1388 print 'After f(*args)'
1389 return wrapped_f
1390 return wwrap
1392 (python-tests-look-at "def decoratorFunctionWithArguments")
1393 (should (= (save-excursion
1394 (python-nav-end-of-block)
1395 (point))
1396 (save-excursion
1397 (goto-char (point-max))
1398 (python-util-forward-comment -1)
1399 (point))))
1400 (python-tests-look-at "def wwrap(f):")
1401 (should (= (save-excursion
1402 (python-nav-end-of-block)
1403 (point))
1404 (save-excursion
1405 (python-tests-look-at "return wrapped_f")
1406 (line-end-position))))
1407 (end-of-line)
1408 (should (= (save-excursion
1409 (python-nav-end-of-block)
1410 (point))
1411 (save-excursion
1412 (python-tests-look-at "return wrapped_f")
1413 (line-end-position))))
1414 (python-tests-look-at "f(*args)")
1415 (should (= (save-excursion
1416 (python-nav-end-of-block)
1417 (point))
1418 (save-excursion
1419 (python-tests-look-at "print 'After f(*args)'")
1420 (line-end-position))))))
1422 (ert-deftest python-nav-forward-block-1 ()
1423 "This also accounts as a test for `python-nav-backward-block'."
1424 (python-tests-with-temp-buffer
1426 if request.user.is_authenticated():
1427 # def block():
1428 # pass
1429 try:
1430 profile = request.user.get_profile()
1431 except Profile.DoesNotExist:
1432 profile = Profile.objects.create(user=request.user)
1433 else:
1434 if profile.stats:
1435 profile.recalculate_stats()
1436 else:
1437 profile.clear_stats()
1438 finally:
1439 profile.views += 1
1440 profile.save()
1442 (should (= (save-excursion (python-nav-forward-block))
1443 (python-tests-look-at "if request.user.is_authenticated():")))
1444 (should (= (save-excursion (python-nav-forward-block))
1445 (python-tests-look-at "try:")))
1446 (should (= (save-excursion (python-nav-forward-block))
1447 (python-tests-look-at "except Profile.DoesNotExist:")))
1448 (should (= (save-excursion (python-nav-forward-block))
1449 (python-tests-look-at "else:")))
1450 (should (= (save-excursion (python-nav-forward-block))
1451 (python-tests-look-at "if profile.stats:")))
1452 (should (= (save-excursion (python-nav-forward-block))
1453 (python-tests-look-at "else:")))
1454 (should (= (save-excursion (python-nav-forward-block))
1455 (python-tests-look-at "finally:")))
1456 ;; When point is at the last block, leave it there and return nil
1457 (should (not (save-excursion (python-nav-forward-block))))
1458 ;; Move backwards, and even if the number of moves is less than the
1459 ;; provided argument return the point.
1460 (should (= (save-excursion (python-nav-forward-block -10))
1461 (python-tests-look-at
1462 "if request.user.is_authenticated():" -1)))))
1464 (ert-deftest python-nav-forward-sexp-1 ()
1465 (python-tests-with-temp-buffer
1471 (python-tests-look-at "a()")
1472 (python-nav-forward-sexp)
1473 (should (looking-at "$"))
1474 (should (save-excursion
1475 (beginning-of-line)
1476 (looking-at "a()")))
1477 (python-nav-forward-sexp)
1478 (should (looking-at "$"))
1479 (should (save-excursion
1480 (beginning-of-line)
1481 (looking-at "b()")))
1482 (python-nav-forward-sexp)
1483 (should (looking-at "$"))
1484 (should (save-excursion
1485 (beginning-of-line)
1486 (looking-at "c()")))
1487 ;; Movement next to a paren should do what lisp does and
1488 ;; unfortunately It can't change, because otherwise
1489 ;; `blink-matching-open' breaks.
1490 (python-nav-forward-sexp -1)
1491 (should (looking-at "()"))
1492 (should (save-excursion
1493 (beginning-of-line)
1494 (looking-at "c()")))
1495 (python-nav-forward-sexp -1)
1496 (should (looking-at "c()"))
1497 (python-nav-forward-sexp -1)
1498 (should (looking-at "b()"))
1499 (python-nav-forward-sexp -1)
1500 (should (looking-at "a()"))))
1502 (ert-deftest python-nav-forward-sexp-2 ()
1503 (python-tests-with-temp-buffer
1505 def func():
1506 if True:
1507 aaa = bbb
1508 ccc = ddd
1509 eee = fff
1510 return ggg
1512 (python-tests-look-at "aa =")
1513 (python-nav-forward-sexp)
1514 (should (looking-at " = bbb"))
1515 (python-nav-forward-sexp)
1516 (should (looking-at "$"))
1517 (should (save-excursion
1518 (back-to-indentation)
1519 (looking-at "aaa = bbb")))
1520 (python-nav-forward-sexp)
1521 (should (looking-at "$"))
1522 (should (save-excursion
1523 (back-to-indentation)
1524 (looking-at "ccc = ddd")))
1525 (python-nav-forward-sexp)
1526 (should (looking-at "$"))
1527 (should (save-excursion
1528 (back-to-indentation)
1529 (looking-at "eee = fff")))
1530 (python-nav-forward-sexp)
1531 (should (looking-at "$"))
1532 (should (save-excursion
1533 (back-to-indentation)
1534 (looking-at "return ggg")))
1535 (python-nav-forward-sexp -1)
1536 (should (looking-at "def func():"))))
1538 (ert-deftest python-nav-forward-sexp-3 ()
1539 (python-tests-with-temp-buffer
1541 from some_module import some_sub_module
1542 from another_module import another_sub_module
1544 def another_statement():
1545 pass
1547 (python-tests-look-at "some_module")
1548 (python-nav-forward-sexp)
1549 (should (looking-at " import"))
1550 (python-nav-forward-sexp)
1551 (should (looking-at " some_sub_module"))
1552 (python-nav-forward-sexp)
1553 (should (looking-at "$"))
1554 (should
1555 (save-excursion
1556 (back-to-indentation)
1557 (looking-at
1558 "from some_module import some_sub_module")))
1559 (python-nav-forward-sexp)
1560 (should (looking-at "$"))
1561 (should
1562 (save-excursion
1563 (back-to-indentation)
1564 (looking-at
1565 "from another_module import another_sub_module")))
1566 (python-nav-forward-sexp)
1567 (should (looking-at "$"))
1568 (should
1569 (save-excursion
1570 (back-to-indentation)
1571 (looking-at
1572 "pass")))
1573 (python-nav-forward-sexp -1)
1574 (should (looking-at "def another_statement():"))
1575 (python-nav-forward-sexp -1)
1576 (should (looking-at "from another_module import another_sub_module"))
1577 (python-nav-forward-sexp -1)
1578 (should (looking-at "from some_module import some_sub_module"))))
1580 (ert-deftest python-nav-forward-sexp-safe-1 ()
1581 (python-tests-with-temp-buffer
1583 profile = Profile.objects.create(user=request.user)
1584 profile.notify()
1586 (python-tests-look-at "profile =")
1587 (python-nav-forward-sexp-safe 1)
1588 (should (looking-at "$"))
1589 (beginning-of-line 1)
1590 (python-tests-look-at "user=request.user")
1591 (python-nav-forward-sexp-safe -1)
1592 (should (looking-at "(user=request.user)"))
1593 (python-nav-forward-sexp-safe -4)
1594 (should (looking-at "profile ="))
1595 (python-tests-look-at "user=request.user")
1596 (python-nav-forward-sexp-safe 3)
1597 (should (looking-at ")"))
1598 (python-nav-forward-sexp-safe 1)
1599 (should (looking-at "$"))
1600 (python-nav-forward-sexp-safe 1)
1601 (should (looking-at "$"))))
1603 (ert-deftest python-nav-up-list-1 ()
1604 (python-tests-with-temp-buffer
1606 def f():
1607 if True:
1608 return [i for i in range(3)]
1610 (python-tests-look-at "3)]")
1611 (python-nav-up-list)
1612 (should (looking-at "]"))
1613 (python-nav-up-list)
1614 (should (looking-at "$"))))
1616 (ert-deftest python-nav-backward-up-list-1 ()
1617 :expected-result :failed
1618 (python-tests-with-temp-buffer
1620 def f():
1621 if True:
1622 return [i for i in range(3)]
1624 (python-tests-look-at "3)]")
1625 (python-nav-backward-up-list)
1626 (should (looking-at "(3)\\]"))
1627 (python-nav-backward-up-list)
1628 (should (looking-at
1629 "\\[i for i in range(3)\\]"))
1630 ;; FIXME: Need to move to beginning-of-statement.
1631 (python-nav-backward-up-list)
1632 (should (looking-at
1633 "return \\[i for i in range(3)\\]"))
1634 (python-nav-backward-up-list)
1635 (should (looking-at "if True:"))
1636 (python-nav-backward-up-list)
1637 (should (looking-at "def f():"))))
1640 ;;; Shell integration
1642 (defvar python-tests-shell-interpreter "python")
1644 (ert-deftest python-shell-get-process-name-1 ()
1645 "Check process name calculation on different scenarios."
1646 (python-tests-with-temp-buffer
1648 (should (string= (python-shell-get-process-name nil)
1649 python-shell-buffer-name))
1650 ;; When the `current-buffer' doesn't have `buffer-file-name', even
1651 ;; if dedicated flag is non-nil should not include its name.
1652 (should (string= (python-shell-get-process-name t)
1653 python-shell-buffer-name)))
1654 (python-tests-with-temp-file
1656 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1657 ;; should be respected.
1658 (should (string= (python-shell-get-process-name nil)
1659 python-shell-buffer-name))
1660 (should (string=
1661 (python-shell-get-process-name t)
1662 (format "%s[%s]" python-shell-buffer-name buffer-file-name)))))
1664 (ert-deftest python-shell-internal-get-process-name-1 ()
1665 "Check the internal process name is config-unique."
1666 (let* ((python-shell-interpreter python-tests-shell-interpreter)
1667 (python-shell-interpreter-args "")
1668 (python-shell-prompt-regexp ">>> ")
1669 (python-shell-prompt-block-regexp "[.][.][.] ")
1670 (python-shell-setup-codes "")
1671 (python-shell-process-environment "")
1672 (python-shell-extra-pythonpaths "")
1673 (python-shell-exec-path "")
1674 (python-shell-virtualenv-path "")
1675 (expected (python-tests-with-temp-buffer
1676 "" (python-shell-internal-get-process-name))))
1677 ;; Same configurations should match.
1678 (should
1679 (string= expected
1680 (python-tests-with-temp-buffer
1681 "" (python-shell-internal-get-process-name))))
1682 (let ((python-shell-interpreter-args "-B"))
1683 ;; A minimal change should generate different names.
1684 (should
1685 (not (string=
1686 expected
1687 (python-tests-with-temp-buffer
1688 "" (python-shell-internal-get-process-name))))))))
1690 (ert-deftest python-shell-parse-command-1 ()
1691 "Check the command to execute is calculated correctly.
1692 Using `python-shell-interpreter' and
1693 `python-shell-interpreter-args'."
1694 (skip-unless (executable-find python-tests-shell-interpreter))
1695 (let ((python-shell-interpreter (executable-find
1696 python-tests-shell-interpreter))
1697 (python-shell-interpreter-args "-B"))
1698 (should (string=
1699 (format "%s %s"
1700 python-shell-interpreter
1701 python-shell-interpreter-args)
1702 (python-shell-parse-command)))))
1704 (ert-deftest python-shell-calculate-process-environment-1 ()
1705 "Test `python-shell-process-environment' modification."
1706 (let* ((original-process-environment process-environment)
1707 (python-shell-process-environment
1708 '("TESTVAR1=value1" "TESTVAR2=value2"))
1709 (process-environment
1710 (python-shell-calculate-process-environment)))
1711 (should (equal (getenv "TESTVAR1") "value1"))
1712 (should (equal (getenv "TESTVAR2") "value2"))))
1714 (ert-deftest python-shell-calculate-process-environment-2 ()
1715 "Test `python-shell-extra-pythonpaths' modification."
1716 (let* ((original-process-environment process-environment)
1717 (original-pythonpath (getenv "PYTHONPATH"))
1718 (paths '("path1" "path2"))
1719 (python-shell-extra-pythonpaths paths)
1720 (process-environment
1721 (python-shell-calculate-process-environment)))
1722 (should (equal (getenv "PYTHONPATH")
1723 (concat
1724 (mapconcat 'identity paths path-separator)
1725 path-separator original-pythonpath)))))
1727 (ert-deftest python-shell-calculate-process-environment-3 ()
1728 "Test `python-shell-virtualenv-path' modification."
1729 (let* ((original-process-environment process-environment)
1730 (original-path (or (getenv "PATH") ""))
1731 (python-shell-virtualenv-path
1732 (directory-file-name user-emacs-directory))
1733 (process-environment
1734 (python-shell-calculate-process-environment)))
1735 (should (not (getenv "PYTHONHOME")))
1736 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-path))
1737 (should (equal (getenv "PATH")
1738 (format "%s/bin%s%s"
1739 python-shell-virtualenv-path
1740 path-separator original-path)))))
1742 (ert-deftest python-shell-calculate-exec-path-1 ()
1743 "Test `python-shell-exec-path' modification."
1744 (let* ((original-exec-path exec-path)
1745 (python-shell-exec-path '("path1" "path2"))
1746 (exec-path (python-shell-calculate-exec-path)))
1747 (should (equal
1748 exec-path
1749 (append python-shell-exec-path
1750 original-exec-path)))))
1752 (ert-deftest python-shell-calculate-exec-path-2 ()
1753 "Test `python-shell-exec-path' modification."
1754 (let* ((original-exec-path exec-path)
1755 (python-shell-virtualenv-path
1756 (directory-file-name user-emacs-directory))
1757 (exec-path (python-shell-calculate-exec-path)))
1758 (should (equal
1759 exec-path
1760 (append (cons
1761 (format "%s/bin" python-shell-virtualenv-path)
1762 original-exec-path))))))
1764 (ert-deftest python-shell-make-comint-1 ()
1765 "Check comint creation for global shell buffer."
1766 (skip-unless (executable-find python-tests-shell-interpreter))
1767 ;; The interpreter can get killed too quickly to allow it to clean
1768 ;; up the tempfiles that the default python-shell-setup-codes create,
1769 ;; so it leaves tempfiles behind, which is a minor irritation.
1770 (let* ((python-shell-setup-codes nil)
1771 (python-shell-interpreter
1772 (executable-find python-tests-shell-interpreter))
1773 (proc-name (python-shell-get-process-name nil))
1774 (shell-buffer
1775 (python-tests-with-temp-buffer
1776 "" (python-shell-make-comint
1777 (python-shell-parse-command) proc-name)))
1778 (process (get-buffer-process shell-buffer)))
1779 (unwind-protect
1780 (progn
1781 (set-process-query-on-exit-flag process nil)
1782 (should (process-live-p process))
1783 (with-current-buffer shell-buffer
1784 (should (eq major-mode 'inferior-python-mode))
1785 (should (string= (buffer-name) (format "*%s*" proc-name)))))
1786 (kill-buffer shell-buffer))))
1788 (ert-deftest python-shell-make-comint-2 ()
1789 "Check comint creation for internal shell buffer."
1790 (skip-unless (executable-find python-tests-shell-interpreter))
1791 (let* ((python-shell-setup-codes nil)
1792 (python-shell-interpreter
1793 (executable-find python-tests-shell-interpreter))
1794 (proc-name (python-shell-internal-get-process-name))
1795 (shell-buffer
1796 (python-tests-with-temp-buffer
1797 "" (python-shell-make-comint
1798 (python-shell-parse-command) proc-name nil t)))
1799 (process (get-buffer-process shell-buffer)))
1800 (unwind-protect
1801 (progn
1802 (set-process-query-on-exit-flag process nil)
1803 (should (process-live-p process))
1804 (with-current-buffer shell-buffer
1805 (should (eq major-mode 'inferior-python-mode))
1806 (should (string= (buffer-name) (format " *%s*" proc-name)))))
1807 (kill-buffer shell-buffer))))
1809 (ert-deftest python-shell-make-comint-3 ()
1810 "Check comint creation with overriden python interpreter and args.
1811 The command passed to `python-shell-make-comint' as argument must
1812 locally override global values set in `python-shell-interpreter'
1813 and `python-shell-interpreter-args' in the new shell buffer."
1814 (skip-unless (executable-find python-tests-shell-interpreter))
1815 (let* ((python-shell-setup-codes nil)
1816 (python-shell-interpreter "interpreter")
1817 (python-shell-interpreter-args "--some-args")
1818 (proc-name (python-shell-get-process-name nil))
1819 (interpreter-override
1820 (concat (executable-find python-tests-shell-interpreter) " " "-i"))
1821 (shell-buffer
1822 (python-tests-with-temp-buffer
1823 "" (python-shell-make-comint interpreter-override proc-name nil)))
1824 (process (get-buffer-process shell-buffer)))
1825 (unwind-protect
1826 (progn
1827 (set-process-query-on-exit-flag process nil)
1828 (should (process-live-p process))
1829 (with-current-buffer shell-buffer
1830 (should (eq major-mode 'inferior-python-mode))
1831 (should (string= python-shell-interpreter
1832 (executable-find python-tests-shell-interpreter)))
1833 (should (string= python-shell-interpreter-args "-i"))))
1834 (kill-buffer shell-buffer))))
1836 (ert-deftest python-shell-make-comint-4 ()
1837 "Check shell calculated prompts regexps are set."
1838 (skip-unless (executable-find python-tests-shell-interpreter))
1839 (let* ((process-environment process-environment)
1840 (python-shell-setup-codes nil)
1841 (python-shell-interpreter
1842 (executable-find python-tests-shell-interpreter))
1843 (python-shell-interpreter-args "-i")
1844 (python-shell--prompt-calculated-input-regexp nil)
1845 (python-shell--prompt-calculated-output-regexp nil)
1846 (python-shell-prompt-detect-enabled t)
1847 (python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
1848 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
1849 (python-shell-prompt-regexp "in")
1850 (python-shell-prompt-block-regexp "block")
1851 (python-shell-prompt-pdb-regexp "pdf")
1852 (python-shell-prompt-output-regexp "output")
1853 (startup-code (concat "import sys\n"
1854 "sys.ps1 = 'py> '\n"
1855 "sys.ps2 = '..> '\n"
1856 "sys.ps3 = 'out '\n"))
1857 (startup-file (python-shell--save-temp-file startup-code))
1858 (proc-name (python-shell-get-process-name nil))
1859 (shell-buffer
1860 (progn
1861 (setenv "PYTHONSTARTUP" startup-file)
1862 (python-tests-with-temp-buffer
1863 "" (python-shell-make-comint
1864 (python-shell-parse-command) proc-name nil))))
1865 (process (get-buffer-process shell-buffer)))
1866 (unwind-protect
1867 (progn
1868 (set-process-query-on-exit-flag process nil)
1869 (should (process-live-p process))
1870 (with-current-buffer shell-buffer
1871 (should (eq major-mode 'inferior-python-mode))
1872 (should (string=
1873 python-shell--prompt-calculated-input-regexp
1874 (concat "^\\(extralargeinputprompt\\|\\.\\.> \\|"
1875 "block\\|py> \\|pdf\\|sml\\|in\\)")))
1876 (should (string=
1877 python-shell--prompt-calculated-output-regexp
1878 "^\\(extralargeoutputprompt\\|output\\|out \\|sml\\)"))))
1879 (delete-file startup-file)
1880 (kill-buffer shell-buffer))))
1882 (ert-deftest python-shell-get-process-1 ()
1883 "Check dedicated shell process preference over global."
1884 (skip-unless (executable-find python-tests-shell-interpreter))
1885 (python-tests-with-temp-file
1887 (let* ((python-shell-setup-codes nil)
1888 (python-shell-interpreter
1889 (executable-find python-tests-shell-interpreter))
1890 (global-proc-name (python-shell-get-process-name nil))
1891 (dedicated-proc-name (python-shell-get-process-name t))
1892 (global-shell-buffer
1893 (python-shell-make-comint
1894 (python-shell-parse-command) global-proc-name))
1895 (dedicated-shell-buffer
1896 (python-shell-make-comint
1897 (python-shell-parse-command) dedicated-proc-name))
1898 (global-process (get-buffer-process global-shell-buffer))
1899 (dedicated-process (get-buffer-process dedicated-shell-buffer)))
1900 (unwind-protect
1901 (progn
1902 (set-process-query-on-exit-flag global-process nil)
1903 (set-process-query-on-exit-flag dedicated-process nil)
1904 ;; Prefer dedicated if global also exists.
1905 (should (equal (python-shell-get-process) dedicated-process))
1906 (kill-buffer dedicated-shell-buffer)
1907 ;; If there's only global, use it.
1908 (should (equal (python-shell-get-process) global-process))
1909 (kill-buffer global-shell-buffer)
1910 ;; No buffer available.
1911 (should (not (python-shell-get-process))))
1912 (ignore-errors (kill-buffer global-shell-buffer))
1913 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
1915 (ert-deftest python-shell-get-or-create-process-1 ()
1916 "Check shell dedicated process creation."
1917 (skip-unless (executable-find python-tests-shell-interpreter))
1918 (python-tests-with-temp-file
1920 (let* ((python-shell-interpreter
1921 (executable-find python-tests-shell-interpreter))
1922 (use-dialog-box)
1923 (dedicated-process-name (python-shell-get-process-name t))
1924 (dedicated-process
1925 (python-shell-get-or-create-process python-shell-interpreter t))
1926 (dedicated-shell-buffer (process-buffer dedicated-process)))
1927 (unwind-protect
1928 (progn
1929 (set-process-query-on-exit-flag dedicated-process nil)
1930 ;; should be dedicated.
1931 (should (equal (process-name dedicated-process)
1932 dedicated-process-name))
1933 (kill-buffer dedicated-shell-buffer)
1934 ;; Check there are no processes for current buffer.
1935 (should (not (python-shell-get-process))))
1936 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
1938 (ert-deftest python-shell-get-or-create-process-2 ()
1939 "Check shell global process creation."
1940 (skip-unless (executable-find python-tests-shell-interpreter))
1941 (python-tests-with-temp-file
1943 (let* ((python-shell-interpreter
1944 (executable-find python-tests-shell-interpreter))
1945 (use-dialog-box)
1946 (process-name (python-shell-get-process-name nil))
1947 (process
1948 (python-shell-get-or-create-process python-shell-interpreter))
1949 (shell-buffer (process-buffer process)))
1950 (unwind-protect
1951 (progn
1952 (set-process-query-on-exit-flag process nil)
1953 ;; should be global.
1954 (should (equal (process-name process) process-name))
1955 (kill-buffer shell-buffer)
1956 ;; Check there are no processes for current buffer.
1957 (should (not (python-shell-get-process))))
1958 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
1960 (ert-deftest python-shell-get-or-create-process-3 ()
1961 "Check shell dedicated/global process preference."
1962 (skip-unless (executable-find python-tests-shell-interpreter))
1963 (python-tests-with-temp-file
1965 (let* ((python-shell-interpreter
1966 (executable-find python-tests-shell-interpreter))
1967 (use-dialog-box)
1968 (dedicated-process-name (python-shell-get-process-name t))
1969 (global-process)
1970 (dedicated-process))
1971 (unwind-protect
1972 (progn
1973 ;; Create global process
1974 (run-python python-shell-interpreter nil)
1975 (setq global-process (get-buffer-process "*Python*"))
1976 (should global-process)
1977 (set-process-query-on-exit-flag global-process nil)
1978 ;; Create dedicated process
1979 (run-python python-shell-interpreter t)
1980 (setq dedicated-process (get-process dedicated-process-name))
1981 (should dedicated-process)
1982 (set-process-query-on-exit-flag dedicated-process nil)
1983 ;; Prefer dedicated.
1984 (should (equal (python-shell-get-or-create-process)
1985 dedicated-process))
1986 ;; Kill the dedicated so the global takes over.
1987 (kill-buffer (process-buffer dedicated-process))
1988 ;; Detect global.
1989 (should (equal (python-shell-get-or-create-process) global-process))
1990 ;; Kill the global.
1991 (kill-buffer (process-buffer global-process))
1992 ;; Check there are no processes for current buffer.
1993 (should (not (python-shell-get-process))))
1994 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
1996 (ert-deftest python-shell-internal-get-or-create-process-1 ()
1997 "Check internal shell process creation fallback."
1998 (skip-unless (executable-find python-tests-shell-interpreter))
1999 (python-tests-with-temp-file
2001 (should (not (process-live-p (python-shell-internal-get-process-name))))
2002 (let* ((python-shell-interpreter
2003 (executable-find python-tests-shell-interpreter))
2004 (internal-process-name (python-shell-internal-get-process-name))
2005 (internal-process (python-shell-internal-get-or-create-process))
2006 (internal-shell-buffer (process-buffer internal-process)))
2007 (unwind-protect
2008 (progn
2009 (set-process-query-on-exit-flag internal-process nil)
2010 (should (equal (process-name internal-process)
2011 internal-process-name))
2012 (should (equal internal-process
2013 (python-shell-internal-get-or-create-process)))
2014 ;; Assert the internal process is not a user process
2015 (should (not (python-shell-get-process)))
2016 (kill-buffer internal-shell-buffer))
2017 (ignore-errors (kill-buffer internal-shell-buffer))))))
2019 (ert-deftest python-shell-prompt-detect-1 ()
2020 "Check prompt autodetection."
2021 (skip-unless (executable-find python-tests-shell-interpreter))
2022 (let ((process-environment process-environment))
2023 ;; Ensure no startup file is enabled
2024 (setenv "PYTHONSTARTUP" "")
2025 (should python-shell-prompt-detect-enabled)
2026 (should (equal (python-shell-prompt-detect) '(">>> " "... " "")))))
2028 (ert-deftest python-shell-prompt-detect-2 ()
2029 "Check prompt autodetection with startup file. Bug#17370."
2030 (skip-unless (executable-find python-tests-shell-interpreter))
2031 (let* ((process-environment process-environment)
2032 (startup-code (concat "import sys\n"
2033 "sys.ps1 = 'py> '\n"
2034 "sys.ps2 = '..> '\n"
2035 "sys.ps3 = 'out '\n"))
2036 (startup-file (python-shell--save-temp-file startup-code)))
2037 (unwind-protect
2038 (progn
2039 ;; Ensure startup file is enabled
2040 (setenv "PYTHONSTARTUP" startup-file)
2041 (should python-shell-prompt-detect-enabled)
2042 (should (equal (python-shell-prompt-detect) '("py> " "..> " "out "))))
2043 (ignore-errors (delete-file startup-file)))))
2045 (ert-deftest python-shell-prompt-detect-3 ()
2046 "Check prompts are not autodetected when feature is disabled."
2047 (skip-unless (executable-find python-tests-shell-interpreter))
2048 (let ((process-environment process-environment)
2049 (python-shell-prompt-detect-enabled nil))
2050 ;; Ensure no startup file is enabled
2051 (should (not python-shell-prompt-detect-enabled))
2052 (should (not (python-shell-prompt-detect)))))
2054 (ert-deftest python-shell-prompt-detect-4 ()
2055 "Check warning is shown when detection fails."
2056 (skip-unless (executable-find python-tests-shell-interpreter))
2057 (let* ((process-environment process-environment)
2058 ;; Trigger failure by removing prompts in the startup file
2059 (startup-code (concat "import sys\n"
2060 "sys.ps1 = ''\n"
2061 "sys.ps2 = ''\n"
2062 "sys.ps3 = ''\n"))
2063 (startup-file (python-shell--save-temp-file startup-code)))
2064 (unwind-protect
2065 (progn
2066 (kill-buffer (get-buffer-create "*Warnings*"))
2067 (should (not (get-buffer "*Warnings*")))
2068 (setenv "PYTHONSTARTUP" startup-file)
2069 (should python-shell-prompt-detect-failure-warning)
2070 (should python-shell-prompt-detect-enabled)
2071 (should (not (python-shell-prompt-detect)))
2072 (should (get-buffer "*Warnings*")))
2073 (ignore-errors (delete-file startup-file)))))
2075 (ert-deftest python-shell-prompt-detect-5 ()
2076 "Check disabled warnings are not shown when detection fails."
2077 (skip-unless (executable-find python-tests-shell-interpreter))
2078 (let* ((process-environment process-environment)
2079 (startup-code (concat "import sys\n"
2080 "sys.ps1 = ''\n"
2081 "sys.ps2 = ''\n"
2082 "sys.ps3 = ''\n"))
2083 (startup-file (python-shell--save-temp-file startup-code))
2084 (python-shell-prompt-detect-failure-warning nil))
2085 (unwind-protect
2086 (progn
2087 (kill-buffer (get-buffer-create "*Warnings*"))
2088 (should (not (get-buffer "*Warnings*")))
2089 (setenv "PYTHONSTARTUP" startup-file)
2090 (should (not python-shell-prompt-detect-failure-warning))
2091 (should python-shell-prompt-detect-enabled)
2092 (should (not (python-shell-prompt-detect)))
2093 (should (not (get-buffer "*Warnings*"))))
2094 (ignore-errors (delete-file startup-file)))))
2096 (ert-deftest python-shell-prompt-detect-6 ()
2097 "Warnings are not shown when detection is disabled."
2098 (skip-unless (executable-find python-tests-shell-interpreter))
2099 (let* ((process-environment process-environment)
2100 (startup-code (concat "import sys\n"
2101 "sys.ps1 = ''\n"
2102 "sys.ps2 = ''\n"
2103 "sys.ps3 = ''\n"))
2104 (startup-file (python-shell--save-temp-file startup-code))
2105 (python-shell-prompt-detect-failure-warning t)
2106 (python-shell-prompt-detect-enabled nil))
2107 (unwind-protect
2108 (progn
2109 (kill-buffer (get-buffer-create "*Warnings*"))
2110 (should (not (get-buffer "*Warnings*")))
2111 (setenv "PYTHONSTARTUP" startup-file)
2112 (should python-shell-prompt-detect-failure-warning)
2113 (should (not python-shell-prompt-detect-enabled))
2114 (should (not (python-shell-prompt-detect)))
2115 (should (not (get-buffer "*Warnings*"))))
2116 (ignore-errors (delete-file startup-file)))))
2118 (ert-deftest python-shell-prompt-validate-regexps-1 ()
2119 "Check `python-shell-prompt-input-regexps' are validated."
2120 (let* ((python-shell-prompt-input-regexps '("\\("))
2121 (error-data (should-error (python-shell-prompt-validate-regexps)
2122 :type 'user-error)))
2123 (should
2124 (string= (cadr error-data)
2125 "Invalid regexp \\( in `python-shell-prompt-input-regexps'"))))
2127 (ert-deftest python-shell-prompt-validate-regexps-2 ()
2128 "Check `python-shell-prompt-output-regexps' are validated."
2129 (let* ((python-shell-prompt-output-regexps '("\\("))
2130 (error-data (should-error (python-shell-prompt-validate-regexps)
2131 :type 'user-error)))
2132 (should
2133 (string= (cadr error-data)
2134 "Invalid regexp \\( in `python-shell-prompt-output-regexps'"))))
2136 (ert-deftest python-shell-prompt-validate-regexps-3 ()
2137 "Check `python-shell-prompt-regexp' is validated."
2138 (let* ((python-shell-prompt-regexp "\\(")
2139 (error-data (should-error (python-shell-prompt-validate-regexps)
2140 :type 'user-error)))
2141 (should
2142 (string= (cadr error-data)
2143 "Invalid regexp \\( in `python-shell-prompt-regexp'"))))
2145 (ert-deftest python-shell-prompt-validate-regexps-4 ()
2146 "Check `python-shell-prompt-block-regexp' is validated."
2147 (let* ((python-shell-prompt-block-regexp "\\(")
2148 (error-data (should-error (python-shell-prompt-validate-regexps)
2149 :type 'user-error)))
2150 (should
2151 (string= (cadr error-data)
2152 "Invalid regexp \\( in `python-shell-prompt-block-regexp'"))))
2154 (ert-deftest python-shell-prompt-validate-regexps-5 ()
2155 "Check `python-shell-prompt-pdb-regexp' is validated."
2156 (let* ((python-shell-prompt-pdb-regexp "\\(")
2157 (error-data (should-error (python-shell-prompt-validate-regexps)
2158 :type 'user-error)))
2159 (should
2160 (string= (cadr error-data)
2161 "Invalid regexp \\( in `python-shell-prompt-pdb-regexp'"))))
2163 (ert-deftest python-shell-prompt-validate-regexps-6 ()
2164 "Check `python-shell-prompt-output-regexp' is validated."
2165 (let* ((python-shell-prompt-output-regexp "\\(")
2166 (error-data (should-error (python-shell-prompt-validate-regexps)
2167 :type 'user-error)))
2168 (should
2169 (string= (cadr error-data)
2170 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2172 (ert-deftest python-shell-prompt-validate-regexps-7 ()
2173 "Check default regexps are valid."
2174 ;; should not signal error
2175 (python-shell-prompt-validate-regexps))
2177 (ert-deftest python-shell-prompt-set-calculated-regexps-1 ()
2178 "Check regexps are validated."
2179 (let* ((python-shell-prompt-output-regexp '("\\("))
2180 (python-shell--prompt-calculated-input-regexp nil)
2181 (python-shell--prompt-calculated-output-regexp nil)
2182 (python-shell-prompt-detect-enabled nil)
2183 (error-data (should-error (python-shell-prompt-set-calculated-regexps)
2184 :type 'user-error)))
2185 (should
2186 (string= (cadr error-data)
2187 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2189 (ert-deftest python-shell-prompt-set-calculated-regexps-2 ()
2190 "Check `python-shell-prompt-input-regexps' are set."
2191 (let* ((python-shell-prompt-input-regexps '("my" "prompt"))
2192 (python-shell-prompt-output-regexps '(""))
2193 (python-shell-prompt-regexp "")
2194 (python-shell-prompt-block-regexp "")
2195 (python-shell-prompt-pdb-regexp "")
2196 (python-shell-prompt-output-regexp "")
2197 (python-shell--prompt-calculated-input-regexp nil)
2198 (python-shell--prompt-calculated-output-regexp nil)
2199 (python-shell-prompt-detect-enabled nil))
2200 (python-shell-prompt-set-calculated-regexps)
2201 (should (string= python-shell--prompt-calculated-input-regexp
2202 "^\\(prompt\\|my\\|\\)"))))
2204 (ert-deftest python-shell-prompt-set-calculated-regexps-3 ()
2205 "Check `python-shell-prompt-output-regexps' are set."
2206 (let* ((python-shell-prompt-input-regexps '(""))
2207 (python-shell-prompt-output-regexps '("my" "prompt"))
2208 (python-shell-prompt-regexp "")
2209 (python-shell-prompt-block-regexp "")
2210 (python-shell-prompt-pdb-regexp "")
2211 (python-shell-prompt-output-regexp "")
2212 (python-shell--prompt-calculated-input-regexp nil)
2213 (python-shell--prompt-calculated-output-regexp nil)
2214 (python-shell-prompt-detect-enabled nil))
2215 (python-shell-prompt-set-calculated-regexps)
2216 (should (string= python-shell--prompt-calculated-output-regexp
2217 "^\\(prompt\\|my\\|\\)"))))
2219 (ert-deftest python-shell-prompt-set-calculated-regexps-4 ()
2220 "Check user defined prompts are set."
2221 (let* ((python-shell-prompt-input-regexps '(""))
2222 (python-shell-prompt-output-regexps '(""))
2223 (python-shell-prompt-regexp "prompt")
2224 (python-shell-prompt-block-regexp "block")
2225 (python-shell-prompt-pdb-regexp "pdb")
2226 (python-shell-prompt-output-regexp "output")
2227 (python-shell--prompt-calculated-input-regexp nil)
2228 (python-shell--prompt-calculated-output-regexp nil)
2229 (python-shell-prompt-detect-enabled nil))
2230 (python-shell-prompt-set-calculated-regexps)
2231 (should (string= python-shell--prompt-calculated-input-regexp
2232 "^\\(prompt\\|block\\|pdb\\|\\)"))
2233 (should (string= python-shell--prompt-calculated-output-regexp
2234 "^\\(output\\|\\)"))))
2236 (ert-deftest python-shell-prompt-set-calculated-regexps-5 ()
2237 "Check order of regexps (larger first)."
2238 (let* ((python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2239 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2240 (python-shell-prompt-regexp "in")
2241 (python-shell-prompt-block-regexp "block")
2242 (python-shell-prompt-pdb-regexp "pdf")
2243 (python-shell-prompt-output-regexp "output")
2244 (python-shell--prompt-calculated-input-regexp nil)
2245 (python-shell--prompt-calculated-output-regexp nil)
2246 (python-shell-prompt-detect-enabled nil))
2247 (python-shell-prompt-set-calculated-regexps)
2248 (should (string= python-shell--prompt-calculated-input-regexp
2249 "^\\(extralargeinputprompt\\|block\\|pdf\\|sml\\|in\\)"))
2250 (should (string= python-shell--prompt-calculated-output-regexp
2251 "^\\(extralargeoutputprompt\\|output\\|sml\\)"))))
2253 (ert-deftest python-shell-prompt-set-calculated-regexps-6 ()
2254 "Check detected prompts are included `regexp-quote'd."
2255 (skip-unless (executable-find python-tests-shell-interpreter))
2256 (let* ((python-shell-prompt-input-regexps '(""))
2257 (python-shell-prompt-output-regexps '(""))
2258 (python-shell-prompt-regexp "")
2259 (python-shell-prompt-block-regexp "")
2260 (python-shell-prompt-pdb-regexp "")
2261 (python-shell-prompt-output-regexp "")
2262 (python-shell--prompt-calculated-input-regexp nil)
2263 (python-shell--prompt-calculated-output-regexp nil)
2264 (python-shell-prompt-detect-enabled t)
2265 (process-environment process-environment)
2266 (startup-code (concat "import sys\n"
2267 "sys.ps1 = 'p.> '\n"
2268 "sys.ps2 = '..> '\n"
2269 "sys.ps3 = 'o.t '\n"))
2270 (startup-file (python-shell--save-temp-file startup-code)))
2271 (unwind-protect
2272 (progn
2273 (setenv "PYTHONSTARTUP" startup-file)
2274 (python-shell-prompt-set-calculated-regexps)
2275 (should (string= python-shell--prompt-calculated-input-regexp
2276 "^\\(\\.\\.> \\|p\\.> \\|\\)"))
2277 (should (string= python-shell--prompt-calculated-output-regexp
2278 "^\\(o\\.t \\|\\)")))
2279 (ignore-errors (delete-file startup-file)))))
2282 ;;; Shell completion
2285 ;;; PDB Track integration
2288 ;;; Symbol completion
2291 ;;; Fill paragraph
2294 ;;; Skeletons
2297 ;;; FFAP
2300 ;;; Code check
2303 ;;; Eldoc
2306 ;;; Imenu
2308 (ert-deftest python-imenu-create-index-1 ()
2309 (python-tests-with-temp-buffer
2311 class Foo(models.Model):
2312 pass
2315 class Bar(models.Model):
2316 pass
2319 def decorator(arg1, arg2, arg3):
2320 '''print decorated function call data to stdout.
2322 Usage:
2324 @decorator('arg1', 'arg2')
2325 def func(a, b, c=True):
2326 pass
2329 def wrap(f):
2330 print ('wrap')
2331 def wrapped_f(*args):
2332 print ('wrapped_f')
2333 print ('Decorator arguments:', arg1, arg2, arg3)
2334 f(*args)
2335 print ('called f(*args)')
2336 return wrapped_f
2337 return wrap
2340 class Baz(object):
2342 def a(self):
2343 pass
2345 def b(self):
2346 pass
2348 class Frob(object):
2350 def c(self):
2351 pass
2353 (goto-char (point-max))
2354 (should (equal
2355 (list
2356 (cons "Foo (class)" (copy-marker 2))
2357 (cons "Bar (class)" (copy-marker 38))
2358 (list
2359 "decorator (def)"
2360 (cons "*function definition*" (copy-marker 74))
2361 (list
2362 "wrap (def)"
2363 (cons "*function definition*" (copy-marker 254))
2364 (cons "wrapped_f (def)" (copy-marker 294))))
2365 (list
2366 "Baz (class)"
2367 (cons "*class definition*" (copy-marker 519))
2368 (cons "a (def)" (copy-marker 539))
2369 (cons "b (def)" (copy-marker 570))
2370 (list
2371 "Frob (class)"
2372 (cons "*class definition*" (copy-marker 601))
2373 (cons "c (def)" (copy-marker 626)))))
2374 (python-imenu-create-index)))))
2376 (ert-deftest python-imenu-create-index-2 ()
2377 (python-tests-with-temp-buffer
2379 class Foo(object):
2380 def foo(self):
2381 def foo1():
2382 pass
2384 def foobar(self):
2385 pass
2387 (goto-char (point-max))
2388 (should (equal
2389 (list
2390 (list
2391 "Foo (class)"
2392 (cons "*class definition*" (copy-marker 2))
2393 (list
2394 "foo (def)"
2395 (cons "*function definition*" (copy-marker 21))
2396 (cons "foo1 (def)" (copy-marker 40)))
2397 (cons "foobar (def)" (copy-marker 78))))
2398 (python-imenu-create-index)))))
2400 (ert-deftest python-imenu-create-index-3 ()
2401 (python-tests-with-temp-buffer
2403 class Foo(object):
2404 def foo(self):
2405 def foo1():
2406 pass
2407 def foo2():
2408 pass
2410 (goto-char (point-max))
2411 (should (equal
2412 (list
2413 (list
2414 "Foo (class)"
2415 (cons "*class definition*" (copy-marker 2))
2416 (list
2417 "foo (def)"
2418 (cons "*function definition*" (copy-marker 21))
2419 (cons "foo1 (def)" (copy-marker 40))
2420 (cons "foo2 (def)" (copy-marker 77)))))
2421 (python-imenu-create-index)))))
2423 (ert-deftest python-imenu-create-index-4 ()
2424 (python-tests-with-temp-buffer
2426 class Foo(object):
2427 class Bar(object):
2428 def __init__(self):
2429 pass
2431 def __str__(self):
2432 pass
2434 def __init__(self):
2435 pass
2437 (goto-char (point-max))
2438 (should (equal
2439 (list
2440 (list
2441 "Foo (class)"
2442 (cons "*class definition*" (copy-marker 2))
2443 (list
2444 "Bar (class)"
2445 (cons "*class definition*" (copy-marker 21))
2446 (cons "__init__ (def)" (copy-marker 44))
2447 (cons "__str__ (def)" (copy-marker 90)))
2448 (cons "__init__ (def)" (copy-marker 135))))
2449 (python-imenu-create-index)))))
2451 (ert-deftest python-imenu-create-flat-index-1 ()
2452 (python-tests-with-temp-buffer
2454 class Foo(models.Model):
2455 pass
2458 class Bar(models.Model):
2459 pass
2462 def decorator(arg1, arg2, arg3):
2463 '''print decorated function call data to stdout.
2465 Usage:
2467 @decorator('arg1', 'arg2')
2468 def func(a, b, c=True):
2469 pass
2472 def wrap(f):
2473 print ('wrap')
2474 def wrapped_f(*args):
2475 print ('wrapped_f')
2476 print ('Decorator arguments:', arg1, arg2, arg3)
2477 f(*args)
2478 print ('called f(*args)')
2479 return wrapped_f
2480 return wrap
2483 class Baz(object):
2485 def a(self):
2486 pass
2488 def b(self):
2489 pass
2491 class Frob(object):
2493 def c(self):
2494 pass
2496 (goto-char (point-max))
2497 (should (equal
2498 (list (cons "Foo" (copy-marker 2))
2499 (cons "Bar" (copy-marker 38))
2500 (cons "decorator" (copy-marker 74))
2501 (cons "decorator.wrap" (copy-marker 254))
2502 (cons "decorator.wrap.wrapped_f" (copy-marker 294))
2503 (cons "Baz" (copy-marker 519))
2504 (cons "Baz.a" (copy-marker 539))
2505 (cons "Baz.b" (copy-marker 570))
2506 (cons "Baz.Frob" (copy-marker 601))
2507 (cons "Baz.Frob.c" (copy-marker 626)))
2508 (python-imenu-create-flat-index)))))
2510 (ert-deftest python-imenu-create-flat-index-2 ()
2511 (python-tests-with-temp-buffer
2513 class Foo(object):
2514 class Bar(object):
2515 def __init__(self):
2516 pass
2518 def __str__(self):
2519 pass
2521 def __init__(self):
2522 pass
2524 (goto-char (point-max))
2525 (should (equal
2526 (list
2527 (cons "Foo" (copy-marker 2))
2528 (cons "Foo.Bar" (copy-marker 21))
2529 (cons "Foo.Bar.__init__" (copy-marker 44))
2530 (cons "Foo.Bar.__str__" (copy-marker 90))
2531 (cons "Foo.__init__" (copy-marker 135)))
2532 (python-imenu-create-flat-index)))))
2535 ;;; Misc helpers
2537 (ert-deftest python-info-current-defun-1 ()
2538 (python-tests-with-temp-buffer
2540 def foo(a, b):
2542 (forward-line 1)
2543 (should (string= "foo" (python-info-current-defun)))
2544 (should (string= "def foo" (python-info-current-defun t)))
2545 (forward-line 1)
2546 (should (not (python-info-current-defun)))
2547 (indent-for-tab-command)
2548 (should (string= "foo" (python-info-current-defun)))
2549 (should (string= "def foo" (python-info-current-defun t)))))
2551 (ert-deftest python-info-current-defun-2 ()
2552 (python-tests-with-temp-buffer
2554 class C(object):
2556 def m(self):
2557 if True:
2558 return [i for i in range(3)]
2559 else:
2560 return []
2562 def b():
2563 do_b()
2565 def a():
2566 do_a()
2568 def c(self):
2569 do_c()
2571 (forward-line 1)
2572 (should (string= "C" (python-info-current-defun)))
2573 (should (string= "class C" (python-info-current-defun t)))
2574 (python-tests-look-at "return [i for ")
2575 (should (string= "C.m" (python-info-current-defun)))
2576 (should (string= "def C.m" (python-info-current-defun t)))
2577 (python-tests-look-at "def b():")
2578 (should (string= "C.m.b" (python-info-current-defun)))
2579 (should (string= "def C.m.b" (python-info-current-defun t)))
2580 (forward-line 2)
2581 (indent-for-tab-command)
2582 (python-indent-dedent-line-backspace 1)
2583 (should (string= "C.m" (python-info-current-defun)))
2584 (should (string= "def C.m" (python-info-current-defun t)))
2585 (python-tests-look-at "def c(self):")
2586 (forward-line -1)
2587 (indent-for-tab-command)
2588 (should (string= "C.m.a" (python-info-current-defun)))
2589 (should (string= "def C.m.a" (python-info-current-defun t)))
2590 (python-indent-dedent-line-backspace 1)
2591 (should (string= "C.m" (python-info-current-defun)))
2592 (should (string= "def C.m" (python-info-current-defun t)))
2593 (python-indent-dedent-line-backspace 1)
2594 (should (string= "C" (python-info-current-defun)))
2595 (should (string= "class C" (python-info-current-defun t)))
2596 (python-tests-look-at "def c(self):")
2597 (should (string= "C.c" (python-info-current-defun)))
2598 (should (string= "def C.c" (python-info-current-defun t)))
2599 (python-tests-look-at "do_c()")
2600 (should (string= "C.c" (python-info-current-defun)))
2601 (should (string= "def C.c" (python-info-current-defun t)))))
2603 (ert-deftest python-info-current-defun-3 ()
2604 (python-tests-with-temp-buffer
2606 def decoratorFunctionWithArguments(arg1, arg2, arg3):
2607 '''print decorated function call data to stdout.
2609 Usage:
2611 @decoratorFunctionWithArguments('arg1', 'arg2')
2612 def func(a, b, c=True):
2613 pass
2616 def wwrap(f):
2617 print 'Inside wwrap()'
2618 def wrapped_f(*args):
2619 print 'Inside wrapped_f()'
2620 print 'Decorator arguments:', arg1, arg2, arg3
2621 f(*args)
2622 print 'After f(*args)'
2623 return wrapped_f
2624 return wwrap
2626 (python-tests-look-at "def wwrap(f):")
2627 (forward-line -1)
2628 (should (not (python-info-current-defun)))
2629 (indent-for-tab-command 1)
2630 (should (string= (python-info-current-defun)
2631 "decoratorFunctionWithArguments"))
2632 (should (string= (python-info-current-defun t)
2633 "def decoratorFunctionWithArguments"))
2634 (python-tests-look-at "def wrapped_f(*args):")
2635 (should (string= (python-info-current-defun)
2636 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
2637 (should (string= (python-info-current-defun t)
2638 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
2639 (python-tests-look-at "return wrapped_f")
2640 (should (string= (python-info-current-defun)
2641 "decoratorFunctionWithArguments.wwrap"))
2642 (should (string= (python-info-current-defun t)
2643 "def decoratorFunctionWithArguments.wwrap"))
2644 (end-of-line 1)
2645 (python-tests-look-at "return wwrap")
2646 (should (string= (python-info-current-defun)
2647 "decoratorFunctionWithArguments"))
2648 (should (string= (python-info-current-defun t)
2649 "def decoratorFunctionWithArguments"))))
2651 (ert-deftest python-info-current-symbol-1 ()
2652 (python-tests-with-temp-buffer
2654 class C(object):
2656 def m(self):
2657 self.c()
2659 def c(self):
2660 print ('a')
2662 (python-tests-look-at "self.c()")
2663 (should (string= "self.c" (python-info-current-symbol)))
2664 (should (string= "C.c" (python-info-current-symbol t)))))
2666 (ert-deftest python-info-current-symbol-2 ()
2667 (python-tests-with-temp-buffer
2669 class C(object):
2671 class M(object):
2673 def a(self):
2674 self.c()
2676 def c(self):
2677 pass
2679 (python-tests-look-at "self.c()")
2680 (should (string= "self.c" (python-info-current-symbol)))
2681 (should (string= "C.M.c" (python-info-current-symbol t)))))
2683 (ert-deftest python-info-current-symbol-3 ()
2684 "Keywords should not be considered symbols."
2685 :expected-result :failed
2686 (python-tests-with-temp-buffer
2688 class C(object):
2689 pass
2691 ;; FIXME: keywords are not symbols.
2692 (python-tests-look-at "class C")
2693 (should (not (python-info-current-symbol)))
2694 (should (not (python-info-current-symbol t)))
2695 (python-tests-look-at "C(object)")
2696 (should (string= "C" (python-info-current-symbol)))
2697 (should (string= "class C" (python-info-current-symbol t)))))
2699 (ert-deftest python-info-statement-starts-block-p-1 ()
2700 (python-tests-with-temp-buffer
2702 def long_function_name(
2703 var_one, var_two, var_three,
2704 var_four):
2705 print (var_one)
2707 (python-tests-look-at "def long_function_name")
2708 (should (python-info-statement-starts-block-p))
2709 (python-tests-look-at "print (var_one)")
2710 (python-util-forward-comment -1)
2711 (should (python-info-statement-starts-block-p))))
2713 (ert-deftest python-info-statement-starts-block-p-2 ()
2714 (python-tests-with-temp-buffer
2716 if width == 0 and height == 0 and \\\\
2717 color == 'red' and emphasis == 'strong' or \\\\
2718 highlight > 100:
2719 raise ValueError('sorry, you lose')
2721 (python-tests-look-at "if width == 0 and")
2722 (should (python-info-statement-starts-block-p))
2723 (python-tests-look-at "raise ValueError(")
2724 (python-util-forward-comment -1)
2725 (should (python-info-statement-starts-block-p))))
2727 (ert-deftest python-info-statement-ends-block-p-1 ()
2728 (python-tests-with-temp-buffer
2730 def long_function_name(
2731 var_one, var_two, var_three,
2732 var_four):
2733 print (var_one)
2735 (python-tests-look-at "print (var_one)")
2736 (should (python-info-statement-ends-block-p))))
2738 (ert-deftest python-info-statement-ends-block-p-2 ()
2739 (python-tests-with-temp-buffer
2741 if width == 0 and height == 0 and \\\\
2742 color == 'red' and emphasis == 'strong' or \\\\
2743 highlight > 100:
2744 raise ValueError(
2745 'sorry, you lose'
2749 (python-tests-look-at "raise ValueError(")
2750 (should (python-info-statement-ends-block-p))))
2752 (ert-deftest python-info-beginning-of-statement-p-1 ()
2753 (python-tests-with-temp-buffer
2755 def long_function_name(
2756 var_one, var_two, var_three,
2757 var_four):
2758 print (var_one)
2760 (python-tests-look-at "def long_function_name")
2761 (should (python-info-beginning-of-statement-p))
2762 (forward-char 10)
2763 (should (not (python-info-beginning-of-statement-p)))
2764 (python-tests-look-at "print (var_one)")
2765 (should (python-info-beginning-of-statement-p))
2766 (goto-char (line-beginning-position))
2767 (should (not (python-info-beginning-of-statement-p)))))
2769 (ert-deftest python-info-beginning-of-statement-p-2 ()
2770 (python-tests-with-temp-buffer
2772 if width == 0 and height == 0 and \\\\
2773 color == 'red' and emphasis == 'strong' or \\\\
2774 highlight > 100:
2775 raise ValueError(
2776 'sorry, you lose'
2780 (python-tests-look-at "if width == 0 and")
2781 (should (python-info-beginning-of-statement-p))
2782 (forward-char 10)
2783 (should (not (python-info-beginning-of-statement-p)))
2784 (python-tests-look-at "raise ValueError(")
2785 (should (python-info-beginning-of-statement-p))
2786 (goto-char (line-beginning-position))
2787 (should (not (python-info-beginning-of-statement-p)))))
2789 (ert-deftest python-info-end-of-statement-p-1 ()
2790 (python-tests-with-temp-buffer
2792 def long_function_name(
2793 var_one, var_two, var_three,
2794 var_four):
2795 print (var_one)
2797 (python-tests-look-at "def long_function_name")
2798 (should (not (python-info-end-of-statement-p)))
2799 (end-of-line)
2800 (should (not (python-info-end-of-statement-p)))
2801 (python-tests-look-at "print (var_one)")
2802 (python-util-forward-comment -1)
2803 (should (python-info-end-of-statement-p))
2804 (python-tests-look-at "print (var_one)")
2805 (should (not (python-info-end-of-statement-p)))
2806 (end-of-line)
2807 (should (python-info-end-of-statement-p))))
2809 (ert-deftest python-info-end-of-statement-p-2 ()
2810 (python-tests-with-temp-buffer
2812 if width == 0 and height == 0 and \\\\
2813 color == 'red' and emphasis == 'strong' or \\\\
2814 highlight > 100:
2815 raise ValueError(
2816 'sorry, you lose'
2820 (python-tests-look-at "if width == 0 and")
2821 (should (not (python-info-end-of-statement-p)))
2822 (end-of-line)
2823 (should (not (python-info-end-of-statement-p)))
2824 (python-tests-look-at "raise ValueError(")
2825 (python-util-forward-comment -1)
2826 (should (python-info-end-of-statement-p))
2827 (python-tests-look-at "raise ValueError(")
2828 (should (not (python-info-end-of-statement-p)))
2829 (end-of-line)
2830 (should (not (python-info-end-of-statement-p)))
2831 (goto-char (point-max))
2832 (python-util-forward-comment -1)
2833 (should (python-info-end-of-statement-p))))
2835 (ert-deftest python-info-beginning-of-block-p-1 ()
2836 (python-tests-with-temp-buffer
2838 def long_function_name(
2839 var_one, var_two, var_three,
2840 var_four):
2841 print (var_one)
2843 (python-tests-look-at "def long_function_name")
2844 (should (python-info-beginning-of-block-p))
2845 (python-tests-look-at "var_one, var_two, var_three,")
2846 (should (not (python-info-beginning-of-block-p)))
2847 (python-tests-look-at "print (var_one)")
2848 (should (not (python-info-beginning-of-block-p)))))
2850 (ert-deftest python-info-beginning-of-block-p-2 ()
2851 (python-tests-with-temp-buffer
2853 if width == 0 and height == 0 and \\\\
2854 color == 'red' and emphasis == 'strong' or \\\\
2855 highlight > 100:
2856 raise ValueError(
2857 'sorry, you lose'
2861 (python-tests-look-at "if width == 0 and")
2862 (should (python-info-beginning-of-block-p))
2863 (python-tests-look-at "color == 'red' and emphasis")
2864 (should (not (python-info-beginning-of-block-p)))
2865 (python-tests-look-at "raise ValueError(")
2866 (should (not (python-info-beginning-of-block-p)))))
2868 (ert-deftest python-info-end-of-block-p-1 ()
2869 (python-tests-with-temp-buffer
2871 def long_function_name(
2872 var_one, var_two, var_three,
2873 var_four):
2874 print (var_one)
2876 (python-tests-look-at "def long_function_name")
2877 (should (not (python-info-end-of-block-p)))
2878 (python-tests-look-at "var_one, var_two, var_three,")
2879 (should (not (python-info-end-of-block-p)))
2880 (python-tests-look-at "var_four):")
2881 (end-of-line)
2882 (should (not (python-info-end-of-block-p)))
2883 (python-tests-look-at "print (var_one)")
2884 (should (not (python-info-end-of-block-p)))
2885 (end-of-line 1)
2886 (should (python-info-end-of-block-p))))
2888 (ert-deftest python-info-end-of-block-p-2 ()
2889 (python-tests-with-temp-buffer
2891 if width == 0 and height == 0 and \\\\
2892 color == 'red' and emphasis == 'strong' or \\\\
2893 highlight > 100:
2894 raise ValueError(
2895 'sorry, you lose'
2899 (python-tests-look-at "if width == 0 and")
2900 (should (not (python-info-end-of-block-p)))
2901 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2902 (should (not (python-info-end-of-block-p)))
2903 (python-tests-look-at "highlight > 100:")
2904 (end-of-line)
2905 (should (not (python-info-end-of-block-p)))
2906 (python-tests-look-at "raise ValueError(")
2907 (should (not (python-info-end-of-block-p)))
2908 (end-of-line 1)
2909 (should (not (python-info-end-of-block-p)))
2910 (goto-char (point-max))
2911 (python-util-forward-comment -1)
2912 (should (python-info-end-of-block-p))))
2914 (ert-deftest python-info-dedenter-opening-block-position-1 ()
2915 (python-tests-with-temp-buffer
2917 if request.user.is_authenticated():
2918 try:
2919 profile = request.user.get_profile()
2920 except Profile.DoesNotExist:
2921 profile = Profile.objects.create(user=request.user)
2922 else:
2923 if profile.stats:
2924 profile.recalculate_stats()
2925 else:
2926 profile.clear_stats()
2927 finally:
2928 profile.views += 1
2929 profile.save()
2931 (python-tests-look-at "try:")
2932 (should (not (python-info-dedenter-opening-block-position)))
2933 (python-tests-look-at "except Profile.DoesNotExist:")
2934 (should (= (python-tests-look-at "try:" -1 t)
2935 (python-info-dedenter-opening-block-position)))
2936 (python-tests-look-at "else:")
2937 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -1 t)
2938 (python-info-dedenter-opening-block-position)))
2939 (python-tests-look-at "if profile.stats:")
2940 (should (not (python-info-dedenter-opening-block-position)))
2941 (python-tests-look-at "else:")
2942 (should (= (python-tests-look-at "if profile.stats:" -1 t)
2943 (python-info-dedenter-opening-block-position)))
2944 (python-tests-look-at "finally:")
2945 (should (= (python-tests-look-at "else:" -2 t)
2946 (python-info-dedenter-opening-block-position)))))
2948 (ert-deftest python-info-dedenter-opening-block-position-2 ()
2949 (python-tests-with-temp-buffer
2951 if request.user.is_authenticated():
2952 profile = Profile.objects.get_or_create(user=request.user)
2953 if profile.stats:
2954 profile.recalculate_stats()
2956 data = {
2957 'else': 'do it'
2959 'else'
2961 (python-tests-look-at "'else': 'do it'")
2962 (should (not (python-info-dedenter-opening-block-position)))
2963 (python-tests-look-at "'else'")
2964 (should (not (python-info-dedenter-opening-block-position)))))
2966 (ert-deftest python-info-dedenter-opening-block-position-3 ()
2967 (python-tests-with-temp-buffer
2969 if save:
2970 try:
2971 write_to_disk(data)
2972 except IOError:
2973 msg = 'Error saving to disk'
2974 message(msg)
2975 logger.exception(msg)
2976 except Exception:
2977 if hide_details:
2978 logger.exception('Unhandled exception')
2979 else
2980 finally:
2981 data.free()
2983 (python-tests-look-at "try:")
2984 (should (not (python-info-dedenter-opening-block-position)))
2986 (python-tests-look-at "except IOError:")
2987 (should (= (python-tests-look-at "try:" -1 t)
2988 (python-info-dedenter-opening-block-position)))
2990 (python-tests-look-at "except Exception:")
2991 (should (= (python-tests-look-at "except IOError:" -1 t)
2992 (python-info-dedenter-opening-block-position)))
2994 (python-tests-look-at "if hide_details:")
2995 (should (not (python-info-dedenter-opening-block-position)))
2997 ;; check indentation modifies the detected opening block
2998 (python-tests-look-at "else")
2999 (should (= (python-tests-look-at "if hide_details:" -1 t)
3000 (python-info-dedenter-opening-block-position)))
3002 (indent-line-to 8)
3003 (should (= (python-tests-look-at "if hide_details:" -1 t)
3004 (python-info-dedenter-opening-block-position)))
3006 (indent-line-to 4)
3007 (should (= (python-tests-look-at "except Exception:" -1 t)
3008 (python-info-dedenter-opening-block-position)))
3010 (indent-line-to 0)
3011 (should (= (python-tests-look-at "if save:" -1 t)
3012 (python-info-dedenter-opening-block-position)))))
3014 (ert-deftest python-info-dedenter-opening-block-positions-1 ()
3015 (python-tests-with-temp-buffer
3017 if save:
3018 try:
3019 write_to_disk(data)
3020 except IOError:
3021 msg = 'Error saving to disk'
3022 message(msg)
3023 logger.exception(msg)
3024 except Exception:
3025 if hide_details:
3026 logger.exception('Unhandled exception')
3027 else
3028 finally:
3029 data.free()
3031 (python-tests-look-at "try:")
3032 (should (not (python-info-dedenter-opening-block-positions)))
3034 (python-tests-look-at "except IOError:")
3035 (should
3036 (equal (list
3037 (python-tests-look-at "try:" -1 t))
3038 (python-info-dedenter-opening-block-positions)))
3040 (python-tests-look-at "except Exception:")
3041 (should
3042 (equal (list
3043 (python-tests-look-at "except IOError:" -1 t))
3044 (python-info-dedenter-opening-block-positions)))
3046 (python-tests-look-at "if hide_details:")
3047 (should (not (python-info-dedenter-opening-block-positions)))
3049 ;; check indentation does not modify the detected opening blocks
3050 (python-tests-look-at "else")
3051 (should
3052 (equal (list
3053 (python-tests-look-at "if hide_details:" -1 t)
3054 (python-tests-look-at "except Exception:" -1 t)
3055 (python-tests-look-at "if save:" -1 t))
3056 (python-info-dedenter-opening-block-positions)))
3058 (indent-line-to 8)
3059 (should
3060 (equal (list
3061 (python-tests-look-at "if hide_details:" -1 t)
3062 (python-tests-look-at "except Exception:" -1 t)
3063 (python-tests-look-at "if save:" -1 t))
3064 (python-info-dedenter-opening-block-positions)))
3066 (indent-line-to 4)
3067 (should
3068 (equal (list
3069 (python-tests-look-at "if hide_details:" -1 t)
3070 (python-tests-look-at "except Exception:" -1 t)
3071 (python-tests-look-at "if save:" -1 t))
3072 (python-info-dedenter-opening-block-positions)))
3074 (indent-line-to 0)
3075 (should
3076 (equal (list
3077 (python-tests-look-at "if hide_details:" -1 t)
3078 (python-tests-look-at "except Exception:" -1 t)
3079 (python-tests-look-at "if save:" -1 t))
3080 (python-info-dedenter-opening-block-positions)))))
3082 (ert-deftest python-info-dedenter-opening-block-positions-2 ()
3083 "Test detection of opening blocks for elif."
3084 (python-tests-with-temp-buffer
3086 if var:
3087 if var2:
3088 something()
3089 elif var3:
3090 something_else()
3091 elif
3093 (python-tests-look-at "elif var3:")
3094 (should
3095 (equal (list
3096 (python-tests-look-at "if var2:" -1 t)
3097 (python-tests-look-at "if var:" -1 t))
3098 (python-info-dedenter-opening-block-positions)))
3100 (python-tests-look-at "elif\n")
3101 (should
3102 (equal (list
3103 (python-tests-look-at "elif var3:" -1 t)
3104 (python-tests-look-at "if var:" -1 t))
3105 (python-info-dedenter-opening-block-positions)))))
3107 (ert-deftest python-info-dedenter-opening-block-positions-3 ()
3108 "Test detection of opening blocks for else."
3109 (python-tests-with-temp-buffer
3111 try:
3112 something()
3113 except:
3114 if var:
3115 if var2:
3116 something()
3117 elif var3:
3118 something_else()
3119 else
3121 if var4:
3122 while var5:
3123 var4.pop()
3124 else
3126 for value in var6:
3127 if value > 0:
3128 print value
3129 else
3131 (python-tests-look-at "else\n")
3132 (should
3133 (equal (list
3134 (python-tests-look-at "elif var3:" -1 t)
3135 (python-tests-look-at "if var:" -1 t)
3136 (python-tests-look-at "except:" -1 t))
3137 (python-info-dedenter-opening-block-positions)))
3139 (python-tests-look-at "else\n")
3140 (should
3141 (equal (list
3142 (python-tests-look-at "while var5:" -1 t)
3143 (python-tests-look-at "if var4:" -1 t))
3144 (python-info-dedenter-opening-block-positions)))
3146 (python-tests-look-at "else\n")
3147 (should
3148 (equal (list
3149 (python-tests-look-at "if value > 0:" -1 t)
3150 (python-tests-look-at "for value in var6:" -1 t)
3151 (python-tests-look-at "if var4:" -1 t))
3152 (python-info-dedenter-opening-block-positions)))))
3154 (ert-deftest python-info-dedenter-opening-block-positions-4 ()
3155 "Test detection of opening blocks for except."
3156 (python-tests-with-temp-buffer
3158 try:
3159 something()
3160 except ValueError:
3161 something_else()
3162 except
3164 (python-tests-look-at "except ValueError:")
3165 (should
3166 (equal (list (python-tests-look-at "try:" -1 t))
3167 (python-info-dedenter-opening-block-positions)))
3169 (python-tests-look-at "except\n")
3170 (should
3171 (equal (list (python-tests-look-at "except ValueError:" -1 t))
3172 (python-info-dedenter-opening-block-positions)))))
3174 (ert-deftest python-info-dedenter-opening-block-positions-5 ()
3175 "Test detection of opening blocks for finally."
3176 (python-tests-with-temp-buffer
3178 try:
3179 something()
3180 finally
3182 try:
3183 something_else()
3184 except:
3185 logger.exception('something went wrong')
3186 finally
3188 try:
3189 something_else_else()
3190 except Exception:
3191 logger.exception('something else went wrong')
3192 else:
3193 print ('all good')
3194 finally
3196 (python-tests-look-at "finally\n")
3197 (should
3198 (equal (list (python-tests-look-at "try:" -1 t))
3199 (python-info-dedenter-opening-block-positions)))
3201 (python-tests-look-at "finally\n")
3202 (should
3203 (equal (list (python-tests-look-at "except:" -1 t))
3204 (python-info-dedenter-opening-block-positions)))
3206 (python-tests-look-at "finally\n")
3207 (should
3208 (equal (list (python-tests-look-at "else:" -1 t))
3209 (python-info-dedenter-opening-block-positions)))))
3211 (ert-deftest python-info-dedenter-opening-block-message-1 ()
3212 "Test dedenters inside strings are ignored."
3213 (python-tests-with-temp-buffer
3214 "'''
3215 try:
3216 something()
3217 except:
3218 logger.exception('something went wrong')
3221 (python-tests-look-at "except\n")
3222 (should (not (python-info-dedenter-opening-block-message)))))
3224 (ert-deftest python-info-dedenter-opening-block-message-2 ()
3225 "Test except keyword."
3226 (python-tests-with-temp-buffer
3228 try:
3229 something()
3230 except:
3231 logger.exception('something went wrong')
3233 (python-tests-look-at "except:")
3234 (should (string=
3235 "Closes try:"
3236 (substring-no-properties
3237 (python-info-dedenter-opening-block-message))))
3238 (end-of-line)
3239 (should (string=
3240 "Closes try:"
3241 (substring-no-properties
3242 (python-info-dedenter-opening-block-message))))))
3244 (ert-deftest python-info-dedenter-opening-block-message-3 ()
3245 "Test else keyword."
3246 (python-tests-with-temp-buffer
3248 try:
3249 something()
3250 except:
3251 logger.exception('something went wrong')
3252 else:
3253 logger.debug('all good')
3255 (python-tests-look-at "else:")
3256 (should (string=
3257 "Closes except:"
3258 (substring-no-properties
3259 (python-info-dedenter-opening-block-message))))
3260 (end-of-line)
3261 (should (string=
3262 "Closes except:"
3263 (substring-no-properties
3264 (python-info-dedenter-opening-block-message))))))
3266 (ert-deftest python-info-dedenter-opening-block-message-4 ()
3267 "Test finally keyword."
3268 (python-tests-with-temp-buffer
3270 try:
3271 something()
3272 except:
3273 logger.exception('something went wrong')
3274 else:
3275 logger.debug('all good')
3276 finally:
3277 clean()
3279 (python-tests-look-at "finally:")
3280 (should (string=
3281 "Closes else:"
3282 (substring-no-properties
3283 (python-info-dedenter-opening-block-message))))
3284 (end-of-line)
3285 (should (string=
3286 "Closes else:"
3287 (substring-no-properties
3288 (python-info-dedenter-opening-block-message))))))
3290 (ert-deftest python-info-dedenter-opening-block-message-5 ()
3291 "Test elif keyword."
3292 (python-tests-with-temp-buffer
3294 if a:
3295 something()
3296 elif b:
3298 (python-tests-look-at "elif b:")
3299 (should (string=
3300 "Closes if a:"
3301 (substring-no-properties
3302 (python-info-dedenter-opening-block-message))))
3303 (end-of-line)
3304 (should (string=
3305 "Closes if a:"
3306 (substring-no-properties
3307 (python-info-dedenter-opening-block-message))))))
3310 (ert-deftest python-info-dedenter-statement-p-1 ()
3311 "Test dedenters inside strings are ignored."
3312 (python-tests-with-temp-buffer
3313 "'''
3314 try:
3315 something()
3316 except:
3317 logger.exception('something went wrong')
3320 (python-tests-look-at "except\n")
3321 (should (not (python-info-dedenter-statement-p)))))
3323 (ert-deftest python-info-dedenter-statement-p-2 ()
3324 "Test except keyword."
3325 (python-tests-with-temp-buffer
3327 try:
3328 something()
3329 except:
3330 logger.exception('something went wrong')
3332 (python-tests-look-at "except:")
3333 (should (= (point) (python-info-dedenter-statement-p)))
3334 (end-of-line)
3335 (should (= (save-excursion
3336 (back-to-indentation)
3337 (point))
3338 (python-info-dedenter-statement-p)))))
3340 (ert-deftest python-info-dedenter-statement-p-3 ()
3341 "Test else keyword."
3342 (python-tests-with-temp-buffer
3344 try:
3345 something()
3346 except:
3347 logger.exception('something went wrong')
3348 else:
3349 logger.debug('all good')
3351 (python-tests-look-at "else:")
3352 (should (= (point) (python-info-dedenter-statement-p)))
3353 (end-of-line)
3354 (should (= (save-excursion
3355 (back-to-indentation)
3356 (point))
3357 (python-info-dedenter-statement-p)))))
3359 (ert-deftest python-info-dedenter-statement-p-4 ()
3360 "Test finally keyword."
3361 (python-tests-with-temp-buffer
3363 try:
3364 something()
3365 except:
3366 logger.exception('something went wrong')
3367 else:
3368 logger.debug('all good')
3369 finally:
3370 clean()
3372 (python-tests-look-at "finally:")
3373 (should (= (point) (python-info-dedenter-statement-p)))
3374 (end-of-line)
3375 (should (= (save-excursion
3376 (back-to-indentation)
3377 (point))
3378 (python-info-dedenter-statement-p)))))
3380 (ert-deftest python-info-dedenter-statement-p-5 ()
3381 "Test elif keyword."
3382 (python-tests-with-temp-buffer
3384 if a:
3385 something()
3386 elif b:
3388 (python-tests-look-at "elif b:")
3389 (should (= (point) (python-info-dedenter-statement-p)))
3390 (end-of-line)
3391 (should (= (save-excursion
3392 (back-to-indentation)
3393 (point))
3394 (python-info-dedenter-statement-p)))))
3396 (ert-deftest python-info-line-ends-backslash-p-1 ()
3397 (python-tests-with-temp-buffer
3399 objects = Thing.objects.all() \\\\
3400 .filter(
3401 type='toy',
3402 status='bought'
3403 ) \\\\
3404 .aggregate(
3405 Sum('amount')
3406 ) \\\\
3407 .values_list()
3409 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
3410 (should (python-info-line-ends-backslash-p 3))
3411 (should (python-info-line-ends-backslash-p 4))
3412 (should (python-info-line-ends-backslash-p 5))
3413 (should (python-info-line-ends-backslash-p 6)) ; ) \...
3414 (should (python-info-line-ends-backslash-p 7))
3415 (should (python-info-line-ends-backslash-p 8))
3416 (should (python-info-line-ends-backslash-p 9))
3417 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
3419 (ert-deftest python-info-beginning-of-backslash-1 ()
3420 (python-tests-with-temp-buffer
3422 objects = Thing.objects.all() \\\\
3423 .filter(
3424 type='toy',
3425 status='bought'
3426 ) \\\\
3427 .aggregate(
3428 Sum('amount')
3429 ) \\\\
3430 .values_list()
3432 (let ((first 2)
3433 (second (python-tests-look-at ".filter("))
3434 (third (python-tests-look-at ".aggregate(")))
3435 (should (= first (python-info-beginning-of-backslash 2)))
3436 (should (= second (python-info-beginning-of-backslash 3)))
3437 (should (= second (python-info-beginning-of-backslash 4)))
3438 (should (= second (python-info-beginning-of-backslash 5)))
3439 (should (= second (python-info-beginning-of-backslash 6)))
3440 (should (= third (python-info-beginning-of-backslash 7)))
3441 (should (= third (python-info-beginning-of-backslash 8)))
3442 (should (= third (python-info-beginning-of-backslash 9)))
3443 (should (not (python-info-beginning-of-backslash 10))))))
3445 (ert-deftest python-info-continuation-line-p-1 ()
3446 (python-tests-with-temp-buffer
3448 if width == 0 and height == 0 and \\\\
3449 color == 'red' and emphasis == 'strong' or \\\\
3450 highlight > 100:
3451 raise ValueError(
3452 'sorry, you lose'
3456 (python-tests-look-at "if width == 0 and height == 0 and")
3457 (should (not (python-info-continuation-line-p)))
3458 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3459 (should (python-info-continuation-line-p))
3460 (python-tests-look-at "highlight > 100:")
3461 (should (python-info-continuation-line-p))
3462 (python-tests-look-at "raise ValueError(")
3463 (should (not (python-info-continuation-line-p)))
3464 (python-tests-look-at "'sorry, you lose'")
3465 (should (python-info-continuation-line-p))
3466 (forward-line 1)
3467 (should (python-info-continuation-line-p))
3468 (python-tests-look-at ")")
3469 (should (python-info-continuation-line-p))
3470 (forward-line 1)
3471 (should (not (python-info-continuation-line-p)))))
3473 (ert-deftest python-info-block-continuation-line-p-1 ()
3474 (python-tests-with-temp-buffer
3476 if width == 0 and height == 0 and \\\\
3477 color == 'red' and emphasis == 'strong' or \\\\
3478 highlight > 100:
3479 raise ValueError(
3480 'sorry, you lose'
3484 (python-tests-look-at "if width == 0 and")
3485 (should (not (python-info-block-continuation-line-p)))
3486 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3487 (should (= (python-info-block-continuation-line-p)
3488 (python-tests-look-at "if width == 0 and" -1 t)))
3489 (python-tests-look-at "highlight > 100:")
3490 (should (not (python-info-block-continuation-line-p)))))
3492 (ert-deftest python-info-block-continuation-line-p-2 ()
3493 (python-tests-with-temp-buffer
3495 def foo(a,
3498 pass
3500 (python-tests-look-at "def foo(a,")
3501 (should (not (python-info-block-continuation-line-p)))
3502 (python-tests-look-at "b,")
3503 (should (= (python-info-block-continuation-line-p)
3504 (python-tests-look-at "def foo(a," -1 t)))
3505 (python-tests-look-at "c):")
3506 (should (not (python-info-block-continuation-line-p)))))
3508 (ert-deftest python-info-assignment-continuation-line-p-1 ()
3509 (python-tests-with-temp-buffer
3511 data = foo(), bar() \\\\
3512 baz(), 4 \\\\
3513 5, 6
3515 (python-tests-look-at "data = foo(), bar()")
3516 (should (not (python-info-assignment-continuation-line-p)))
3517 (python-tests-look-at "baz(), 4")
3518 (should (= (python-info-assignment-continuation-line-p)
3519 (python-tests-look-at "foo()," -1 t)))
3520 (python-tests-look-at "5, 6")
3521 (should (not (python-info-assignment-continuation-line-p)))))
3523 (ert-deftest python-info-assignment-continuation-line-p-2 ()
3524 (python-tests-with-temp-buffer
3526 data = (foo(), bar()
3527 baz(), 4
3528 5, 6)
3530 (python-tests-look-at "data = (foo(), bar()")
3531 (should (not (python-info-assignment-continuation-line-p)))
3532 (python-tests-look-at "baz(), 4")
3533 (should (= (python-info-assignment-continuation-line-p)
3534 (python-tests-look-at "(foo()," -1 t)))
3535 (python-tests-look-at "5, 6)")
3536 (should (not (python-info-assignment-continuation-line-p)))))
3538 (ert-deftest python-info-looking-at-beginning-of-defun-1 ()
3539 (python-tests-with-temp-buffer
3541 def decorat0r(deff):
3542 '''decorates stuff.
3544 @decorat0r
3545 def foo(arg):
3548 def wrap():
3549 deff()
3550 return wwrap
3552 (python-tests-look-at "def decorat0r(deff):")
3553 (should (python-info-looking-at-beginning-of-defun))
3554 (python-tests-look-at "def foo(arg):")
3555 (should (not (python-info-looking-at-beginning-of-defun)))
3556 (python-tests-look-at "def wrap():")
3557 (should (python-info-looking-at-beginning-of-defun))
3558 (python-tests-look-at "deff()")
3559 (should (not (python-info-looking-at-beginning-of-defun)))))
3561 (ert-deftest python-info-current-line-comment-p-1 ()
3562 (python-tests-with-temp-buffer
3564 # this is a comment
3565 foo = True # another comment
3566 '#this is a string'
3567 if foo:
3568 # more comments
3569 print ('bar') # print bar
3571 (python-tests-look-at "# this is a comment")
3572 (should (python-info-current-line-comment-p))
3573 (python-tests-look-at "foo = True # another comment")
3574 (should (not (python-info-current-line-comment-p)))
3575 (python-tests-look-at "'#this is a string'")
3576 (should (not (python-info-current-line-comment-p)))
3577 (python-tests-look-at "# more comments")
3578 (should (python-info-current-line-comment-p))
3579 (python-tests-look-at "print ('bar') # print bar")
3580 (should (not (python-info-current-line-comment-p)))))
3582 (ert-deftest python-info-current-line-empty-p ()
3583 (python-tests-with-temp-buffer
3585 # this is a comment
3587 foo = True # another comment
3589 (should (python-info-current-line-empty-p))
3590 (python-tests-look-at "# this is a comment")
3591 (should (not (python-info-current-line-empty-p)))
3592 (forward-line 1)
3593 (should (python-info-current-line-empty-p))))
3596 ;;; Utility functions
3598 (ert-deftest python-util-goto-line-1 ()
3599 (python-tests-with-temp-buffer
3600 (concat
3601 "# a comment
3602 # another comment
3603 def foo(a, b, c):
3604 pass" (make-string 20 ?\n))
3605 (python-util-goto-line 10)
3606 (should (= (line-number-at-pos) 10))
3607 (python-util-goto-line 20)
3608 (should (= (line-number-at-pos) 20))))
3610 (ert-deftest python-util-clone-local-variables-1 ()
3611 (let ((buffer (generate-new-buffer
3612 "python-util-clone-local-variables-1"))
3613 (varcons
3614 '((python-fill-docstring-style . django)
3615 (python-shell-interpreter . "python")
3616 (python-shell-interpreter-args . "manage.py shell")
3617 (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
3618 (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
3619 (python-shell-extra-pythonpaths "/home/user/pylib/")
3620 (python-shell-completion-setup-code
3621 . "from IPython.core.completerlib import module_completion")
3622 (python-shell-completion-string-code
3623 . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
3624 (python-shell-virtualenv-path
3625 . "/home/user/.virtualenvs/project"))))
3626 (with-current-buffer buffer
3627 (kill-all-local-variables)
3628 (dolist (ccons varcons)
3629 (set (make-local-variable (car ccons)) (cdr ccons))))
3630 (python-tests-with-temp-buffer
3632 (python-util-clone-local-variables buffer)
3633 (dolist (ccons varcons)
3634 (should
3635 (equal (symbol-value (car ccons)) (cdr ccons)))))
3636 (kill-buffer buffer)))
3638 (ert-deftest python-util-strip-string-1 ()
3639 (should (string= (python-util-strip-string "\t\r\n str") "str"))
3640 (should (string= (python-util-strip-string "str \n\r") "str"))
3641 (should (string= (python-util-strip-string "\t\r\n str \n\r ") "str"))
3642 (should
3643 (string= (python-util-strip-string "\n str \nin \tg \n\r") "str \nin \tg"))
3644 (should (string= (python-util-strip-string "\n \t \n\r ") ""))
3645 (should (string= (python-util-strip-string "") "")))
3647 (ert-deftest python-util-forward-comment-1 ()
3648 (python-tests-with-temp-buffer
3649 (concat
3650 "# a comment
3651 # another comment
3652 # bad indented comment
3653 # more comments" (make-string 9999 ?\n))
3654 (python-util-forward-comment 1)
3655 (should (= (point) (point-max)))
3656 (python-util-forward-comment -1)
3657 (should (= (point) (point-min)))))
3659 (ert-deftest python-util-valid-regexp-p-1 ()
3660 (should (python-util-valid-regexp-p ""))
3661 (should (python-util-valid-regexp-p python-shell-prompt-regexp))
3662 (should (not (python-util-valid-regexp-p "\\("))))
3665 ;;; Electricity
3667 (ert-deftest python-parens-electric-indent-1 ()
3668 (require 'electric)
3669 (let ((eim electric-indent-mode))
3670 (unwind-protect
3671 (progn
3672 (python-tests-with-temp-buffer
3674 from django.conf.urls import patterns, include, url
3676 from django.contrib import admin
3678 from myapp import views
3681 urlpatterns = patterns('',
3682 url(r'^$', views.index
3685 (electric-indent-mode 1)
3686 (python-tests-look-at "views.index")
3687 (end-of-line)
3689 ;; Inserting commas within the same line should leave
3690 ;; indentation unchanged.
3691 (python-tests-self-insert ",")
3692 (should (= (current-indentation) 4))
3694 ;; As well as any other input happening within the same
3695 ;; set of parens.
3696 (python-tests-self-insert " name='index')")
3697 (should (= (current-indentation) 4))
3699 ;; But a comma outside it, should trigger indentation.
3700 (python-tests-self-insert ",")
3701 (should (= (current-indentation) 23))
3703 ;; Newline indents to the first argument column
3704 (python-tests-self-insert "\n")
3705 (should (= (current-indentation) 23))
3707 ;; All this input must not change indentation
3708 (indent-line-to 4)
3709 (python-tests-self-insert "url(r'^/login$', views.login)")
3710 (should (= (current-indentation) 4))
3712 ;; But this comma does
3713 (python-tests-self-insert ",")
3714 (should (= (current-indentation) 23))))
3715 (or eim (electric-indent-mode -1)))))
3717 (ert-deftest python-triple-quote-pairing ()
3718 (require 'electric)
3719 (let ((epm electric-pair-mode))
3720 (unwind-protect
3721 (progn
3722 (python-tests-with-temp-buffer
3723 "\"\"\n"
3724 (or epm (electric-pair-mode 1))
3725 (goto-char (1- (point-max)))
3726 (python-tests-self-insert ?\")
3727 (should (string= (buffer-string)
3728 "\"\"\"\"\"\"\n"))
3729 (should (= (point) 4)))
3730 (python-tests-with-temp-buffer
3731 "\n"
3732 (python-tests-self-insert (list ?\" ?\" ?\"))
3733 (should (string= (buffer-string)
3734 "\"\"\"\"\"\"\n"))
3735 (should (= (point) 4)))
3736 (python-tests-with-temp-buffer
3737 "\"\n\"\"\n"
3738 (goto-char (1- (point-max)))
3739 (python-tests-self-insert ?\")
3740 (should (= (point) (1- (point-max))))
3741 (should (string= (buffer-string)
3742 "\"\n\"\"\"\n"))))
3743 (or epm (electric-pair-mode -1)))))
3746 (provide 'python-tests)
3748 ;; Local Variables:
3749 ;; coding: utf-8
3750 ;; indent-tabs-mode: nil
3751 ;; End:
3753 ;;; python-tests.el ends here