.gitignore tweaks
[emacs.git] / test / automated / python-tests.el
blobae4323ba8af249c08e1f6f5b1370fe399880386f
1 ;;; python-tests.el --- Test suite for python.el
3 ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20 ;;; Commentary:
22 ;;; Code:
24 (require 'ert)
25 (require 'python)
27 ;; Dependencies for testing:
28 (require 'electric)
29 (require 'hideshow)
32 (defmacro python-tests-with-temp-buffer (contents &rest body)
33 "Create a `python-mode' enabled temp buffer with CONTENTS.
34 BODY is code to be executed within the temp buffer. Point is
35 always located at the beginning of buffer."
36 (declare (indent 1) (debug t))
37 `(with-temp-buffer
38 (python-mode)
39 (insert ,contents)
40 (goto-char (point-min))
41 ,@body))
43 (defmacro python-tests-with-temp-file (contents &rest body)
44 "Create a `python-mode' enabled file with CONTENTS.
45 BODY is code to be executed within the temp buffer. Point is
46 always located at the beginning of buffer."
47 (declare (indent 1) (debug t))
48 ;; temp-file never actually used for anything?
49 `(let* ((temp-file (make-temp-file "python-tests" nil ".py"))
50 (buffer (find-file-noselect temp-file)))
51 (unwind-protect
52 (with-current-buffer buffer
53 (python-mode)
54 (insert ,contents)
55 (goto-char (point-min))
56 ,@body)
57 (and buffer (kill-buffer buffer))
58 (delete-file temp-file))))
60 (defun python-tests-look-at (string &optional num restore-point)
61 "Move point at beginning of STRING in the current buffer.
62 Optional argument NUM defaults to 1 and is an integer indicating
63 how many occurrences must be found, when positive the search is
64 done forwards, otherwise backwards. When RESTORE-POINT is
65 non-nil the point is not moved but the position found is still
66 returned. When searching forward and point is already looking at
67 STRING, it is skipped so the next STRING occurrence is selected."
68 (let* ((num (or num 1))
69 (starting-point (point))
70 (string (regexp-quote string))
71 (search-fn (if (> num 0) #'re-search-forward #'re-search-backward))
72 (deinc-fn (if (> num 0) #'1- #'1+))
73 (found-point))
74 (prog2
75 (catch 'exit
76 (while (not (= num 0))
77 (when (and (> num 0)
78 (looking-at string))
79 ;; Moving forward and already looking at STRING, skip it.
80 (forward-char (length (match-string-no-properties 0))))
81 (and (not (funcall search-fn string nil t))
82 (throw 'exit t))
83 (when (> num 0)
84 ;; `re-search-forward' leaves point at the end of the
85 ;; occurrence, move back so point is at the beginning
86 ;; instead.
87 (forward-char (- (length (match-string-no-properties 0)))))
88 (setq
89 num (funcall deinc-fn num)
90 found-point (point))))
91 found-point
92 (and restore-point (goto-char starting-point)))))
94 (defun python-tests-self-insert (char-or-str)
95 "Call `self-insert-command' for chars in CHAR-OR-STR."
96 (let ((chars
97 (cond
98 ((characterp char-or-str)
99 (list char-or-str))
100 ((stringp char-or-str)
101 (string-to-list char-or-str))
102 ((not
103 (cl-remove-if #'characterp char-or-str))
104 char-or-str)
105 (t (error "CHAR-OR-STR must be a char, string, or list of char")))))
106 (mapc
107 (lambda (char)
108 (let ((last-command-event char))
109 (call-interactively 'self-insert-command)))
110 chars)))
112 (defun python-tests-visible-string (&optional min max)
113 "Return the buffer string excluding invisible overlays.
114 Argument MIN and MAX delimit the region to be returned and
115 default to `point-min' and `point-max' respectively."
116 (let* ((min (or min (point-min)))
117 (max (or max (point-max)))
118 (buffer (current-buffer))
119 (buffer-contents (buffer-substring-no-properties min max))
120 (overlays
121 (sort (overlays-in min max)
122 (lambda (a b)
123 (let ((overlay-end-a (overlay-end a))
124 (overlay-end-b (overlay-end b)))
125 (> overlay-end-a overlay-end-b))))))
126 (with-temp-buffer
127 (insert buffer-contents)
128 (dolist (overlay overlays)
129 (if (overlay-get overlay 'invisible)
130 (delete-region (overlay-start overlay)
131 (overlay-end overlay))))
132 (buffer-substring-no-properties (point-min) (point-max)))))
135 ;;; Tests for your tests, so you can test while you test.
137 (ert-deftest python-tests-look-at-1 ()
138 "Test forward movement."
139 (python-tests-with-temp-buffer
140 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
141 sed do eiusmod tempor incididunt ut labore et dolore magna
142 aliqua."
143 (let ((expected (save-excursion
144 (dotimes (i 3)
145 (re-search-forward "et" nil t))
146 (forward-char -2)
147 (point))))
148 (should (= (python-tests-look-at "et" 3 t) expected))
149 ;; Even if NUM is bigger than found occurrences the point of last
150 ;; one should be returned.
151 (should (= (python-tests-look-at "et" 6 t) expected))
152 ;; If already looking at STRING, it should skip it.
153 (dotimes (i 2) (re-search-forward "et"))
154 (forward-char -2)
155 (should (= (python-tests-look-at "et") expected)))))
157 (ert-deftest python-tests-look-at-2 ()
158 "Test backward movement."
159 (python-tests-with-temp-buffer
160 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
161 sed do eiusmod tempor incididunt ut labore et dolore magna
162 aliqua."
163 (let ((expected
164 (save-excursion
165 (re-search-forward "et" nil t)
166 (forward-char -2)
167 (point))))
168 (dotimes (i 3)
169 (re-search-forward "et" nil t))
170 (should (= (python-tests-look-at "et" -3 t) expected))
171 (should (= (python-tests-look-at "et" -6 t) expected)))))
174 ;;; Bindings
177 ;;; Python specialized rx
180 ;;; Font-lock and syntax
182 (ert-deftest python-syntax-after-python-backspace ()
183 ;; `python-indent-dedent-line-backspace' garbles syntax
184 :expected-result :failed
185 (python-tests-with-temp-buffer
186 "\"\"\""
187 (goto-char (point-max))
188 (python-indent-dedent-line-backspace 1)
189 (should (string= (buffer-string) "\"\""))
190 (should (null (nth 3 (syntax-ppss))))))
193 ;;; Indentation
195 ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
197 (ert-deftest python-indent-pep8-1 ()
198 "First pep8 case."
199 (python-tests-with-temp-buffer
200 "# Aligned with opening delimiter
201 foo = long_function_name(var_one, var_two,
202 var_three, var_four)
204 (should (eq (car (python-indent-context)) :no-indent))
205 (should (= (python-indent-calculate-indentation) 0))
206 (python-tests-look-at "foo = long_function_name(var_one, var_two,")
207 (should (eq (car (python-indent-context)) :after-comment))
208 (should (= (python-indent-calculate-indentation) 0))
209 (python-tests-look-at "var_three, var_four)")
210 (should (eq (car (python-indent-context)) :inside-paren))
211 (should (= (python-indent-calculate-indentation) 25))))
213 (ert-deftest python-indent-pep8-2 ()
214 "Second pep8 case."
215 (python-tests-with-temp-buffer
216 "# More indentation included to distinguish this from the rest.
217 def long_function_name(
218 var_one, var_two, var_three,
219 var_four):
220 print (var_one)
222 (should (eq (car (python-indent-context)) :no-indent))
223 (should (= (python-indent-calculate-indentation) 0))
224 (python-tests-look-at "def long_function_name(")
225 (should (eq (car (python-indent-context)) :after-comment))
226 (should (= (python-indent-calculate-indentation) 0))
227 (python-tests-look-at "var_one, var_two, var_three,")
228 (should (eq (car (python-indent-context))
229 :inside-paren-newline-start-from-block))
230 (should (= (python-indent-calculate-indentation) 8))
231 (python-tests-look-at "var_four):")
232 (should (eq (car (python-indent-context))
233 :inside-paren-newline-start-from-block))
234 (should (= (python-indent-calculate-indentation) 8))
235 (python-tests-look-at "print (var_one)")
236 (should (eq (car (python-indent-context))
237 :after-block-start))
238 (should (= (python-indent-calculate-indentation) 4))))
240 (ert-deftest python-indent-pep8-3 ()
241 "Third pep8 case."
242 (python-tests-with-temp-buffer
243 "# Extra indentation is not necessary.
244 foo = long_function_name(
245 var_one, var_two,
246 var_three, var_four)
248 (should (eq (car (python-indent-context)) :no-indent))
249 (should (= (python-indent-calculate-indentation) 0))
250 (python-tests-look-at "foo = long_function_name(")
251 (should (eq (car (python-indent-context)) :after-comment))
252 (should (= (python-indent-calculate-indentation) 0))
253 (python-tests-look-at "var_one, var_two,")
254 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
255 (should (= (python-indent-calculate-indentation) 4))
256 (python-tests-look-at "var_three, var_four)")
257 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
258 (should (= (python-indent-calculate-indentation) 4))))
260 (ert-deftest python-indent-base-case ()
261 "Check base case does not trigger errors."
262 (python-tests-with-temp-buffer
266 (goto-char (point-min))
267 (should (eq (car (python-indent-context)) :no-indent))
268 (should (= (python-indent-calculate-indentation) 0))
269 (forward-line 1)
270 (should (eq (car (python-indent-context)) :after-line))
271 (should (= (python-indent-calculate-indentation) 0))
272 (forward-line 1)
273 (should (eq (car (python-indent-context)) :after-line))
274 (should (= (python-indent-calculate-indentation) 0))))
276 (ert-deftest python-indent-after-comment-1 ()
277 "The most simple after-comment case that shouldn't fail."
278 (python-tests-with-temp-buffer
279 "# Contents will be modified to correct indentation
280 class Blag(object):
281 def _on_child_complete(self, child_future):
282 if self.in_terminal_state():
283 pass
284 # We only complete when all our async children have entered a
285 # terminal state. At that point, if any child failed, we fail
286 # with the exception with which the first child failed.
288 (python-tests-look-at "# We only complete")
289 (should (eq (car (python-indent-context)) :after-block-end))
290 (should (= (python-indent-calculate-indentation) 8))
291 (python-tests-look-at "# terminal state")
292 (should (eq (car (python-indent-context)) :after-comment))
293 (should (= (python-indent-calculate-indentation) 8))
294 (python-tests-look-at "# with the exception")
295 (should (eq (car (python-indent-context)) :after-comment))
296 ;; This one indents relative to previous block, even given the fact
297 ;; that it was under-indented.
298 (should (= (python-indent-calculate-indentation) 4))
299 (python-tests-look-at "# terminal state" -1)
300 ;; It doesn't hurt to check again.
301 (should (eq (car (python-indent-context)) :after-comment))
302 (python-indent-line)
303 (should (= (current-indentation) 8))
304 (python-tests-look-at "# with the exception")
305 (should (eq (car (python-indent-context)) :after-comment))
306 ;; Now everything should be lined up.
307 (should (= (python-indent-calculate-indentation) 8))))
309 (ert-deftest python-indent-after-comment-2 ()
310 "Test after-comment in weird cases."
311 (python-tests-with-temp-buffer
312 "# Contents will be modified to correct indentation
313 def func(arg):
314 # I don't do much
315 return arg
316 # This comment is badly indented because the user forced so.
317 # At this line python.el wont dedent, user is always right.
319 comment_wins_over_ender = True
321 # yeah, that.
323 (python-tests-look-at "# I don't do much")
324 (should (eq (car (python-indent-context)) :after-block-start))
325 (should (= (python-indent-calculate-indentation) 4))
326 (python-tests-look-at "return arg")
327 ;; Comment here just gets ignored, this line is not a comment so
328 ;; the rules won't apply here.
329 (should (eq (car (python-indent-context)) :after-block-start))
330 (should (= (python-indent-calculate-indentation) 4))
331 (python-tests-look-at "# This comment is badly indented")
332 (should (eq (car (python-indent-context)) :after-block-end))
333 ;; The return keyword do make indentation lose a level...
334 (should (= (python-indent-calculate-indentation) 0))
335 ;; ...but the current indentation was forced by the user.
336 (python-tests-look-at "# At this line python.el wont dedent")
337 (should (eq (car (python-indent-context)) :after-comment))
338 (should (= (python-indent-calculate-indentation) 4))
339 ;; Should behave the same for blank lines: potentially a comment.
340 (forward-line 1)
341 (should (eq (car (python-indent-context)) :after-comment))
342 (should (= (python-indent-calculate-indentation) 4))
343 (python-tests-look-at "comment_wins_over_ender")
344 ;; The comment won over the ender because the user said so.
345 (should (eq (car (python-indent-context)) :after-comment))
346 (should (= (python-indent-calculate-indentation) 4))
347 ;; The indentation calculated fine for the assignment, but the user
348 ;; choose to force it back to the first column. Next line should
349 ;; be aware of that.
350 (python-tests-look-at "# yeah, that.")
351 (should (eq (car (python-indent-context)) :after-line))
352 (should (= (python-indent-calculate-indentation) 0))))
354 (ert-deftest python-indent-after-comment-3 ()
355 "Test after-comment in buggy case."
356 (python-tests-with-temp-buffer
358 class A(object):
360 def something(self, arg):
361 if True:
362 return arg
364 # A comment
366 @adecorator
367 def method(self, a, b):
368 pass
370 (python-tests-look-at "@adecorator")
371 (should (eq (car (python-indent-context)) :after-comment))
372 (should (= (python-indent-calculate-indentation) 4))))
374 (ert-deftest python-indent-inside-paren-1 ()
375 "The most simple inside-paren case that shouldn't fail."
376 (python-tests-with-temp-buffer
378 data = {
379 'key':
381 'objlist': [
383 'pk': 1,
384 'name': 'first',
387 'pk': 2,
388 'name': 'second',
394 (python-tests-look-at "data = {")
395 (should (eq (car (python-indent-context)) :after-line))
396 (should (= (python-indent-calculate-indentation) 0))
397 (python-tests-look-at "'key':")
398 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
399 (should (= (python-indent-calculate-indentation) 4))
400 (python-tests-look-at "{")
401 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
402 (should (= (python-indent-calculate-indentation) 4))
403 (python-tests-look-at "'objlist': [")
404 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
405 (should (= (python-indent-calculate-indentation) 8))
406 (python-tests-look-at "{")
407 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
408 (should (= (python-indent-calculate-indentation) 12))
409 (python-tests-look-at "'pk': 1,")
410 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
411 (should (= (python-indent-calculate-indentation) 16))
412 (python-tests-look-at "'name': 'first',")
413 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
414 (should (= (python-indent-calculate-indentation) 16))
415 (python-tests-look-at "},")
416 (should (eq (car (python-indent-context))
417 :inside-paren-at-closing-nested-paren))
418 (should (= (python-indent-calculate-indentation) 12))
419 (python-tests-look-at "{")
420 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
421 (should (= (python-indent-calculate-indentation) 12))
422 (python-tests-look-at "'pk': 2,")
423 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
424 (should (= (python-indent-calculate-indentation) 16))
425 (python-tests-look-at "'name': 'second',")
426 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
427 (should (= (python-indent-calculate-indentation) 16))
428 (python-tests-look-at "}")
429 (should (eq (car (python-indent-context))
430 :inside-paren-at-closing-nested-paren))
431 (should (= (python-indent-calculate-indentation) 12))
432 (python-tests-look-at "]")
433 (should (eq (car (python-indent-context))
434 :inside-paren-at-closing-nested-paren))
435 (should (= (python-indent-calculate-indentation) 8))
436 (python-tests-look-at "}")
437 (should (eq (car (python-indent-context))
438 :inside-paren-at-closing-nested-paren))
439 (should (= (python-indent-calculate-indentation) 4))
440 (python-tests-look-at "}")
441 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
442 (should (= (python-indent-calculate-indentation) 0))))
444 (ert-deftest python-indent-inside-paren-2 ()
445 "Another more compact paren group style."
446 (python-tests-with-temp-buffer
448 data = {'key': {
449 'objlist': [
450 {'pk': 1,
451 'name': 'first'},
452 {'pk': 2,
453 'name': 'second'}
457 (python-tests-look-at "data = {")
458 (should (eq (car (python-indent-context)) :after-line))
459 (should (= (python-indent-calculate-indentation) 0))
460 (python-tests-look-at "'objlist': [")
461 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
462 (should (= (python-indent-calculate-indentation) 4))
463 (python-tests-look-at "{'pk': 1,")
464 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
465 (should (= (python-indent-calculate-indentation) 8))
466 (python-tests-look-at "'name': 'first'},")
467 (should (eq (car (python-indent-context)) :inside-paren))
468 (should (= (python-indent-calculate-indentation) 9))
469 (python-tests-look-at "{'pk': 2,")
470 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
471 (should (= (python-indent-calculate-indentation) 8))
472 (python-tests-look-at "'name': 'second'}")
473 (should (eq (car (python-indent-context)) :inside-paren))
474 (should (= (python-indent-calculate-indentation) 9))
475 (python-tests-look-at "]")
476 (should (eq (car (python-indent-context))
477 :inside-paren-at-closing-nested-paren))
478 (should (= (python-indent-calculate-indentation) 4))
479 (python-tests-look-at "}}")
480 (should (eq (car (python-indent-context))
481 :inside-paren-at-closing-nested-paren))
482 (should (= (python-indent-calculate-indentation) 0))
483 (python-tests-look-at "}")
484 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
485 (should (= (python-indent-calculate-indentation) 0))))
487 (ert-deftest python-indent-inside-paren-3 ()
488 "The simplest case possible."
489 (python-tests-with-temp-buffer
491 data = ('these',
492 'are',
493 'the',
494 'tokens')
496 (python-tests-look-at "data = ('these',")
497 (should (eq (car (python-indent-context)) :after-line))
498 (should (= (python-indent-calculate-indentation) 0))
499 (forward-line 1)
500 (should (eq (car (python-indent-context)) :inside-paren))
501 (should (= (python-indent-calculate-indentation) 8))
502 (forward-line 1)
503 (should (eq (car (python-indent-context)) :inside-paren))
504 (should (= (python-indent-calculate-indentation) 8))
505 (forward-line 1)
506 (should (eq (car (python-indent-context)) :inside-paren))
507 (should (= (python-indent-calculate-indentation) 8))))
509 (ert-deftest python-indent-inside-paren-4 ()
510 "Respect indentation of first column."
511 (python-tests-with-temp-buffer
513 data = [ [ 'these', 'are'],
514 ['the', 'tokens' ] ]
516 (python-tests-look-at "data = [ [ 'these', 'are'],")
517 (should (eq (car (python-indent-context)) :after-line))
518 (should (= (python-indent-calculate-indentation) 0))
519 (forward-line 1)
520 (should (eq (car (python-indent-context)) :inside-paren))
521 (should (= (python-indent-calculate-indentation) 9))))
523 (ert-deftest python-indent-inside-paren-5 ()
524 "Test when :inside-paren initial parens are skipped in context start."
525 (python-tests-with-temp-buffer
527 while ((not some_condition) and
528 another_condition):
529 do_something_interesting(
530 with_some_arg)
532 (python-tests-look-at "while ((not some_condition) and")
533 (should (eq (car (python-indent-context)) :after-line))
534 (should (= (python-indent-calculate-indentation) 0))
535 (forward-line 1)
536 (should (eq (car (python-indent-context)) :inside-paren))
537 (should (= (python-indent-calculate-indentation) 7))
538 (forward-line 1)
539 (should (eq (car (python-indent-context)) :after-block-start))
540 (should (= (python-indent-calculate-indentation) 4))
541 (forward-line 1)
542 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
543 (should (= (python-indent-calculate-indentation) 8))))
545 (ert-deftest python-indent-inside-paren-6 ()
546 "This should be aligned.."
547 (python-tests-with-temp-buffer
549 CHOICES = (('some', 'choice'),
550 ('another', 'choice'),
551 ('more', 'choices'))
553 (python-tests-look-at "CHOICES = (('some', 'choice'),")
554 (should (eq (car (python-indent-context)) :after-line))
555 (should (= (python-indent-calculate-indentation) 0))
556 (forward-line 1)
557 (should (eq (car (python-indent-context)) :inside-paren))
558 (should (= (python-indent-calculate-indentation) 11))
559 (forward-line 1)
560 (should (eq (car (python-indent-context)) :inside-paren))
561 (should (= (python-indent-calculate-indentation) 11))))
563 (ert-deftest python-indent-after-block-1 ()
564 "The most simple after-block case that shouldn't fail."
565 (python-tests-with-temp-buffer
567 def foo(a, b, c=True):
569 (should (eq (car (python-indent-context)) :no-indent))
570 (should (= (python-indent-calculate-indentation) 0))
571 (goto-char (point-max))
572 (should (eq (car (python-indent-context)) :after-block-start))
573 (should (= (python-indent-calculate-indentation) 4))))
575 (ert-deftest python-indent-after-block-2 ()
576 "A weird (malformed) multiline block statement."
577 (python-tests-with-temp-buffer
579 def foo(a, b, c={
580 'a':
583 (goto-char (point-max))
584 (should (eq (car (python-indent-context)) :after-block-start))
585 (should (= (python-indent-calculate-indentation) 4))))
587 (ert-deftest python-indent-after-block-3 ()
588 "A weird (malformed) sample, usually found in python shells."
589 (python-tests-with-temp-buffer
591 In [1]:
592 def func():
593 pass
595 In [2]:
596 something
598 (python-tests-look-at "pass")
599 (should (eq (car (python-indent-context)) :after-block-start))
600 (should (= (python-indent-calculate-indentation) 4))
601 (python-tests-look-at "something")
602 (end-of-line)
603 (should (eq (car (python-indent-context)) :after-line))
604 (should (= (python-indent-calculate-indentation) 0))))
606 (ert-deftest python-indent-after-backslash-1 ()
607 "The most common case."
608 (python-tests-with-temp-buffer
610 from foo.bar.baz import something, something_1 \\\\
611 something_2 something_3, \\\\
612 something_4, something_5
614 (python-tests-look-at "from foo.bar.baz import something, something_1")
615 (should (eq (car (python-indent-context)) :after-line))
616 (should (= (python-indent-calculate-indentation) 0))
617 (python-tests-look-at "something_2 something_3,")
618 (should (eq (car (python-indent-context)) :after-backslash-first-line))
619 (should (= (python-indent-calculate-indentation) 4))
620 (python-tests-look-at "something_4, something_5")
621 (should (eq (car (python-indent-context)) :after-backslash))
622 (should (= (python-indent-calculate-indentation) 4))
623 (goto-char (point-max))
624 (should (eq (car (python-indent-context)) :after-line))
625 (should (= (python-indent-calculate-indentation) 0))))
627 (ert-deftest python-indent-after-backslash-2 ()
628 "A pretty extreme complicated case."
629 (python-tests-with-temp-buffer
631 objects = Thing.objects.all() \\\\
632 .filter(
633 type='toy',
634 status='bought'
635 ) \\\\
636 .aggregate(
637 Sum('amount')
638 ) \\\\
639 .values_list()
641 (python-tests-look-at "objects = Thing.objects.all()")
642 (should (eq (car (python-indent-context)) :after-line))
643 (should (= (python-indent-calculate-indentation) 0))
644 (python-tests-look-at ".filter(")
645 (should (eq (car (python-indent-context))
646 :after-backslash-dotted-continuation))
647 (should (= (python-indent-calculate-indentation) 23))
648 (python-tests-look-at "type='toy',")
649 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
650 (should (= (python-indent-calculate-indentation) 27))
651 (python-tests-look-at "status='bought'")
652 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
653 (should (= (python-indent-calculate-indentation) 27))
654 (python-tests-look-at ") \\\\")
655 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
656 (should (= (python-indent-calculate-indentation) 23))
657 (python-tests-look-at ".aggregate(")
658 (should (eq (car (python-indent-context))
659 :after-backslash-dotted-continuation))
660 (should (= (python-indent-calculate-indentation) 23))
661 (python-tests-look-at "Sum('amount')")
662 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
663 (should (= (python-indent-calculate-indentation) 27))
664 (python-tests-look-at ") \\\\")
665 (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
666 (should (= (python-indent-calculate-indentation) 23))
667 (python-tests-look-at ".values_list()")
668 (should (eq (car (python-indent-context))
669 :after-backslash-dotted-continuation))
670 (should (= (python-indent-calculate-indentation) 23))
671 (forward-line 1)
672 (should (eq (car (python-indent-context)) :after-line))
673 (should (= (python-indent-calculate-indentation) 0))))
675 (ert-deftest python-indent-after-backslash-3 ()
676 "Backslash continuation from block start."
677 (python-tests-with-temp-buffer
679 with open('/path/to/some/file/you/want/to/read') as file_1, \\\\
680 open('/path/to/some/file/being/written', 'w') as file_2:
681 file_2.write(file_1.read())
683 (python-tests-look-at
684 "with open('/path/to/some/file/you/want/to/read') as file_1, \\\\")
685 (should (eq (car (python-indent-context)) :after-line))
686 (should (= (python-indent-calculate-indentation) 0))
687 (python-tests-look-at
688 "open('/path/to/some/file/being/written', 'w') as file_2")
689 (should (eq (car (python-indent-context))
690 :after-backslash-block-continuation))
691 (should (= (python-indent-calculate-indentation) 5))
692 (python-tests-look-at "file_2.write(file_1.read())")
693 (should (eq (car (python-indent-context)) :after-block-start))
694 (should (= (python-indent-calculate-indentation) 4))))
696 (ert-deftest python-indent-after-backslash-4 ()
697 "Backslash continuation from assignment."
698 (python-tests-with-temp-buffer
700 super_awful_assignment = some_calculation() and \\\\
701 another_calculation() and \\\\
702 some_final_calculation()
704 (python-tests-look-at
705 "super_awful_assignment = some_calculation() and \\\\")
706 (should (eq (car (python-indent-context)) :after-line))
707 (should (= (python-indent-calculate-indentation) 0))
708 (python-tests-look-at "another_calculation() and \\\\")
709 (should (eq (car (python-indent-context))
710 :after-backslash-assignment-continuation))
711 (should (= (python-indent-calculate-indentation) 25))
712 (python-tests-look-at "some_final_calculation()")
713 (should (eq (car (python-indent-context)) :after-backslash))
714 (should (= (python-indent-calculate-indentation) 25))))
716 (ert-deftest python-indent-after-backslash-5 ()
717 "Dotted continuation bizarre example."
718 (python-tests-with-temp-buffer
720 def delete_all_things():
721 Thing \\\\
722 .objects.all() \\\\
723 .delete()
725 (python-tests-look-at "Thing \\\\")
726 (should (eq (car (python-indent-context)) :after-block-start))
727 (should (= (python-indent-calculate-indentation) 4))
728 (python-tests-look-at ".objects.all() \\\\")
729 (should (eq (car (python-indent-context)) :after-backslash-first-line))
730 (should (= (python-indent-calculate-indentation) 8))
731 (python-tests-look-at ".delete()")
732 (should (eq (car (python-indent-context))
733 :after-backslash-dotted-continuation))
734 (should (= (python-indent-calculate-indentation) 16))))
736 (ert-deftest python-indent-block-enders-1 ()
737 "Test de-indentation for pass keyword."
738 (python-tests-with-temp-buffer
740 Class foo(object):
742 def bar(self):
743 if self.baz:
744 return (1,
748 else:
749 pass
751 (python-tests-look-at "3)")
752 (forward-line 1)
753 (should (= (python-indent-calculate-indentation) 8))
754 (python-tests-look-at "pass")
755 (forward-line 1)
756 (should (eq (car (python-indent-context)) :after-block-end))
757 (should (= (python-indent-calculate-indentation) 8))))
759 (ert-deftest python-indent-block-enders-2 ()
760 "Test de-indentation for return keyword."
761 (python-tests-with-temp-buffer
763 Class foo(object):
764 '''raise lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
766 eiusmod tempor incididunt ut labore et dolore magna aliqua.
768 def bar(self):
769 \"return (1, 2, 3).\"
770 if self.baz:
771 return (1,
775 (python-tests-look-at "def")
776 (should (= (python-indent-calculate-indentation) 4))
777 (python-tests-look-at "if")
778 (should (= (python-indent-calculate-indentation) 8))
779 (python-tests-look-at "return")
780 (should (= (python-indent-calculate-indentation) 12))
781 (goto-char (point-max))
782 (should (eq (car (python-indent-context)) :after-block-end))
783 (should (= (python-indent-calculate-indentation) 8))))
785 (ert-deftest python-indent-block-enders-3 ()
786 "Test de-indentation for continue keyword."
787 (python-tests-with-temp-buffer
789 for element in lst:
790 if element is None:
791 continue
793 (python-tests-look-at "if")
794 (should (= (python-indent-calculate-indentation) 4))
795 (python-tests-look-at "continue")
796 (should (= (python-indent-calculate-indentation) 8))
797 (forward-line 1)
798 (should (eq (car (python-indent-context)) :after-block-end))
799 (should (= (python-indent-calculate-indentation) 4))))
801 (ert-deftest python-indent-block-enders-4 ()
802 "Test de-indentation for break keyword."
803 (python-tests-with-temp-buffer
805 for element in lst:
806 if element is None:
807 break
809 (python-tests-look-at "if")
810 (should (= (python-indent-calculate-indentation) 4))
811 (python-tests-look-at "break")
812 (should (= (python-indent-calculate-indentation) 8))
813 (forward-line 1)
814 (should (eq (car (python-indent-context)) :after-block-end))
815 (should (= (python-indent-calculate-indentation) 4))))
817 (ert-deftest python-indent-block-enders-5 ()
818 "Test de-indentation for raise keyword."
819 (python-tests-with-temp-buffer
821 for element in lst:
822 if element is None:
823 raise ValueError('Element cannot be None')
825 (python-tests-look-at "if")
826 (should (= (python-indent-calculate-indentation) 4))
827 (python-tests-look-at "raise")
828 (should (= (python-indent-calculate-indentation) 8))
829 (forward-line 1)
830 (should (eq (car (python-indent-context)) :after-block-end))
831 (should (= (python-indent-calculate-indentation) 4))))
833 (ert-deftest python-indent-dedenters-1 ()
834 "Test de-indentation for the elif keyword."
835 (python-tests-with-temp-buffer
837 if save:
838 try:
839 write_to_disk(data)
840 finally:
841 cleanup()
842 elif
844 (python-tests-look-at "elif\n")
845 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
846 (should (= (python-indent-calculate-indentation) 0))
847 (should (= (python-indent-calculate-indentation t) 0))))
849 (ert-deftest python-indent-dedenters-2 ()
850 "Test de-indentation for the else keyword."
851 (python-tests-with-temp-buffer
853 if save:
854 try:
855 write_to_disk(data)
856 except IOError:
857 msg = 'Error saving to disk'
858 message(msg)
859 logger.exception(msg)
860 except Exception:
861 if hide_details:
862 logger.exception('Unhandled exception')
863 else
864 finally:
865 data.free()
867 (python-tests-look-at "else\n")
868 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
869 (should (= (python-indent-calculate-indentation) 8))
870 (python-indent-line t)
871 (should (= (python-indent-calculate-indentation t) 4))
872 (python-indent-line t)
873 (should (= (python-indent-calculate-indentation t) 0))
874 (python-indent-line t)
875 (should (= (python-indent-calculate-indentation t) 8))))
877 (ert-deftest python-indent-dedenters-3 ()
878 "Test de-indentation for the except keyword."
879 (python-tests-with-temp-buffer
881 if save:
882 try:
883 write_to_disk(data)
884 except
886 (python-tests-look-at "except\n")
887 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
888 (should (= (python-indent-calculate-indentation) 4))
889 (python-indent-line t)
890 (should (= (python-indent-calculate-indentation t) 4))))
892 (ert-deftest python-indent-dedenters-4 ()
893 "Test de-indentation for the finally keyword."
894 (python-tests-with-temp-buffer
896 if save:
897 try:
898 write_to_disk(data)
899 finally
901 (python-tests-look-at "finally\n")
902 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
903 (should (= (python-indent-calculate-indentation) 4))
904 (python-indent-line t)
905 (should (= (python-indent-calculate-indentation) 4))))
907 (ert-deftest python-indent-dedenters-5 ()
908 "Test invalid levels are skipped in a complex example."
909 (python-tests-with-temp-buffer
911 if save:
912 try:
913 write_to_disk(data)
914 except IOError:
915 msg = 'Error saving to disk'
916 message(msg)
917 logger.exception(msg)
918 finally:
919 if cleanup:
920 do_cleanup()
921 else
923 (python-tests-look-at "else\n")
924 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
925 (should (= (python-indent-calculate-indentation) 8))
926 (should (= (python-indent-calculate-indentation t) 0))
927 (python-indent-line t)
928 (should (= (python-indent-calculate-indentation t) 8))))
930 (ert-deftest python-indent-dedenters-6 ()
931 "Test indentation is zero when no opening block for dedenter."
932 (python-tests-with-temp-buffer
934 try:
935 # if save:
936 write_to_disk(data)
937 else
939 (python-tests-look-at "else\n")
940 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
941 (should (= (python-indent-calculate-indentation) 0))
942 (should (= (python-indent-calculate-indentation t) 0))))
944 (ert-deftest python-indent-dedenters-7 ()
945 "Test indentation case from Bug#15163."
946 (python-tests-with-temp-buffer
948 if a:
949 if b:
950 pass
951 else:
952 pass
953 else:
955 (python-tests-look-at "else:" 2)
956 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
957 (should (= (python-indent-calculate-indentation) 0))
958 (should (= (python-indent-calculate-indentation t) 0))))
960 (ert-deftest python-indent-dedenters-8 ()
961 "Test indentation for Bug#18432."
962 (python-tests-with-temp-buffer
964 if (a == 1 or
965 a == 2):
966 pass
967 elif (a == 3 or
968 a == 4):
970 (python-tests-look-at "elif (a == 3 or")
971 (should (eq (car (python-indent-context)) :at-dedenter-block-start))
972 (should (= (python-indent-calculate-indentation) 0))
973 (should (= (python-indent-calculate-indentation t) 0))
974 (python-tests-look-at "a == 4):\n")
975 (should (eq (car (python-indent-context)) :inside-paren))
976 (should (= (python-indent-calculate-indentation) 6))
977 (python-indent-line)
978 (should (= (python-indent-calculate-indentation t) 4))
979 (python-indent-line t)
980 (should (= (python-indent-calculate-indentation t) 0))
981 (python-indent-line t)
982 (should (= (python-indent-calculate-indentation t) 6))))
984 (ert-deftest python-indent-inside-string-1 ()
985 "Test indentation for strings."
986 (python-tests-with-temp-buffer
988 multiline = '''
989 bunch
991 lines
994 (python-tests-look-at "multiline = '''")
995 (should (eq (car (python-indent-context)) :after-line))
996 (should (= (python-indent-calculate-indentation) 0))
997 (python-tests-look-at "bunch")
998 (should (eq (car (python-indent-context)) :inside-string))
999 (should (= (python-indent-calculate-indentation) 0))
1000 (python-tests-look-at "of")
1001 (should (eq (car (python-indent-context)) :inside-string))
1002 (should (= (python-indent-calculate-indentation) 0))
1003 (python-tests-look-at "lines")
1004 (should (eq (car (python-indent-context)) :inside-string))
1005 (should (= (python-indent-calculate-indentation) 0))
1006 (python-tests-look-at "'''")
1007 (should (eq (car (python-indent-context)) :inside-string))
1008 (should (= (python-indent-calculate-indentation) 0))))
1010 (ert-deftest python-indent-inside-string-2 ()
1011 "Test indentation for docstrings."
1012 (python-tests-with-temp-buffer
1014 def fn(a, b, c=True):
1015 '''docstring
1016 bunch
1018 lines
1021 (python-tests-look-at "'''docstring")
1022 (should (eq (car (python-indent-context)) :after-block-start))
1023 (should (= (python-indent-calculate-indentation) 4))
1024 (python-tests-look-at "bunch")
1025 (should (eq (car (python-indent-context)) :inside-docstring))
1026 (should (= (python-indent-calculate-indentation) 4))
1027 (python-tests-look-at "of")
1028 (should (eq (car (python-indent-context)) :inside-docstring))
1029 ;; Any indentation deeper than the base-indent must remain unmodified.
1030 (should (= (python-indent-calculate-indentation) 8))
1031 (python-tests-look-at "lines")
1032 (should (eq (car (python-indent-context)) :inside-docstring))
1033 (should (= (python-indent-calculate-indentation) 4))
1034 (python-tests-look-at "'''")
1035 (should (eq (car (python-indent-context)) :inside-docstring))
1036 (should (= (python-indent-calculate-indentation) 4))))
1038 (ert-deftest python-indent-inside-string-3 ()
1039 "Test indentation for nested strings."
1040 (python-tests-with-temp-buffer
1042 def fn(a, b, c=True):
1043 some_var = '''
1044 bunch
1046 lines
1049 (python-tests-look-at "some_var = '''")
1050 (should (eq (car (python-indent-context)) :after-block-start))
1051 (should (= (python-indent-calculate-indentation) 4))
1052 (python-tests-look-at "bunch")
1053 (should (eq (car (python-indent-context)) :inside-string))
1054 (should (= (python-indent-calculate-indentation) 4))
1055 (python-tests-look-at "of")
1056 (should (eq (car (python-indent-context)) :inside-string))
1057 (should (= (python-indent-calculate-indentation) 4))
1058 (python-tests-look-at "lines")
1059 (should (eq (car (python-indent-context)) :inside-string))
1060 (should (= (python-indent-calculate-indentation) 4))
1061 (python-tests-look-at "'''")
1062 (should (eq (car (python-indent-context)) :inside-string))
1063 (should (= (python-indent-calculate-indentation) 4))))
1065 (ert-deftest python-indent-electric-colon-1 ()
1066 "Test indentation case from Bug#18228."
1067 (python-tests-with-temp-buffer
1069 def a():
1070 pass
1072 def b()
1074 (python-tests-look-at "def b()")
1075 (goto-char (line-end-position))
1076 (python-tests-self-insert ":")
1077 (should (= (current-indentation) 0))))
1079 (ert-deftest python-indent-electric-colon-2 ()
1080 "Test indentation case for dedenter."
1081 (python-tests-with-temp-buffer
1083 if do:
1084 something()
1085 else
1087 (python-tests-look-at "else")
1088 (goto-char (line-end-position))
1089 (python-tests-self-insert ":")
1090 (should (= (current-indentation) 0))))
1092 (ert-deftest python-indent-electric-colon-3 ()
1093 "Test indentation case for multi-line dedenter."
1094 (python-tests-with-temp-buffer
1096 if do:
1097 something()
1098 elif (this
1100 that)
1102 (python-tests-look-at "that)")
1103 (goto-char (line-end-position))
1104 (python-tests-self-insert ":")
1105 (python-tests-look-at "elif" -1)
1106 (should (= (current-indentation) 0))
1107 (python-tests-look-at "and")
1108 (should (= (current-indentation) 6))
1109 (python-tests-look-at "that)")
1110 (should (= (current-indentation) 6))))
1112 (ert-deftest python-indent-region-1 ()
1113 "Test indentation case from Bug#18843."
1114 (let ((contents "
1115 def foo ():
1116 try:
1117 pass
1118 except:
1119 pass
1121 (python-tests-with-temp-buffer
1122 contents
1123 (python-indent-region (point-min) (point-max))
1124 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1125 contents)))))
1127 (ert-deftest python-indent-region-2 ()
1128 "Test region indentation on comments."
1129 (let ((contents "
1130 def f():
1131 if True:
1132 pass
1134 # This is
1135 # some multiline
1136 # comment
1138 (python-tests-with-temp-buffer
1139 contents
1140 (python-indent-region (point-min) (point-max))
1141 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1142 contents)))))
1144 (ert-deftest python-indent-region-3 ()
1145 "Test region indentation on comments."
1146 (let ((contents "
1147 def f():
1148 if True:
1149 pass
1150 # This is
1151 # some multiline
1152 # comment
1154 (expected "
1155 def f():
1156 if True:
1157 pass
1158 # This is
1159 # some multiline
1160 # comment
1162 (python-tests-with-temp-buffer
1163 contents
1164 (python-indent-region (point-min) (point-max))
1165 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1166 expected)))))
1168 (ert-deftest python-indent-region-4 ()
1169 "Test region indentation block starts, dedenters and enders."
1170 (let ((contents "
1171 def f():
1172 if True:
1173 a = 5
1174 else:
1175 a = 10
1176 return a
1178 (expected "
1179 def f():
1180 if True:
1181 a = 5
1182 else:
1183 a = 10
1184 return a
1186 (python-tests-with-temp-buffer
1187 contents
1188 (python-indent-region (point-min) (point-max))
1189 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1190 expected)))))
1192 (ert-deftest python-indent-region-5 ()
1193 "Test region indentation for docstrings."
1194 (let ((contents "
1195 def f():
1197 this is
1198 a multiline
1199 string
1201 x = \\
1203 this is an arbitrarily
1204 indented multiline
1205 string
1208 (expected "
1209 def f():
1211 this is
1212 a multiline
1213 string
1215 x = \\
1217 this is an arbitrarily
1218 indented multiline
1219 string
1222 (python-tests-with-temp-buffer
1223 contents
1224 (python-indent-region (point-min) (point-max))
1225 (should (string= (buffer-substring-no-properties (point-min) (point-max))
1226 expected)))))
1229 ;;; Navigation
1231 (ert-deftest python-nav-beginning-of-defun-1 ()
1232 (python-tests-with-temp-buffer
1234 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1235 '''print decorated function call data to stdout.
1237 Usage:
1239 @decoratorFunctionWithArguments('arg1', 'arg2')
1240 def func(a, b, c=True):
1241 pass
1244 def wwrap(f):
1245 print 'Inside wwrap()'
1246 def wrapped_f(*args):
1247 print 'Inside wrapped_f()'
1248 print 'Decorator arguments:', arg1, arg2, arg3
1249 f(*args)
1250 print 'After f(*args)'
1251 return wrapped_f
1252 return wwrap
1254 (python-tests-look-at "return wrap")
1255 (should (= (save-excursion
1256 (python-nav-beginning-of-defun)
1257 (point))
1258 (save-excursion
1259 (python-tests-look-at "def wrapped_f(*args):" -1)
1260 (beginning-of-line)
1261 (point))))
1262 (python-tests-look-at "def wrapped_f(*args):" -1)
1263 (should (= (save-excursion
1264 (python-nav-beginning-of-defun)
1265 (point))
1266 (save-excursion
1267 (python-tests-look-at "def wwrap(f):" -1)
1268 (beginning-of-line)
1269 (point))))
1270 (python-tests-look-at "def wwrap(f):" -1)
1271 (should (= (save-excursion
1272 (python-nav-beginning-of-defun)
1273 (point))
1274 (save-excursion
1275 (python-tests-look-at "def decoratorFunctionWithArguments" -1)
1276 (beginning-of-line)
1277 (point))))))
1279 (ert-deftest python-nav-beginning-of-defun-2 ()
1280 (python-tests-with-temp-buffer
1282 class C(object):
1284 def m(self):
1285 self.c()
1287 def b():
1288 pass
1290 def a():
1291 pass
1293 def c(self):
1294 pass
1296 ;; Nested defuns, are handled with care.
1297 (python-tests-look-at "def c(self):")
1298 (should (= (save-excursion
1299 (python-nav-beginning-of-defun)
1300 (point))
1301 (save-excursion
1302 (python-tests-look-at "def m(self):" -1)
1303 (beginning-of-line)
1304 (point))))
1305 ;; Defuns on same levels should be respected.
1306 (python-tests-look-at "def a():" -1)
1307 (should (= (save-excursion
1308 (python-nav-beginning-of-defun)
1309 (point))
1310 (save-excursion
1311 (python-tests-look-at "def b():" -1)
1312 (beginning-of-line)
1313 (point))))
1314 ;; Jump to a top level defun.
1315 (python-tests-look-at "def b():" -1)
1316 (should (= (save-excursion
1317 (python-nav-beginning-of-defun)
1318 (point))
1319 (save-excursion
1320 (python-tests-look-at "def m(self):" -1)
1321 (beginning-of-line)
1322 (point))))
1323 ;; Jump to a top level defun again.
1324 (python-tests-look-at "def m(self):" -1)
1325 (should (= (save-excursion
1326 (python-nav-beginning-of-defun)
1327 (point))
1328 (save-excursion
1329 (python-tests-look-at "class C(object):" -1)
1330 (beginning-of-line)
1331 (point))))))
1333 (ert-deftest python-nav-end-of-defun-1 ()
1334 (python-tests-with-temp-buffer
1336 class C(object):
1338 def m(self):
1339 self.c()
1341 def b():
1342 pass
1344 def a():
1345 pass
1347 def c(self):
1348 pass
1350 (should (= (save-excursion
1351 (python-tests-look-at "class C(object):")
1352 (python-nav-end-of-defun)
1353 (point))
1354 (save-excursion
1355 (point-max))))
1356 (should (= (save-excursion
1357 (python-tests-look-at "def m(self):")
1358 (python-nav-end-of-defun)
1359 (point))
1360 (save-excursion
1361 (python-tests-look-at "def c(self):")
1362 (forward-line -1)
1363 (point))))
1364 (should (= (save-excursion
1365 (python-tests-look-at "def b():")
1366 (python-nav-end-of-defun)
1367 (point))
1368 (save-excursion
1369 (python-tests-look-at "def b():")
1370 (forward-line 2)
1371 (point))))
1372 (should (= (save-excursion
1373 (python-tests-look-at "def c(self):")
1374 (python-nav-end-of-defun)
1375 (point))
1376 (save-excursion
1377 (point-max))))))
1379 (ert-deftest python-nav-end-of-defun-2 ()
1380 (python-tests-with-temp-buffer
1382 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1383 '''print decorated function call data to stdout.
1385 Usage:
1387 @decoratorFunctionWithArguments('arg1', 'arg2')
1388 def func(a, b, c=True):
1389 pass
1392 def wwrap(f):
1393 print 'Inside wwrap()'
1394 def wrapped_f(*args):
1395 print 'Inside wrapped_f()'
1396 print 'Decorator arguments:', arg1, arg2, arg3
1397 f(*args)
1398 print 'After f(*args)'
1399 return wrapped_f
1400 return wwrap
1402 (should (= (save-excursion
1403 (python-tests-look-at "def decoratorFunctionWithArguments")
1404 (python-nav-end-of-defun)
1405 (point))
1406 (save-excursion
1407 (point-max))))
1408 (should (= (save-excursion
1409 (python-tests-look-at "@decoratorFunctionWithArguments")
1410 (python-nav-end-of-defun)
1411 (point))
1412 (save-excursion
1413 (point-max))))
1414 (should (= (save-excursion
1415 (python-tests-look-at "def wwrap(f):")
1416 (python-nav-end-of-defun)
1417 (point))
1418 (save-excursion
1419 (python-tests-look-at "return wwrap")
1420 (line-beginning-position))))
1421 (should (= (save-excursion
1422 (python-tests-look-at "def wrapped_f(*args):")
1423 (python-nav-end-of-defun)
1424 (point))
1425 (save-excursion
1426 (python-tests-look-at "return wrapped_f")
1427 (line-beginning-position))))
1428 (should (= (save-excursion
1429 (python-tests-look-at "f(*args)")
1430 (python-nav-end-of-defun)
1431 (point))
1432 (save-excursion
1433 (python-tests-look-at "return wrapped_f")
1434 (line-beginning-position))))))
1436 (ert-deftest python-nav-backward-defun-1 ()
1437 (python-tests-with-temp-buffer
1439 class A(object): # A
1441 def a(self): # a
1442 pass
1444 def b(self): # b
1445 pass
1447 class B(object): # B
1449 class C(object): # C
1451 def d(self): # d
1452 pass
1454 # def e(self): # e
1455 # pass
1457 def c(self): # c
1458 pass
1460 # def d(self): # d
1461 # pass
1463 (goto-char (point-max))
1464 (should (= (save-excursion (python-nav-backward-defun))
1465 (python-tests-look-at " def c(self): # c" -1)))
1466 (should (= (save-excursion (python-nav-backward-defun))
1467 (python-tests-look-at " def d(self): # d" -1)))
1468 (should (= (save-excursion (python-nav-backward-defun))
1469 (python-tests-look-at " class C(object): # C" -1)))
1470 (should (= (save-excursion (python-nav-backward-defun))
1471 (python-tests-look-at " class B(object): # B" -1)))
1472 (should (= (save-excursion (python-nav-backward-defun))
1473 (python-tests-look-at " def b(self): # b" -1)))
1474 (should (= (save-excursion (python-nav-backward-defun))
1475 (python-tests-look-at " def a(self): # a" -1)))
1476 (should (= (save-excursion (python-nav-backward-defun))
1477 (python-tests-look-at "class A(object): # A" -1)))
1478 (should (not (python-nav-backward-defun)))))
1480 (ert-deftest python-nav-backward-defun-2 ()
1481 (python-tests-with-temp-buffer
1483 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1484 '''print decorated function call data to stdout.
1486 Usage:
1488 @decoratorFunctionWithArguments('arg1', 'arg2')
1489 def func(a, b, c=True):
1490 pass
1493 def wwrap(f):
1494 print 'Inside wwrap()'
1495 def wrapped_f(*args):
1496 print 'Inside wrapped_f()'
1497 print 'Decorator arguments:', arg1, arg2, arg3
1498 f(*args)
1499 print 'After f(*args)'
1500 return wrapped_f
1501 return wwrap
1503 (goto-char (point-max))
1504 (should (= (save-excursion (python-nav-backward-defun))
1505 (python-tests-look-at " def wrapped_f(*args):" -1)))
1506 (should (= (save-excursion (python-nav-backward-defun))
1507 (python-tests-look-at " def wwrap(f):" -1)))
1508 (should (= (save-excursion (python-nav-backward-defun))
1509 (python-tests-look-at "def decoratorFunctionWithArguments(arg1, arg2, arg3):" -1)))
1510 (should (not (python-nav-backward-defun)))))
1512 (ert-deftest python-nav-backward-defun-3 ()
1513 (python-tests-with-temp-buffer
1516 def u(self):
1517 pass
1519 def v(self):
1520 pass
1522 def w(self):
1523 pass
1526 class A(object):
1527 pass
1529 (goto-char (point-min))
1530 (let ((point (python-tests-look-at "class A(object):")))
1531 (should (not (python-nav-backward-defun)))
1532 (should (= point (point))))))
1534 (ert-deftest python-nav-forward-defun-1 ()
1535 (python-tests-with-temp-buffer
1537 class A(object): # A
1539 def a(self): # a
1540 pass
1542 def b(self): # b
1543 pass
1545 class B(object): # B
1547 class C(object): # C
1549 def d(self): # d
1550 pass
1552 # def e(self): # e
1553 # pass
1555 def c(self): # c
1556 pass
1558 # def d(self): # d
1559 # pass
1561 (goto-char (point-min))
1562 (should (= (save-excursion (python-nav-forward-defun))
1563 (python-tests-look-at "(object): # A")))
1564 (should (= (save-excursion (python-nav-forward-defun))
1565 (python-tests-look-at "(self): # a")))
1566 (should (= (save-excursion (python-nav-forward-defun))
1567 (python-tests-look-at "(self): # b")))
1568 (should (= (save-excursion (python-nav-forward-defun))
1569 (python-tests-look-at "(object): # B")))
1570 (should (= (save-excursion (python-nav-forward-defun))
1571 (python-tests-look-at "(object): # C")))
1572 (should (= (save-excursion (python-nav-forward-defun))
1573 (python-tests-look-at "(self): # d")))
1574 (should (= (save-excursion (python-nav-forward-defun))
1575 (python-tests-look-at "(self): # c")))
1576 (should (not (python-nav-forward-defun)))))
1578 (ert-deftest python-nav-forward-defun-2 ()
1579 (python-tests-with-temp-buffer
1581 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1582 '''print decorated function call data to stdout.
1584 Usage:
1586 @decoratorFunctionWithArguments('arg1', 'arg2')
1587 def func(a, b, c=True):
1588 pass
1591 def wwrap(f):
1592 print 'Inside wwrap()'
1593 def wrapped_f(*args):
1594 print 'Inside wrapped_f()'
1595 print 'Decorator arguments:', arg1, arg2, arg3
1596 f(*args)
1597 print 'After f(*args)'
1598 return wrapped_f
1599 return wwrap
1601 (goto-char (point-min))
1602 (should (= (save-excursion (python-nav-forward-defun))
1603 (python-tests-look-at "(arg1, arg2, arg3):")))
1604 (should (= (save-excursion (python-nav-forward-defun))
1605 (python-tests-look-at "(f):")))
1606 (should (= (save-excursion (python-nav-forward-defun))
1607 (python-tests-look-at "(*args):")))
1608 (should (not (python-nav-forward-defun)))))
1610 (ert-deftest python-nav-forward-defun-3 ()
1611 (python-tests-with-temp-buffer
1613 class A(object):
1614 pass
1617 def u(self):
1618 pass
1620 def v(self):
1621 pass
1623 def w(self):
1624 pass
1627 (goto-char (point-min))
1628 (let ((point (python-tests-look-at "(object):")))
1629 (should (not (python-nav-forward-defun)))
1630 (should (= point (point))))))
1632 (ert-deftest python-nav-beginning-of-statement-1 ()
1633 (python-tests-with-temp-buffer
1635 v1 = 123 + \
1636 456 + \
1638 v2 = (value1,
1639 value2,
1641 value3,
1642 value4)
1643 v3 = ('this is a string'
1645 'that is continued'
1646 'between lines'
1647 'within a paren',
1648 # this is a comment, yo
1649 'continue previous line')
1650 v4 = '''
1651 a very long
1652 string
1655 (python-tests-look-at "v2 =")
1656 (python-util-forward-comment -1)
1657 (should (= (save-excursion
1658 (python-nav-beginning-of-statement)
1659 (point))
1660 (python-tests-look-at "v1 =" -1 t)))
1661 (python-tests-look-at "v3 =")
1662 (python-util-forward-comment -1)
1663 (should (= (save-excursion
1664 (python-nav-beginning-of-statement)
1665 (point))
1666 (python-tests-look-at "v2 =" -1 t)))
1667 (python-tests-look-at "v4 =")
1668 (python-util-forward-comment -1)
1669 (should (= (save-excursion
1670 (python-nav-beginning-of-statement)
1671 (point))
1672 (python-tests-look-at "v3 =" -1 t)))
1673 (goto-char (point-max))
1674 (python-util-forward-comment -1)
1675 (should (= (save-excursion
1676 (python-nav-beginning-of-statement)
1677 (point))
1678 (python-tests-look-at "v4 =" -1 t)))))
1680 (ert-deftest python-nav-end-of-statement-1 ()
1681 (python-tests-with-temp-buffer
1683 v1 = 123 + \
1684 456 + \
1686 v2 = (value1,
1687 value2,
1689 value3,
1690 value4)
1691 v3 = ('this is a string'
1693 'that is continued'
1694 'between lines'
1695 'within a paren',
1696 # this is a comment, yo
1697 'continue previous line')
1698 v4 = '''
1699 a very long
1700 string
1703 (python-tests-look-at "v1 =")
1704 (should (= (save-excursion
1705 (python-nav-end-of-statement)
1706 (point))
1707 (save-excursion
1708 (python-tests-look-at "789")
1709 (line-end-position))))
1710 (python-tests-look-at "v2 =")
1711 (should (= (save-excursion
1712 (python-nav-end-of-statement)
1713 (point))
1714 (save-excursion
1715 (python-tests-look-at "value4)")
1716 (line-end-position))))
1717 (python-tests-look-at "v3 =")
1718 (should (= (save-excursion
1719 (python-nav-end-of-statement)
1720 (point))
1721 (save-excursion
1722 (python-tests-look-at
1723 "'continue previous line')")
1724 (line-end-position))))
1725 (python-tests-look-at "v4 =")
1726 (should (= (save-excursion
1727 (python-nav-end-of-statement)
1728 (point))
1729 (save-excursion
1730 (goto-char (point-max))
1731 (python-util-forward-comment -1)
1732 (point))))))
1734 (ert-deftest python-nav-forward-statement-1 ()
1735 (python-tests-with-temp-buffer
1737 v1 = 123 + \
1738 456 + \
1740 v2 = (value1,
1741 value2,
1743 value3,
1744 value4)
1745 v3 = ('this is a string'
1747 'that is continued'
1748 'between lines'
1749 'within a paren',
1750 # this is a comment, yo
1751 'continue previous line')
1752 v4 = '''
1753 a very long
1754 string
1757 (python-tests-look-at "v1 =")
1758 (should (= (save-excursion
1759 (python-nav-forward-statement)
1760 (point))
1761 (python-tests-look-at "v2 =")))
1762 (should (= (save-excursion
1763 (python-nav-forward-statement)
1764 (point))
1765 (python-tests-look-at "v3 =")))
1766 (should (= (save-excursion
1767 (python-nav-forward-statement)
1768 (point))
1769 (python-tests-look-at "v4 =")))
1770 (should (= (save-excursion
1771 (python-nav-forward-statement)
1772 (point))
1773 (point-max)))))
1775 (ert-deftest python-nav-backward-statement-1 ()
1776 (python-tests-with-temp-buffer
1778 v1 = 123 + \
1779 456 + \
1781 v2 = (value1,
1782 value2,
1784 value3,
1785 value4)
1786 v3 = ('this is a string'
1788 'that is continued'
1789 'between lines'
1790 'within a paren',
1791 # this is a comment, yo
1792 'continue previous line')
1793 v4 = '''
1794 a very long
1795 string
1798 (goto-char (point-max))
1799 (should (= (save-excursion
1800 (python-nav-backward-statement)
1801 (point))
1802 (python-tests-look-at "v4 =" -1)))
1803 (should (= (save-excursion
1804 (python-nav-backward-statement)
1805 (point))
1806 (python-tests-look-at "v3 =" -1)))
1807 (should (= (save-excursion
1808 (python-nav-backward-statement)
1809 (point))
1810 (python-tests-look-at "v2 =" -1)))
1811 (should (= (save-excursion
1812 (python-nav-backward-statement)
1813 (point))
1814 (python-tests-look-at "v1 =" -1)))))
1816 (ert-deftest python-nav-backward-statement-2 ()
1817 :expected-result :failed
1818 (python-tests-with-temp-buffer
1820 v1 = 123 + \
1821 456 + \
1823 v2 = (value1,
1824 value2,
1826 value3,
1827 value4)
1829 ;; FIXME: For some reason `python-nav-backward-statement' is moving
1830 ;; back two sentences when starting from 'value4)'.
1831 (goto-char (point-max))
1832 (python-util-forward-comment -1)
1833 (should (= (save-excursion
1834 (python-nav-backward-statement)
1835 (point))
1836 (python-tests-look-at "v2 =" -1 t)))))
1838 (ert-deftest python-nav-beginning-of-block-1 ()
1839 (python-tests-with-temp-buffer
1841 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1842 '''print decorated function call data to stdout.
1844 Usage:
1846 @decoratorFunctionWithArguments('arg1', 'arg2')
1847 def func(a, b, c=True):
1848 pass
1851 def wwrap(f):
1852 print 'Inside wwrap()'
1853 def wrapped_f(*args):
1854 print 'Inside wrapped_f()'
1855 print 'Decorator arguments:', arg1, arg2, arg3
1856 f(*args)
1857 print 'After f(*args)'
1858 return wrapped_f
1859 return wwrap
1861 (python-tests-look-at "return wwrap")
1862 (should (= (save-excursion
1863 (python-nav-beginning-of-block)
1864 (point))
1865 (python-tests-look-at "def decoratorFunctionWithArguments" -1)))
1866 (python-tests-look-at "print 'Inside wwrap()'")
1867 (should (= (save-excursion
1868 (python-nav-beginning-of-block)
1869 (point))
1870 (python-tests-look-at "def wwrap(f):" -1)))
1871 (python-tests-look-at "print 'After f(*args)'")
1872 (end-of-line)
1873 (should (= (save-excursion
1874 (python-nav-beginning-of-block)
1875 (point))
1876 (python-tests-look-at "def wrapped_f(*args):" -1)))
1877 (python-tests-look-at "return wrapped_f")
1878 (should (= (save-excursion
1879 (python-nav-beginning-of-block)
1880 (point))
1881 (python-tests-look-at "def wwrap(f):" -1)))))
1883 (ert-deftest python-nav-end-of-block-1 ()
1884 (python-tests-with-temp-buffer
1886 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1887 '''print decorated function call data to stdout.
1889 Usage:
1891 @decoratorFunctionWithArguments('arg1', 'arg2')
1892 def func(a, b, c=True):
1893 pass
1896 def wwrap(f):
1897 print 'Inside wwrap()'
1898 def wrapped_f(*args):
1899 print 'Inside wrapped_f()'
1900 print 'Decorator arguments:', arg1, arg2, arg3
1901 f(*args)
1902 print 'After f(*args)'
1903 return wrapped_f
1904 return wwrap
1906 (python-tests-look-at "def decoratorFunctionWithArguments")
1907 (should (= (save-excursion
1908 (python-nav-end-of-block)
1909 (point))
1910 (save-excursion
1911 (goto-char (point-max))
1912 (python-util-forward-comment -1)
1913 (point))))
1914 (python-tests-look-at "def wwrap(f):")
1915 (should (= (save-excursion
1916 (python-nav-end-of-block)
1917 (point))
1918 (save-excursion
1919 (python-tests-look-at "return wrapped_f")
1920 (line-end-position))))
1921 (end-of-line)
1922 (should (= (save-excursion
1923 (python-nav-end-of-block)
1924 (point))
1925 (save-excursion
1926 (python-tests-look-at "return wrapped_f")
1927 (line-end-position))))
1928 (python-tests-look-at "f(*args)")
1929 (should (= (save-excursion
1930 (python-nav-end-of-block)
1931 (point))
1932 (save-excursion
1933 (python-tests-look-at "print 'After f(*args)'")
1934 (line-end-position))))))
1936 (ert-deftest python-nav-forward-block-1 ()
1937 "This also accounts as a test for `python-nav-backward-block'."
1938 (python-tests-with-temp-buffer
1940 if request.user.is_authenticated():
1941 # def block():
1942 # pass
1943 try:
1944 profile = request.user.get_profile()
1945 except Profile.DoesNotExist:
1946 profile = Profile.objects.create(user=request.user)
1947 else:
1948 if profile.stats:
1949 profile.recalculate_stats()
1950 else:
1951 profile.clear_stats()
1952 finally:
1953 profile.views += 1
1954 profile.save()
1956 (should (= (save-excursion (python-nav-forward-block))
1957 (python-tests-look-at "if request.user.is_authenticated():")))
1958 (should (= (save-excursion (python-nav-forward-block))
1959 (python-tests-look-at "try:")))
1960 (should (= (save-excursion (python-nav-forward-block))
1961 (python-tests-look-at "except Profile.DoesNotExist:")))
1962 (should (= (save-excursion (python-nav-forward-block))
1963 (python-tests-look-at "else:")))
1964 (should (= (save-excursion (python-nav-forward-block))
1965 (python-tests-look-at "if profile.stats:")))
1966 (should (= (save-excursion (python-nav-forward-block))
1967 (python-tests-look-at "else:")))
1968 (should (= (save-excursion (python-nav-forward-block))
1969 (python-tests-look-at "finally:")))
1970 ;; When point is at the last block, leave it there and return nil
1971 (should (not (save-excursion (python-nav-forward-block))))
1972 ;; Move backwards, and even if the number of moves is less than the
1973 ;; provided argument return the point.
1974 (should (= (save-excursion (python-nav-forward-block -10))
1975 (python-tests-look-at
1976 "if request.user.is_authenticated():" -1)))))
1978 (ert-deftest python-nav-forward-sexp-1 ()
1979 (python-tests-with-temp-buffer
1985 (python-tests-look-at "a()")
1986 (python-nav-forward-sexp)
1987 (should (looking-at "$"))
1988 (should (save-excursion
1989 (beginning-of-line)
1990 (looking-at "a()")))
1991 (python-nav-forward-sexp)
1992 (should (looking-at "$"))
1993 (should (save-excursion
1994 (beginning-of-line)
1995 (looking-at "b()")))
1996 (python-nav-forward-sexp)
1997 (should (looking-at "$"))
1998 (should (save-excursion
1999 (beginning-of-line)
2000 (looking-at "c()")))
2001 ;; The default behavior when next to a paren should do what lisp
2002 ;; does and, otherwise `blink-matching-open' breaks.
2003 (python-nav-forward-sexp -1)
2004 (should (looking-at "()"))
2005 (should (save-excursion
2006 (beginning-of-line)
2007 (looking-at "c()")))
2008 (end-of-line)
2009 ;; Skipping parens should jump to `bolp'
2010 (python-nav-forward-sexp -1 nil t)
2011 (should (looking-at "c()"))
2012 (forward-line -1)
2013 (end-of-line)
2014 ;; b()
2015 (python-nav-forward-sexp -1)
2016 (should (looking-at "()"))
2017 (python-nav-forward-sexp -1)
2018 (should (looking-at "b()"))
2019 (end-of-line)
2020 (python-nav-forward-sexp -1 nil t)
2021 (should (looking-at "b()"))
2022 (forward-line -1)
2023 (end-of-line)
2024 ;; a()
2025 (python-nav-forward-sexp -1)
2026 (should (looking-at "()"))
2027 (python-nav-forward-sexp -1)
2028 (should (looking-at "a()"))
2029 (end-of-line)
2030 (python-nav-forward-sexp -1 nil t)
2031 (should (looking-at "a()"))))
2033 (ert-deftest python-nav-forward-sexp-2 ()
2034 (python-tests-with-temp-buffer
2036 def func():
2037 if True:
2038 aaa = bbb
2039 ccc = ddd
2040 eee = fff
2041 return ggg
2043 (python-tests-look-at "aa =")
2044 (python-nav-forward-sexp)
2045 (should (looking-at " = bbb"))
2046 (python-nav-forward-sexp)
2047 (should (looking-at "$"))
2048 (should (save-excursion
2049 (back-to-indentation)
2050 (looking-at "aaa = bbb")))
2051 (python-nav-forward-sexp)
2052 (should (looking-at "$"))
2053 (should (save-excursion
2054 (back-to-indentation)
2055 (looking-at "ccc = ddd")))
2056 (python-nav-forward-sexp)
2057 (should (looking-at "$"))
2058 (should (save-excursion
2059 (back-to-indentation)
2060 (looking-at "eee = fff")))
2061 (python-nav-forward-sexp)
2062 (should (looking-at "$"))
2063 (should (save-excursion
2064 (back-to-indentation)
2065 (looking-at "return ggg")))
2066 (python-nav-forward-sexp -1)
2067 (should (looking-at "def func():"))))
2069 (ert-deftest python-nav-forward-sexp-3 ()
2070 (python-tests-with-temp-buffer
2072 from some_module import some_sub_module
2073 from another_module import another_sub_module
2075 def another_statement():
2076 pass
2078 (python-tests-look-at "some_module")
2079 (python-nav-forward-sexp)
2080 (should (looking-at " import"))
2081 (python-nav-forward-sexp)
2082 (should (looking-at " some_sub_module"))
2083 (python-nav-forward-sexp)
2084 (should (looking-at "$"))
2085 (should
2086 (save-excursion
2087 (back-to-indentation)
2088 (looking-at
2089 "from some_module import some_sub_module")))
2090 (python-nav-forward-sexp)
2091 (should (looking-at "$"))
2092 (should
2093 (save-excursion
2094 (back-to-indentation)
2095 (looking-at
2096 "from another_module import another_sub_module")))
2097 (python-nav-forward-sexp)
2098 (should (looking-at "$"))
2099 (should
2100 (save-excursion
2101 (back-to-indentation)
2102 (looking-at
2103 "pass")))
2104 (python-nav-forward-sexp -1)
2105 (should (looking-at "def another_statement():"))
2106 (python-nav-forward-sexp -1)
2107 (should (looking-at "from another_module import another_sub_module"))
2108 (python-nav-forward-sexp -1)
2109 (should (looking-at "from some_module import some_sub_module"))))
2111 (ert-deftest python-nav-forward-sexp-safe-1 ()
2112 (python-tests-with-temp-buffer
2114 profile = Profile.objects.create(user=request.user)
2115 profile.notify()
2117 (python-tests-look-at "profile =")
2118 (python-nav-forward-sexp-safe 1)
2119 (should (looking-at "$"))
2120 (beginning-of-line 1)
2121 (python-tests-look-at "user=request.user")
2122 (python-nav-forward-sexp-safe -1)
2123 (should (looking-at "(user=request.user)"))
2124 (python-nav-forward-sexp-safe -4)
2125 (should (looking-at "profile ="))
2126 (python-tests-look-at "user=request.user")
2127 (python-nav-forward-sexp-safe 3)
2128 (should (looking-at ")"))
2129 (python-nav-forward-sexp-safe 1)
2130 (should (looking-at "$"))
2131 (python-nav-forward-sexp-safe 1)
2132 (should (looking-at "$"))))
2134 (ert-deftest python-nav-up-list-1 ()
2135 (python-tests-with-temp-buffer
2137 def f():
2138 if True:
2139 return [i for i in range(3)]
2141 (python-tests-look-at "3)]")
2142 (python-nav-up-list)
2143 (should (looking-at "]"))
2144 (python-nav-up-list)
2145 (should (looking-at "$"))))
2147 (ert-deftest python-nav-backward-up-list-1 ()
2148 :expected-result :failed
2149 (python-tests-with-temp-buffer
2151 def f():
2152 if True:
2153 return [i for i in range(3)]
2155 (python-tests-look-at "3)]")
2156 (python-nav-backward-up-list)
2157 (should (looking-at "(3)\\]"))
2158 (python-nav-backward-up-list)
2159 (should (looking-at
2160 "\\[i for i in range(3)\\]"))
2161 ;; FIXME: Need to move to beginning-of-statement.
2162 (python-nav-backward-up-list)
2163 (should (looking-at
2164 "return \\[i for i in range(3)\\]"))
2165 (python-nav-backward-up-list)
2166 (should (looking-at "if True:"))
2167 (python-nav-backward-up-list)
2168 (should (looking-at "def f():"))))
2170 (ert-deftest python-indent-dedent-line-backspace-1 ()
2171 "Check de-indentation on first call. Bug#18319."
2172 (python-tests-with-temp-buffer
2174 if True:
2175 x ()
2176 if False:
2178 (python-tests-look-at "if False:")
2179 (call-interactively #'python-indent-dedent-line-backspace)
2180 (should (zerop (current-indentation)))
2181 ;; XXX: This should be a call to `undo' but it's triggering errors.
2182 (insert " ")
2183 (should (= (current-indentation) 4))
2184 (call-interactively #'python-indent-dedent-line-backspace)
2185 (should (zerop (current-indentation)))))
2187 (ert-deftest python-indent-dedent-line-backspace-2 ()
2188 "Check de-indentation with tabs. Bug#19730."
2189 (let ((tab-width 8))
2190 (python-tests-with-temp-buffer
2192 if x:
2193 \tabcdefg
2195 (python-tests-look-at "abcdefg")
2196 (goto-char (line-end-position))
2197 (call-interactively #'python-indent-dedent-line-backspace)
2198 (should
2199 (string= (buffer-substring-no-properties
2200 (line-beginning-position) (line-end-position))
2201 "\tabcdef")))))
2203 (ert-deftest python-indent-dedent-line-backspace-3 ()
2204 "Paranoid check of de-indentation with tabs. Bug#19730."
2205 (let ((tab-width 8))
2206 (python-tests-with-temp-buffer
2208 if x:
2209 \tif y:
2210 \t abcdefg
2212 (python-tests-look-at "abcdefg")
2213 (goto-char (line-end-position))
2214 (call-interactively #'python-indent-dedent-line-backspace)
2215 (should
2216 (string= (buffer-substring-no-properties
2217 (line-beginning-position) (line-end-position))
2218 "\t abcdef"))
2219 (back-to-indentation)
2220 (call-interactively #'python-indent-dedent-line-backspace)
2221 (should
2222 (string= (buffer-substring-no-properties
2223 (line-beginning-position) (line-end-position))
2224 "\tabcdef"))
2225 (call-interactively #'python-indent-dedent-line-backspace)
2226 (should
2227 (string= (buffer-substring-no-properties
2228 (line-beginning-position) (line-end-position))
2229 " abcdef"))
2230 (call-interactively #'python-indent-dedent-line-backspace)
2231 (should
2232 (string= (buffer-substring-no-properties
2233 (line-beginning-position) (line-end-position))
2234 "abcdef")))))
2237 ;;; Shell integration
2239 (defvar python-tests-shell-interpreter "python")
2241 (ert-deftest python-shell-get-process-name-1 ()
2242 "Check process name calculation sans `buffer-file-name'."
2243 (python-tests-with-temp-buffer
2245 (should (string= (python-shell-get-process-name nil)
2246 python-shell-buffer-name))
2247 (should (string= (python-shell-get-process-name t)
2248 (format "%s[%s]" python-shell-buffer-name (buffer-name))))))
2250 (ert-deftest python-shell-get-process-name-2 ()
2251 "Check process name calculation with `buffer-file-name'."
2252 (python-tests-with-temp-file
2254 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
2255 ;; should be respected.
2256 (should (string= (python-shell-get-process-name nil)
2257 python-shell-buffer-name))
2258 (should (string=
2259 (python-shell-get-process-name t)
2260 (format "%s[%s]" python-shell-buffer-name (buffer-name))))))
2262 (ert-deftest python-shell-internal-get-process-name-1 ()
2263 "Check the internal process name is buffer-unique sans `buffer-file-name'."
2264 (python-tests-with-temp-buffer
2266 (should (string= (python-shell-internal-get-process-name)
2267 (format "%s[%s]" python-shell-internal-buffer-name (buffer-name))))))
2269 (ert-deftest python-shell-internal-get-process-name-2 ()
2270 "Check the internal process name is buffer-unique with `buffer-file-name'."
2271 (python-tests-with-temp-file
2273 (should (string= (python-shell-internal-get-process-name)
2274 (format "%s[%s]" python-shell-internal-buffer-name (buffer-name))))))
2276 (ert-deftest python-shell-calculate-command-1 ()
2277 "Check the command to execute is calculated correctly.
2278 Using `python-shell-interpreter' and
2279 `python-shell-interpreter-args'."
2280 (skip-unless (executable-find python-tests-shell-interpreter))
2281 (let ((python-shell-interpreter (executable-find
2282 python-tests-shell-interpreter))
2283 (python-shell-interpreter-args "-B"))
2284 (should (string=
2285 (format "%s %s"
2286 python-shell-interpreter
2287 python-shell-interpreter-args)
2288 (python-shell-calculate-command)))))
2290 (ert-deftest python-shell-calculate-process-environment-1 ()
2291 "Test `python-shell-process-environment' modification."
2292 (let* ((python-shell-process-environment
2293 '("TESTVAR1=value1" "TESTVAR2=value2"))
2294 (process-environment
2295 (python-shell-calculate-process-environment)))
2296 (should (equal (getenv "TESTVAR1") "value1"))
2297 (should (equal (getenv "TESTVAR2") "value2"))))
2299 (ert-deftest python-shell-calculate-process-environment-2 ()
2300 "Test `python-shell-extra-pythonpaths' modification."
2301 (let* ((process-environment process-environment)
2302 (original-pythonpath (setenv "PYTHONPATH" "path3"))
2303 (paths '("path1" "path2"))
2304 (python-shell-extra-pythonpaths paths)
2305 (process-environment
2306 (python-shell-calculate-process-environment)))
2307 (should (equal (getenv "PYTHONPATH")
2308 (concat
2309 (mapconcat 'identity paths path-separator)
2310 path-separator original-pythonpath)))))
2312 (ert-deftest python-shell-calculate-process-environment-3 ()
2313 "Test `python-shell-virtualenv-root' modification."
2314 (let* ((original-path (or (getenv "PATH") ""))
2315 (python-shell-virtualenv-root
2316 (directory-file-name user-emacs-directory))
2317 (process-environment
2318 (python-shell-calculate-process-environment)))
2319 (should (not (getenv "PYTHONHOME")))
2320 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-root))
2321 (should (equal (getenv "PATH")
2322 (format "%s/bin%s%s"
2323 python-shell-virtualenv-root
2324 path-separator original-path)))))
2326 (ert-deftest python-shell-calculate-process-environment-4 ()
2327 "Test `python-shell-unbuffered' modification."
2328 (setenv "PYTHONUNBUFFERED")
2329 (let* ((process-environment
2330 (python-shell-calculate-process-environment)))
2331 ;; Defaults to t
2332 (should python-shell-unbuffered)
2333 (should (string= (getenv "PYTHONUNBUFFERED") "1"))))
2335 (ert-deftest python-shell-calculate-process-environment-5 ()
2336 (setenv "PYTHONUNBUFFERED")
2337 "Test `python-shell-unbuffered' modification."
2338 (let* ((python-shell-unbuffered nil)
2339 (process-environment
2340 (python-shell-calculate-process-environment)))
2341 (should (not (getenv "PYTHONUNBUFFERED")))))
2343 (ert-deftest python-shell-calculate-exec-path-1 ()
2344 "Test `python-shell-exec-path' modification."
2345 (let* ((original-exec-path exec-path)
2346 (python-shell-exec-path '("path1" "path2"))
2347 (exec-path (python-shell-calculate-exec-path)))
2348 (should (equal
2349 exec-path
2350 (append python-shell-exec-path
2351 original-exec-path)))))
2353 (ert-deftest python-shell-calculate-exec-path-2 ()
2354 "Test `python-shell-exec-path' modification."
2355 (let* ((original-exec-path exec-path)
2356 (python-shell-virtualenv-root
2357 (directory-file-name (expand-file-name user-emacs-directory)))
2358 (exec-path (python-shell-calculate-exec-path)))
2359 (should (equal
2360 exec-path
2361 (append (cons
2362 (format "%s/bin" python-shell-virtualenv-root)
2363 original-exec-path))))))
2365 (ert-deftest python-shell-make-comint-1 ()
2366 "Check comint creation for global shell buffer."
2367 (skip-unless (executable-find python-tests-shell-interpreter))
2368 ;; The interpreter can get killed too quickly to allow it to clean
2369 ;; up the tempfiles that the default python-shell-setup-codes create,
2370 ;; so it leaves tempfiles behind, which is a minor irritation.
2371 (let* ((python-shell-setup-codes nil)
2372 (python-shell-interpreter
2373 (executable-find python-tests-shell-interpreter))
2374 (proc-name (python-shell-get-process-name nil))
2375 (shell-buffer
2376 (python-tests-with-temp-buffer
2377 "" (python-shell-make-comint
2378 (python-shell-calculate-command) proc-name)))
2379 (process (get-buffer-process shell-buffer)))
2380 (unwind-protect
2381 (progn
2382 (set-process-query-on-exit-flag process nil)
2383 (should (process-live-p process))
2384 (with-current-buffer shell-buffer
2385 (should (eq major-mode 'inferior-python-mode))
2386 (should (string= (buffer-name) (format "*%s*" proc-name)))))
2387 (kill-buffer shell-buffer))))
2389 (ert-deftest python-shell-make-comint-2 ()
2390 "Check comint creation for internal shell buffer."
2391 (skip-unless (executable-find python-tests-shell-interpreter))
2392 (let* ((python-shell-setup-codes nil)
2393 (python-shell-interpreter
2394 (executable-find python-tests-shell-interpreter))
2395 (proc-name (python-shell-internal-get-process-name))
2396 (shell-buffer
2397 (python-tests-with-temp-buffer
2398 "" (python-shell-make-comint
2399 (python-shell-calculate-command) proc-name nil t)))
2400 (process (get-buffer-process shell-buffer)))
2401 (unwind-protect
2402 (progn
2403 (set-process-query-on-exit-flag process nil)
2404 (should (process-live-p process))
2405 (with-current-buffer shell-buffer
2406 (should (eq major-mode 'inferior-python-mode))
2407 (should (string= (buffer-name) (format " *%s*" proc-name)))))
2408 (kill-buffer shell-buffer))))
2410 (ert-deftest python-shell-make-comint-3 ()
2411 "Check comint creation with overridden python interpreter and args.
2412 The command passed to `python-shell-make-comint' as argument must
2413 locally override global values set in `python-shell-interpreter'
2414 and `python-shell-interpreter-args' in the new shell buffer."
2415 (skip-unless (executable-find python-tests-shell-interpreter))
2416 (let* ((python-shell-setup-codes nil)
2417 (python-shell-interpreter "interpreter")
2418 (python-shell-interpreter-args "--some-args")
2419 (proc-name (python-shell-get-process-name nil))
2420 (interpreter-override
2421 (concat (executable-find python-tests-shell-interpreter) " " "-i"))
2422 (shell-buffer
2423 (python-tests-with-temp-buffer
2424 "" (python-shell-make-comint interpreter-override proc-name nil)))
2425 (process (get-buffer-process shell-buffer)))
2426 (unwind-protect
2427 (progn
2428 (set-process-query-on-exit-flag process nil)
2429 (should (process-live-p process))
2430 (with-current-buffer shell-buffer
2431 (should (eq major-mode 'inferior-python-mode))
2432 (should (file-equal-p
2433 python-shell-interpreter
2434 (executable-find python-tests-shell-interpreter)))
2435 (should (string= python-shell-interpreter-args "-i"))))
2436 (kill-buffer shell-buffer))))
2438 (ert-deftest python-shell-make-comint-4 ()
2439 "Check shell calculated prompts regexps are set."
2440 (skip-unless (executable-find python-tests-shell-interpreter))
2441 (let* ((process-environment process-environment)
2442 (python-shell-setup-codes nil)
2443 (python-shell-interpreter
2444 (executable-find python-tests-shell-interpreter))
2445 (python-shell-interpreter-args "-i")
2446 (python-shell--prompt-calculated-input-regexp nil)
2447 (python-shell--prompt-calculated-output-regexp nil)
2448 (python-shell-prompt-detect-enabled t)
2449 (python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2450 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2451 (python-shell-prompt-regexp "in")
2452 (python-shell-prompt-block-regexp "block")
2453 (python-shell-prompt-pdb-regexp "pdf")
2454 (python-shell-prompt-output-regexp "output")
2455 (startup-code (concat "import sys\n"
2456 "sys.ps1 = 'py> '\n"
2457 "sys.ps2 = '..> '\n"
2458 "sys.ps3 = 'out '\n"))
2459 (startup-file (python-shell--save-temp-file startup-code))
2460 (proc-name (python-shell-get-process-name nil))
2461 (shell-buffer
2462 (progn
2463 (setenv "PYTHONSTARTUP" startup-file)
2464 (python-tests-with-temp-buffer
2465 "" (python-shell-make-comint
2466 (python-shell-calculate-command) proc-name nil))))
2467 (process (get-buffer-process shell-buffer)))
2468 (unwind-protect
2469 (progn
2470 (set-process-query-on-exit-flag process nil)
2471 (should (process-live-p process))
2472 (with-current-buffer shell-buffer
2473 (should (eq major-mode 'inferior-python-mode))
2474 (should (string=
2475 python-shell--prompt-calculated-input-regexp
2476 (concat "^\\(extralargeinputprompt\\|\\.\\.> \\|"
2477 "block\\|py> \\|pdf\\|sml\\|in\\)")))
2478 (should (string=
2479 python-shell--prompt-calculated-output-regexp
2480 "^\\(extralargeoutputprompt\\|output\\|out \\|sml\\)"))))
2481 (delete-file startup-file)
2482 (kill-buffer shell-buffer))))
2484 (ert-deftest python-shell-get-process-1 ()
2485 "Check dedicated shell process preference over global."
2486 (skip-unless (executable-find python-tests-shell-interpreter))
2487 (python-tests-with-temp-file
2489 (let* ((python-shell-setup-codes nil)
2490 (python-shell-interpreter
2491 (executable-find python-tests-shell-interpreter))
2492 (global-proc-name (python-shell-get-process-name nil))
2493 (dedicated-proc-name (python-shell-get-process-name t))
2494 (global-shell-buffer
2495 (python-shell-make-comint
2496 (python-shell-calculate-command) global-proc-name))
2497 (dedicated-shell-buffer
2498 (python-shell-make-comint
2499 (python-shell-calculate-command) dedicated-proc-name))
2500 (global-process (get-buffer-process global-shell-buffer))
2501 (dedicated-process (get-buffer-process dedicated-shell-buffer)))
2502 (unwind-protect
2503 (progn
2504 (set-process-query-on-exit-flag global-process nil)
2505 (set-process-query-on-exit-flag dedicated-process nil)
2506 ;; Prefer dedicated if global also exists.
2507 (should (equal (python-shell-get-process) dedicated-process))
2508 (kill-buffer dedicated-shell-buffer)
2509 ;; If there's only global, use it.
2510 (should (equal (python-shell-get-process) global-process))
2511 (kill-buffer global-shell-buffer)
2512 ;; No buffer available.
2513 (should (not (python-shell-get-process))))
2514 (ignore-errors (kill-buffer global-shell-buffer))
2515 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
2517 (ert-deftest python-shell-internal-get-or-create-process-1 ()
2518 "Check internal shell process creation fallback."
2519 (skip-unless (executable-find python-tests-shell-interpreter))
2520 (python-tests-with-temp-file
2522 (should (not (process-live-p (python-shell-internal-get-process-name))))
2523 (let* ((python-shell-interpreter
2524 (executable-find python-tests-shell-interpreter))
2525 (internal-process-name (python-shell-internal-get-process-name))
2526 (internal-process (python-shell-internal-get-or-create-process))
2527 (internal-shell-buffer (process-buffer internal-process)))
2528 (unwind-protect
2529 (progn
2530 (set-process-query-on-exit-flag internal-process nil)
2531 (should (equal (process-name internal-process)
2532 internal-process-name))
2533 (should (equal internal-process
2534 (python-shell-internal-get-or-create-process)))
2535 ;; Assert the internal process is not a user process
2536 (should (not (python-shell-get-process)))
2537 (kill-buffer internal-shell-buffer))
2538 (ignore-errors (kill-buffer internal-shell-buffer))))))
2540 (ert-deftest python-shell-prompt-detect-1 ()
2541 "Check prompt autodetection."
2542 (skip-unless (executable-find python-tests-shell-interpreter))
2543 (let ((process-environment process-environment))
2544 ;; Ensure no startup file is enabled
2545 (setenv "PYTHONSTARTUP" "")
2546 (should python-shell-prompt-detect-enabled)
2547 (should (equal (python-shell-prompt-detect) '(">>> " "... " "")))))
2549 (ert-deftest python-shell-prompt-detect-2 ()
2550 "Check prompt autodetection with startup file. Bug#17370."
2551 (skip-unless (executable-find python-tests-shell-interpreter))
2552 (let* ((process-environment process-environment)
2553 (startup-code (concat "import sys\n"
2554 "sys.ps1 = 'py> '\n"
2555 "sys.ps2 = '..> '\n"
2556 "sys.ps3 = 'out '\n"))
2557 (startup-file (python-shell--save-temp-file startup-code)))
2558 (unwind-protect
2559 (progn
2560 ;; Ensure startup file is enabled
2561 (setenv "PYTHONSTARTUP" startup-file)
2562 (should python-shell-prompt-detect-enabled)
2563 (should (equal (python-shell-prompt-detect) '("py> " "..> " "out "))))
2564 (ignore-errors (delete-file startup-file)))))
2566 (ert-deftest python-shell-prompt-detect-3 ()
2567 "Check prompts are not autodetected when feature is disabled."
2568 (skip-unless (executable-find python-tests-shell-interpreter))
2569 (let ((process-environment process-environment)
2570 (python-shell-prompt-detect-enabled nil))
2571 ;; Ensure no startup file is enabled
2572 (should (not python-shell-prompt-detect-enabled))
2573 (should (not (python-shell-prompt-detect)))))
2575 (ert-deftest python-shell-prompt-detect-4 ()
2576 "Check warning is shown when detection fails."
2577 (skip-unless (executable-find python-tests-shell-interpreter))
2578 (let* ((process-environment process-environment)
2579 ;; Trigger failure by removing prompts in the startup file
2580 (startup-code (concat "import sys\n"
2581 "sys.ps1 = ''\n"
2582 "sys.ps2 = ''\n"
2583 "sys.ps3 = ''\n"))
2584 (startup-file (python-shell--save-temp-file startup-code)))
2585 (unwind-protect
2586 (progn
2587 (kill-buffer (get-buffer-create "*Warnings*"))
2588 (should (not (get-buffer "*Warnings*")))
2589 (setenv "PYTHONSTARTUP" startup-file)
2590 (should python-shell-prompt-detect-failure-warning)
2591 (should python-shell-prompt-detect-enabled)
2592 (should (not (python-shell-prompt-detect)))
2593 (should (get-buffer "*Warnings*")))
2594 (ignore-errors (delete-file startup-file)))))
2596 (ert-deftest python-shell-prompt-detect-5 ()
2597 "Check disabled warnings are not shown when detection fails."
2598 (skip-unless (executable-find python-tests-shell-interpreter))
2599 (let* ((process-environment process-environment)
2600 (startup-code (concat "import sys\n"
2601 "sys.ps1 = ''\n"
2602 "sys.ps2 = ''\n"
2603 "sys.ps3 = ''\n"))
2604 (startup-file (python-shell--save-temp-file startup-code))
2605 (python-shell-prompt-detect-failure-warning nil))
2606 (unwind-protect
2607 (progn
2608 (kill-buffer (get-buffer-create "*Warnings*"))
2609 (should (not (get-buffer "*Warnings*")))
2610 (setenv "PYTHONSTARTUP" startup-file)
2611 (should (not python-shell-prompt-detect-failure-warning))
2612 (should python-shell-prompt-detect-enabled)
2613 (should (not (python-shell-prompt-detect)))
2614 (should (not (get-buffer "*Warnings*"))))
2615 (ignore-errors (delete-file startup-file)))))
2617 (ert-deftest python-shell-prompt-detect-6 ()
2618 "Warnings are not shown when detection is disabled."
2619 (skip-unless (executable-find python-tests-shell-interpreter))
2620 (let* ((process-environment process-environment)
2621 (startup-code (concat "import sys\n"
2622 "sys.ps1 = ''\n"
2623 "sys.ps2 = ''\n"
2624 "sys.ps3 = ''\n"))
2625 (startup-file (python-shell--save-temp-file startup-code))
2626 (python-shell-prompt-detect-failure-warning t)
2627 (python-shell-prompt-detect-enabled nil))
2628 (unwind-protect
2629 (progn
2630 (kill-buffer (get-buffer-create "*Warnings*"))
2631 (should (not (get-buffer "*Warnings*")))
2632 (setenv "PYTHONSTARTUP" startup-file)
2633 (should python-shell-prompt-detect-failure-warning)
2634 (should (not python-shell-prompt-detect-enabled))
2635 (should (not (python-shell-prompt-detect)))
2636 (should (not (get-buffer "*Warnings*"))))
2637 (ignore-errors (delete-file startup-file)))))
2639 (ert-deftest python-shell-prompt-validate-regexps-1 ()
2640 "Check `python-shell-prompt-input-regexps' are validated."
2641 (let* ((python-shell-prompt-input-regexps '("\\("))
2642 (error-data (should-error (python-shell-prompt-validate-regexps)
2643 :type 'user-error)))
2644 (should
2645 (string= (cadr error-data)
2646 "Invalid regexp \\( in `python-shell-prompt-input-regexps'"))))
2648 (ert-deftest python-shell-prompt-validate-regexps-2 ()
2649 "Check `python-shell-prompt-output-regexps' are validated."
2650 (let* ((python-shell-prompt-output-regexps '("\\("))
2651 (error-data (should-error (python-shell-prompt-validate-regexps)
2652 :type 'user-error)))
2653 (should
2654 (string= (cadr error-data)
2655 "Invalid regexp \\( in `python-shell-prompt-output-regexps'"))))
2657 (ert-deftest python-shell-prompt-validate-regexps-3 ()
2658 "Check `python-shell-prompt-regexp' is validated."
2659 (let* ((python-shell-prompt-regexp "\\(")
2660 (error-data (should-error (python-shell-prompt-validate-regexps)
2661 :type 'user-error)))
2662 (should
2663 (string= (cadr error-data)
2664 "Invalid regexp \\( in `python-shell-prompt-regexp'"))))
2666 (ert-deftest python-shell-prompt-validate-regexps-4 ()
2667 "Check `python-shell-prompt-block-regexp' is validated."
2668 (let* ((python-shell-prompt-block-regexp "\\(")
2669 (error-data (should-error (python-shell-prompt-validate-regexps)
2670 :type 'user-error)))
2671 (should
2672 (string= (cadr error-data)
2673 "Invalid regexp \\( in `python-shell-prompt-block-regexp'"))))
2675 (ert-deftest python-shell-prompt-validate-regexps-5 ()
2676 "Check `python-shell-prompt-pdb-regexp' is validated."
2677 (let* ((python-shell-prompt-pdb-regexp "\\(")
2678 (error-data (should-error (python-shell-prompt-validate-regexps)
2679 :type 'user-error)))
2680 (should
2681 (string= (cadr error-data)
2682 "Invalid regexp \\( in `python-shell-prompt-pdb-regexp'"))))
2684 (ert-deftest python-shell-prompt-validate-regexps-6 ()
2685 "Check `python-shell-prompt-output-regexp' is validated."
2686 (let* ((python-shell-prompt-output-regexp "\\(")
2687 (error-data (should-error (python-shell-prompt-validate-regexps)
2688 :type 'user-error)))
2689 (should
2690 (string= (cadr error-data)
2691 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2693 (ert-deftest python-shell-prompt-validate-regexps-7 ()
2694 "Check default regexps are valid."
2695 ;; should not signal error
2696 (python-shell-prompt-validate-regexps))
2698 (ert-deftest python-shell-prompt-set-calculated-regexps-1 ()
2699 "Check regexps are validated."
2700 (let* ((python-shell-prompt-output-regexp '("\\("))
2701 (python-shell--prompt-calculated-input-regexp nil)
2702 (python-shell--prompt-calculated-output-regexp nil)
2703 (python-shell-prompt-detect-enabled nil)
2704 (error-data (should-error (python-shell-prompt-set-calculated-regexps)
2705 :type 'user-error)))
2706 (should
2707 (string= (cadr error-data)
2708 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2710 (ert-deftest python-shell-prompt-set-calculated-regexps-2 ()
2711 "Check `python-shell-prompt-input-regexps' are set."
2712 (let* ((python-shell-prompt-input-regexps '("my" "prompt"))
2713 (python-shell-prompt-output-regexps '(""))
2714 (python-shell-prompt-regexp "")
2715 (python-shell-prompt-block-regexp "")
2716 (python-shell-prompt-pdb-regexp "")
2717 (python-shell-prompt-output-regexp "")
2718 (python-shell--prompt-calculated-input-regexp nil)
2719 (python-shell--prompt-calculated-output-regexp nil)
2720 (python-shell-prompt-detect-enabled nil))
2721 (python-shell-prompt-set-calculated-regexps)
2722 (should (string= python-shell--prompt-calculated-input-regexp
2723 "^\\(prompt\\|my\\|\\)"))))
2725 (ert-deftest python-shell-prompt-set-calculated-regexps-3 ()
2726 "Check `python-shell-prompt-output-regexps' are set."
2727 (let* ((python-shell-prompt-input-regexps '(""))
2728 (python-shell-prompt-output-regexps '("my" "prompt"))
2729 (python-shell-prompt-regexp "")
2730 (python-shell-prompt-block-regexp "")
2731 (python-shell-prompt-pdb-regexp "")
2732 (python-shell-prompt-output-regexp "")
2733 (python-shell--prompt-calculated-input-regexp nil)
2734 (python-shell--prompt-calculated-output-regexp nil)
2735 (python-shell-prompt-detect-enabled nil))
2736 (python-shell-prompt-set-calculated-regexps)
2737 (should (string= python-shell--prompt-calculated-output-regexp
2738 "^\\(prompt\\|my\\|\\)"))))
2740 (ert-deftest python-shell-prompt-set-calculated-regexps-4 ()
2741 "Check user defined prompts are set."
2742 (let* ((python-shell-prompt-input-regexps '(""))
2743 (python-shell-prompt-output-regexps '(""))
2744 (python-shell-prompt-regexp "prompt")
2745 (python-shell-prompt-block-regexp "block")
2746 (python-shell-prompt-pdb-regexp "pdb")
2747 (python-shell-prompt-output-regexp "output")
2748 (python-shell--prompt-calculated-input-regexp nil)
2749 (python-shell--prompt-calculated-output-regexp nil)
2750 (python-shell-prompt-detect-enabled nil))
2751 (python-shell-prompt-set-calculated-regexps)
2752 (should (string= python-shell--prompt-calculated-input-regexp
2753 "^\\(prompt\\|block\\|pdb\\|\\)"))
2754 (should (string= python-shell--prompt-calculated-output-regexp
2755 "^\\(output\\|\\)"))))
2757 (ert-deftest python-shell-prompt-set-calculated-regexps-5 ()
2758 "Check order of regexps (larger first)."
2759 (let* ((python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2760 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2761 (python-shell-prompt-regexp "in")
2762 (python-shell-prompt-block-regexp "block")
2763 (python-shell-prompt-pdb-regexp "pdf")
2764 (python-shell-prompt-output-regexp "output")
2765 (python-shell--prompt-calculated-input-regexp nil)
2766 (python-shell--prompt-calculated-output-regexp nil)
2767 (python-shell-prompt-detect-enabled nil))
2768 (python-shell-prompt-set-calculated-regexps)
2769 (should (string= python-shell--prompt-calculated-input-regexp
2770 "^\\(extralargeinputprompt\\|block\\|pdf\\|sml\\|in\\)"))
2771 (should (string= python-shell--prompt-calculated-output-regexp
2772 "^\\(extralargeoutputprompt\\|output\\|sml\\)"))))
2774 (ert-deftest python-shell-prompt-set-calculated-regexps-6 ()
2775 "Check detected prompts are included `regexp-quote'd."
2776 (skip-unless (executable-find python-tests-shell-interpreter))
2777 (let* ((python-shell-prompt-input-regexps '(""))
2778 (python-shell-prompt-output-regexps '(""))
2779 (python-shell-prompt-regexp "")
2780 (python-shell-prompt-block-regexp "")
2781 (python-shell-prompt-pdb-regexp "")
2782 (python-shell-prompt-output-regexp "")
2783 (python-shell--prompt-calculated-input-regexp nil)
2784 (python-shell--prompt-calculated-output-regexp nil)
2785 (python-shell-prompt-detect-enabled t)
2786 (process-environment process-environment)
2787 (startup-code (concat "import sys\n"
2788 "sys.ps1 = 'p.> '\n"
2789 "sys.ps2 = '..> '\n"
2790 "sys.ps3 = 'o.t '\n"))
2791 (startup-file (python-shell--save-temp-file startup-code)))
2792 (unwind-protect
2793 (progn
2794 (setenv "PYTHONSTARTUP" startup-file)
2795 (python-shell-prompt-set-calculated-regexps)
2796 (should (string= python-shell--prompt-calculated-input-regexp
2797 "^\\(\\.\\.> \\|p\\.> \\|\\)"))
2798 (should (string= python-shell--prompt-calculated-output-regexp
2799 "^\\(o\\.t \\|\\)")))
2800 (ignore-errors (delete-file startup-file)))))
2802 (ert-deftest python-shell-buffer-substring-1 ()
2803 "Selecting a substring of the whole buffer must match its contents."
2804 (python-tests-with-temp-buffer
2806 class Foo(models.Model):
2807 pass
2810 class Bar(models.Model):
2811 pass
2813 (should (string= (buffer-string)
2814 (python-shell-buffer-substring (point-min) (point-max))))))
2816 (ert-deftest python-shell-buffer-substring-2 ()
2817 "Main block should be removed if NOMAIN is non-nil."
2818 (python-tests-with-temp-buffer
2820 class Foo(models.Model):
2821 pass
2823 class Bar(models.Model):
2824 pass
2826 if __name__ == \"__main__\":
2827 foo = Foo()
2828 print (foo)
2830 (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
2832 class Foo(models.Model):
2833 pass
2835 class Bar(models.Model):
2836 pass
2841 "))))
2843 (ert-deftest python-shell-buffer-substring-3 ()
2844 "Main block should be removed if NOMAIN is non-nil."
2845 (python-tests-with-temp-buffer
2847 class Foo(models.Model):
2848 pass
2850 if __name__ == \"__main__\":
2851 foo = Foo()
2852 print (foo)
2854 class Bar(models.Model):
2855 pass
2857 (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
2859 class Foo(models.Model):
2860 pass
2866 class Bar(models.Model):
2867 pass
2868 "))))
2870 (ert-deftest python-shell-buffer-substring-4 ()
2871 "Coding cookie should be added for substrings."
2872 (python-tests-with-temp-buffer
2873 "# coding: latin-1
2875 class Foo(models.Model):
2876 pass
2878 if __name__ == \"__main__\":
2879 foo = Foo()
2880 print (foo)
2882 class Bar(models.Model):
2883 pass
2885 (should (string= (python-shell-buffer-substring
2886 (python-tests-look-at "class Foo(models.Model):")
2887 (progn (python-nav-forward-sexp) (point)))
2888 "# -*- coding: latin-1 -*-
2890 class Foo(models.Model):
2891 pass"))))
2893 (ert-deftest python-shell-buffer-substring-5 ()
2894 "The proper amount of blank lines is added for a substring."
2895 (python-tests-with-temp-buffer
2896 "# coding: latin-1
2898 class Foo(models.Model):
2899 pass
2901 if __name__ == \"__main__\":
2902 foo = Foo()
2903 print (foo)
2905 class Bar(models.Model):
2906 pass
2908 (should (string= (python-shell-buffer-substring
2909 (python-tests-look-at "class Bar(models.Model):")
2910 (progn (python-nav-forward-sexp) (point)))
2911 "# -*- coding: latin-1 -*-
2920 class Bar(models.Model):
2921 pass"))))
2923 (ert-deftest python-shell-buffer-substring-6 ()
2924 "Handle substring with coding cookie in the second line."
2925 (python-tests-with-temp-buffer
2927 # coding: latin-1
2929 class Foo(models.Model):
2930 pass
2932 if __name__ == \"__main__\":
2933 foo = Foo()
2934 print (foo)
2936 class Bar(models.Model):
2937 pass
2939 (should (string= (python-shell-buffer-substring
2940 (python-tests-look-at "# coding: latin-1")
2941 (python-tests-look-at "if __name__ == \"__main__\":"))
2942 "# -*- coding: latin-1 -*-
2945 class Foo(models.Model):
2946 pass
2948 "))))
2950 (ert-deftest python-shell-buffer-substring-7 ()
2951 "Ensure first coding cookie gets precedence."
2952 (python-tests-with-temp-buffer
2953 "# coding: utf-8
2954 # coding: latin-1
2956 class Foo(models.Model):
2957 pass
2959 if __name__ == \"__main__\":
2960 foo = Foo()
2961 print (foo)
2963 class Bar(models.Model):
2964 pass
2966 (should (string= (python-shell-buffer-substring
2967 (python-tests-look-at "# coding: latin-1")
2968 (python-tests-look-at "if __name__ == \"__main__\":"))
2969 "# -*- coding: utf-8 -*-
2972 class Foo(models.Model):
2973 pass
2975 "))))
2977 (ert-deftest python-shell-buffer-substring-8 ()
2978 "Ensure first coding cookie gets precedence when sending whole buffer."
2979 (python-tests-with-temp-buffer
2980 "# coding: utf-8
2981 # coding: latin-1
2983 class Foo(models.Model):
2984 pass
2986 (should (string= (python-shell-buffer-substring (point-min) (point-max))
2987 "# coding: utf-8
2990 class Foo(models.Model):
2991 pass
2992 "))))
2994 (ert-deftest python-shell-buffer-substring-9 ()
2995 "Check substring starting from `point-min'."
2996 (python-tests-with-temp-buffer
2997 "# coding: utf-8
2999 class Foo(models.Model):
3000 pass
3002 class Bar(models.Model):
3003 pass
3005 (should (string= (python-shell-buffer-substring
3006 (point-min)
3007 (python-tests-look-at "class Bar(models.Model):"))
3008 "# coding: utf-8
3010 class Foo(models.Model):
3011 pass
3013 "))))
3016 ;;; Shell completion
3018 (ert-deftest python-shell-completion-native-interpreter-disabled-p-1 ()
3019 (let* ((python-shell-completion-native-disabled-interpreters (list "pypy"))
3020 (python-shell-interpreter "/some/path/to/bin/pypy"))
3021 (should (python-shell-completion-native-interpreter-disabled-p))))
3026 ;;; PDB Track integration
3029 ;;; Symbol completion
3032 ;;; Fill paragraph
3035 ;;; Skeletons
3038 ;;; FFAP
3041 ;;; Code check
3044 ;;; Eldoc
3046 (ert-deftest python-eldoc--get-symbol-at-point-1 ()
3047 "Test paren handling."
3048 (python-tests-with-temp-buffer
3050 map(xx
3051 map(codecs.open('somefile'
3053 (python-tests-look-at "ap(xx")
3054 (should (string= (python-eldoc--get-symbol-at-point) "map"))
3055 (goto-char (line-end-position))
3056 (should (string= (python-eldoc--get-symbol-at-point) "map"))
3057 (python-tests-look-at "('somefile'")
3058 (should (string= (python-eldoc--get-symbol-at-point) "map"))
3059 (goto-char (line-end-position))
3060 (should (string= (python-eldoc--get-symbol-at-point) "codecs.open"))))
3062 (ert-deftest python-eldoc--get-symbol-at-point-2 ()
3063 "Ensure self is replaced with the class name."
3064 (python-tests-with-temp-buffer
3066 class TheClass:
3068 def some_method(self, n):
3069 return n
3071 def other(self):
3072 return self.some_method(1234)
3075 (python-tests-look-at "self.some_method")
3076 (should (string= (python-eldoc--get-symbol-at-point)
3077 "TheClass.some_method"))
3078 (python-tests-look-at "1234)")
3079 (should (string= (python-eldoc--get-symbol-at-point)
3080 "TheClass.some_method"))))
3082 (ert-deftest python-eldoc--get-symbol-at-point-3 ()
3083 "Ensure symbol is found when point is at end of buffer."
3084 (python-tests-with-temp-buffer
3086 some_symbol
3089 (goto-char (point-max))
3090 (should (string= (python-eldoc--get-symbol-at-point)
3091 "some_symbol"))))
3093 (ert-deftest python-eldoc--get-symbol-at-point-4 ()
3094 "Ensure symbol is found when point is at whitespace."
3095 (python-tests-with-temp-buffer
3097 some_symbol some_other_symbol
3099 (python-tests-look-at " some_other_symbol")
3100 (should (string= (python-eldoc--get-symbol-at-point)
3101 "some_symbol"))))
3104 ;;; Imenu
3106 (ert-deftest python-imenu-create-index-1 ()
3107 (python-tests-with-temp-buffer
3109 class Foo(models.Model):
3110 pass
3113 class Bar(models.Model):
3114 pass
3117 def decorator(arg1, arg2, arg3):
3118 '''print decorated function call data to stdout.
3120 Usage:
3122 @decorator('arg1', 'arg2')
3123 def func(a, b, c=True):
3124 pass
3127 def wrap(f):
3128 print ('wrap')
3129 def wrapped_f(*args):
3130 print ('wrapped_f')
3131 print ('Decorator arguments:', arg1, arg2, arg3)
3132 f(*args)
3133 print ('called f(*args)')
3134 return wrapped_f
3135 return wrap
3138 class Baz(object):
3140 def a(self):
3141 pass
3143 def b(self):
3144 pass
3146 class Frob(object):
3148 def c(self):
3149 pass
3151 (goto-char (point-max))
3152 (should (equal
3153 (list
3154 (cons "Foo (class)" (copy-marker 2))
3155 (cons "Bar (class)" (copy-marker 38))
3156 (list
3157 "decorator (def)"
3158 (cons "*function definition*" (copy-marker 74))
3159 (list
3160 "wrap (def)"
3161 (cons "*function definition*" (copy-marker 254))
3162 (cons "wrapped_f (def)" (copy-marker 294))))
3163 (list
3164 "Baz (class)"
3165 (cons "*class definition*" (copy-marker 519))
3166 (cons "a (def)" (copy-marker 539))
3167 (cons "b (def)" (copy-marker 570))
3168 (list
3169 "Frob (class)"
3170 (cons "*class definition*" (copy-marker 601))
3171 (cons "c (def)" (copy-marker 626)))))
3172 (python-imenu-create-index)))))
3174 (ert-deftest python-imenu-create-index-2 ()
3175 (python-tests-with-temp-buffer
3177 class Foo(object):
3178 def foo(self):
3179 def foo1():
3180 pass
3182 def foobar(self):
3183 pass
3185 (goto-char (point-max))
3186 (should (equal
3187 (list
3188 (list
3189 "Foo (class)"
3190 (cons "*class definition*" (copy-marker 2))
3191 (list
3192 "foo (def)"
3193 (cons "*function definition*" (copy-marker 21))
3194 (cons "foo1 (def)" (copy-marker 40)))
3195 (cons "foobar (def)" (copy-marker 78))))
3196 (python-imenu-create-index)))))
3198 (ert-deftest python-imenu-create-index-3 ()
3199 (python-tests-with-temp-buffer
3201 class Foo(object):
3202 def foo(self):
3203 def foo1():
3204 pass
3205 def foo2():
3206 pass
3208 (goto-char (point-max))
3209 (should (equal
3210 (list
3211 (list
3212 "Foo (class)"
3213 (cons "*class definition*" (copy-marker 2))
3214 (list
3215 "foo (def)"
3216 (cons "*function definition*" (copy-marker 21))
3217 (cons "foo1 (def)" (copy-marker 40))
3218 (cons "foo2 (def)" (copy-marker 77)))))
3219 (python-imenu-create-index)))))
3221 (ert-deftest python-imenu-create-index-4 ()
3222 (python-tests-with-temp-buffer
3224 class Foo(object):
3225 class Bar(object):
3226 def __init__(self):
3227 pass
3229 def __str__(self):
3230 pass
3232 def __init__(self):
3233 pass
3235 (goto-char (point-max))
3236 (should (equal
3237 (list
3238 (list
3239 "Foo (class)"
3240 (cons "*class definition*" (copy-marker 2))
3241 (list
3242 "Bar (class)"
3243 (cons "*class definition*" (copy-marker 21))
3244 (cons "__init__ (def)" (copy-marker 44))
3245 (cons "__str__ (def)" (copy-marker 90)))
3246 (cons "__init__ (def)" (copy-marker 135))))
3247 (python-imenu-create-index)))))
3249 (ert-deftest python-imenu-create-flat-index-1 ()
3250 (python-tests-with-temp-buffer
3252 class Foo(models.Model):
3253 pass
3256 class Bar(models.Model):
3257 pass
3260 def decorator(arg1, arg2, arg3):
3261 '''print decorated function call data to stdout.
3263 Usage:
3265 @decorator('arg1', 'arg2')
3266 def func(a, b, c=True):
3267 pass
3270 def wrap(f):
3271 print ('wrap')
3272 def wrapped_f(*args):
3273 print ('wrapped_f')
3274 print ('Decorator arguments:', arg1, arg2, arg3)
3275 f(*args)
3276 print ('called f(*args)')
3277 return wrapped_f
3278 return wrap
3281 class Baz(object):
3283 def a(self):
3284 pass
3286 def b(self):
3287 pass
3289 class Frob(object):
3291 def c(self):
3292 pass
3294 (goto-char (point-max))
3295 (should (equal
3296 (list (cons "Foo" (copy-marker 2))
3297 (cons "Bar" (copy-marker 38))
3298 (cons "decorator" (copy-marker 74))
3299 (cons "decorator.wrap" (copy-marker 254))
3300 (cons "decorator.wrap.wrapped_f" (copy-marker 294))
3301 (cons "Baz" (copy-marker 519))
3302 (cons "Baz.a" (copy-marker 539))
3303 (cons "Baz.b" (copy-marker 570))
3304 (cons "Baz.Frob" (copy-marker 601))
3305 (cons "Baz.Frob.c" (copy-marker 626)))
3306 (python-imenu-create-flat-index)))))
3308 (ert-deftest python-imenu-create-flat-index-2 ()
3309 (python-tests-with-temp-buffer
3311 class Foo(object):
3312 class Bar(object):
3313 def __init__(self):
3314 pass
3316 def __str__(self):
3317 pass
3319 def __init__(self):
3320 pass
3322 (goto-char (point-max))
3323 (should (equal
3324 (list
3325 (cons "Foo" (copy-marker 2))
3326 (cons "Foo.Bar" (copy-marker 21))
3327 (cons "Foo.Bar.__init__" (copy-marker 44))
3328 (cons "Foo.Bar.__str__" (copy-marker 90))
3329 (cons "Foo.__init__" (copy-marker 135)))
3330 (python-imenu-create-flat-index)))))
3333 ;;; Misc helpers
3335 (ert-deftest python-info-current-defun-1 ()
3336 (python-tests-with-temp-buffer
3338 def foo(a, b):
3340 (forward-line 1)
3341 (should (string= "foo" (python-info-current-defun)))
3342 (should (string= "def foo" (python-info-current-defun t)))
3343 (forward-line 1)
3344 (should (not (python-info-current-defun)))
3345 (indent-for-tab-command)
3346 (should (string= "foo" (python-info-current-defun)))
3347 (should (string= "def foo" (python-info-current-defun t)))))
3349 (ert-deftest python-info-current-defun-2 ()
3350 (python-tests-with-temp-buffer
3352 class C(object):
3354 def m(self):
3355 if True:
3356 return [i for i in range(3)]
3357 else:
3358 return []
3360 def b():
3361 do_b()
3363 def a():
3364 do_a()
3366 def c(self):
3367 do_c()
3369 (forward-line 1)
3370 (should (string= "C" (python-info-current-defun)))
3371 (should (string= "class C" (python-info-current-defun t)))
3372 (python-tests-look-at "return [i for ")
3373 (should (string= "C.m" (python-info-current-defun)))
3374 (should (string= "def C.m" (python-info-current-defun t)))
3375 (python-tests-look-at "def b():")
3376 (should (string= "C.m.b" (python-info-current-defun)))
3377 (should (string= "def C.m.b" (python-info-current-defun t)))
3378 (forward-line 2)
3379 (indent-for-tab-command)
3380 (python-indent-dedent-line-backspace 1)
3381 (should (string= "C.m" (python-info-current-defun)))
3382 (should (string= "def C.m" (python-info-current-defun t)))
3383 (python-tests-look-at "def c(self):")
3384 (forward-line -1)
3385 (indent-for-tab-command)
3386 (should (string= "C.m.a" (python-info-current-defun)))
3387 (should (string= "def C.m.a" (python-info-current-defun t)))
3388 (python-indent-dedent-line-backspace 1)
3389 (should (string= "C.m" (python-info-current-defun)))
3390 (should (string= "def C.m" (python-info-current-defun t)))
3391 (python-indent-dedent-line-backspace 1)
3392 (should (string= "C" (python-info-current-defun)))
3393 (should (string= "class C" (python-info-current-defun t)))
3394 (python-tests-look-at "def c(self):")
3395 (should (string= "C.c" (python-info-current-defun)))
3396 (should (string= "def C.c" (python-info-current-defun t)))
3397 (python-tests-look-at "do_c()")
3398 (should (string= "C.c" (python-info-current-defun)))
3399 (should (string= "def C.c" (python-info-current-defun t)))))
3401 (ert-deftest python-info-current-defun-3 ()
3402 (python-tests-with-temp-buffer
3404 def decoratorFunctionWithArguments(arg1, arg2, arg3):
3405 '''print decorated function call data to stdout.
3407 Usage:
3409 @decoratorFunctionWithArguments('arg1', 'arg2')
3410 def func(a, b, c=True):
3411 pass
3414 def wwrap(f):
3415 print 'Inside wwrap()'
3416 def wrapped_f(*args):
3417 print 'Inside wrapped_f()'
3418 print 'Decorator arguments:', arg1, arg2, arg3
3419 f(*args)
3420 print 'After f(*args)'
3421 return wrapped_f
3422 return wwrap
3424 (python-tests-look-at "def wwrap(f):")
3425 (forward-line -1)
3426 (should (not (python-info-current-defun)))
3427 (indent-for-tab-command 1)
3428 (should (string= (python-info-current-defun)
3429 "decoratorFunctionWithArguments"))
3430 (should (string= (python-info-current-defun t)
3431 "def decoratorFunctionWithArguments"))
3432 (python-tests-look-at "def wrapped_f(*args):")
3433 (should (string= (python-info-current-defun)
3434 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
3435 (should (string= (python-info-current-defun t)
3436 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
3437 (python-tests-look-at "return wrapped_f")
3438 (should (string= (python-info-current-defun)
3439 "decoratorFunctionWithArguments.wwrap"))
3440 (should (string= (python-info-current-defun t)
3441 "def decoratorFunctionWithArguments.wwrap"))
3442 (end-of-line 1)
3443 (python-tests-look-at "return wwrap")
3444 (should (string= (python-info-current-defun)
3445 "decoratorFunctionWithArguments"))
3446 (should (string= (python-info-current-defun t)
3447 "def decoratorFunctionWithArguments"))))
3449 (ert-deftest python-info-current-symbol-1 ()
3450 (python-tests-with-temp-buffer
3452 class C(object):
3454 def m(self):
3455 self.c()
3457 def c(self):
3458 print ('a')
3460 (python-tests-look-at "self.c()")
3461 (should (string= "self.c" (python-info-current-symbol)))
3462 (should (string= "C.c" (python-info-current-symbol t)))))
3464 (ert-deftest python-info-current-symbol-2 ()
3465 (python-tests-with-temp-buffer
3467 class C(object):
3469 class M(object):
3471 def a(self):
3472 self.c()
3474 def c(self):
3475 pass
3477 (python-tests-look-at "self.c()")
3478 (should (string= "self.c" (python-info-current-symbol)))
3479 (should (string= "C.M.c" (python-info-current-symbol t)))))
3481 (ert-deftest python-info-current-symbol-3 ()
3482 "Keywords should not be considered symbols."
3483 :expected-result :failed
3484 (python-tests-with-temp-buffer
3486 class C(object):
3487 pass
3489 ;; FIXME: keywords are not symbols.
3490 (python-tests-look-at "class C")
3491 (should (not (python-info-current-symbol)))
3492 (should (not (python-info-current-symbol t)))
3493 (python-tests-look-at "C(object)")
3494 (should (string= "C" (python-info-current-symbol)))
3495 (should (string= "class C" (python-info-current-symbol t)))))
3497 (ert-deftest python-info-statement-starts-block-p-1 ()
3498 (python-tests-with-temp-buffer
3500 def long_function_name(
3501 var_one, var_two, var_three,
3502 var_four):
3503 print (var_one)
3505 (python-tests-look-at "def long_function_name")
3506 (should (python-info-statement-starts-block-p))
3507 (python-tests-look-at "print (var_one)")
3508 (python-util-forward-comment -1)
3509 (should (python-info-statement-starts-block-p))))
3511 (ert-deftest python-info-statement-starts-block-p-2 ()
3512 (python-tests-with-temp-buffer
3514 if width == 0 and height == 0 and \\\\
3515 color == 'red' and emphasis == 'strong' or \\\\
3516 highlight > 100:
3517 raise ValueError('sorry, you lose')
3519 (python-tests-look-at "if width == 0 and")
3520 (should (python-info-statement-starts-block-p))
3521 (python-tests-look-at "raise ValueError(")
3522 (python-util-forward-comment -1)
3523 (should (python-info-statement-starts-block-p))))
3525 (ert-deftest python-info-statement-ends-block-p-1 ()
3526 (python-tests-with-temp-buffer
3528 def long_function_name(
3529 var_one, var_two, var_three,
3530 var_four):
3531 print (var_one)
3533 (python-tests-look-at "print (var_one)")
3534 (should (python-info-statement-ends-block-p))))
3536 (ert-deftest python-info-statement-ends-block-p-2 ()
3537 (python-tests-with-temp-buffer
3539 if width == 0 and height == 0 and \\\\
3540 color == 'red' and emphasis == 'strong' or \\\\
3541 highlight > 100:
3542 raise ValueError(
3543 'sorry, you lose'
3547 (python-tests-look-at "raise ValueError(")
3548 (should (python-info-statement-ends-block-p))))
3550 (ert-deftest python-info-beginning-of-statement-p-1 ()
3551 (python-tests-with-temp-buffer
3553 def long_function_name(
3554 var_one, var_two, var_three,
3555 var_four):
3556 print (var_one)
3558 (python-tests-look-at "def long_function_name")
3559 (should (python-info-beginning-of-statement-p))
3560 (forward-char 10)
3561 (should (not (python-info-beginning-of-statement-p)))
3562 (python-tests-look-at "print (var_one)")
3563 (should (python-info-beginning-of-statement-p))
3564 (goto-char (line-beginning-position))
3565 (should (not (python-info-beginning-of-statement-p)))))
3567 (ert-deftest python-info-beginning-of-statement-p-2 ()
3568 (python-tests-with-temp-buffer
3570 if width == 0 and height == 0 and \\\\
3571 color == 'red' and emphasis == 'strong' or \\\\
3572 highlight > 100:
3573 raise ValueError(
3574 'sorry, you lose'
3578 (python-tests-look-at "if width == 0 and")
3579 (should (python-info-beginning-of-statement-p))
3580 (forward-char 10)
3581 (should (not (python-info-beginning-of-statement-p)))
3582 (python-tests-look-at "raise ValueError(")
3583 (should (python-info-beginning-of-statement-p))
3584 (goto-char (line-beginning-position))
3585 (should (not (python-info-beginning-of-statement-p)))))
3587 (ert-deftest python-info-end-of-statement-p-1 ()
3588 (python-tests-with-temp-buffer
3590 def long_function_name(
3591 var_one, var_two, var_three,
3592 var_four):
3593 print (var_one)
3595 (python-tests-look-at "def long_function_name")
3596 (should (not (python-info-end-of-statement-p)))
3597 (end-of-line)
3598 (should (not (python-info-end-of-statement-p)))
3599 (python-tests-look-at "print (var_one)")
3600 (python-util-forward-comment -1)
3601 (should (python-info-end-of-statement-p))
3602 (python-tests-look-at "print (var_one)")
3603 (should (not (python-info-end-of-statement-p)))
3604 (end-of-line)
3605 (should (python-info-end-of-statement-p))))
3607 (ert-deftest python-info-end-of-statement-p-2 ()
3608 (python-tests-with-temp-buffer
3610 if width == 0 and height == 0 and \\\\
3611 color == 'red' and emphasis == 'strong' or \\\\
3612 highlight > 100:
3613 raise ValueError(
3614 'sorry, you lose'
3618 (python-tests-look-at "if width == 0 and")
3619 (should (not (python-info-end-of-statement-p)))
3620 (end-of-line)
3621 (should (not (python-info-end-of-statement-p)))
3622 (python-tests-look-at "raise ValueError(")
3623 (python-util-forward-comment -1)
3624 (should (python-info-end-of-statement-p))
3625 (python-tests-look-at "raise ValueError(")
3626 (should (not (python-info-end-of-statement-p)))
3627 (end-of-line)
3628 (should (not (python-info-end-of-statement-p)))
3629 (goto-char (point-max))
3630 (python-util-forward-comment -1)
3631 (should (python-info-end-of-statement-p))))
3633 (ert-deftest python-info-beginning-of-block-p-1 ()
3634 (python-tests-with-temp-buffer
3636 def long_function_name(
3637 var_one, var_two, var_three,
3638 var_four):
3639 print (var_one)
3641 (python-tests-look-at "def long_function_name")
3642 (should (python-info-beginning-of-block-p))
3643 (python-tests-look-at "var_one, var_two, var_three,")
3644 (should (not (python-info-beginning-of-block-p)))
3645 (python-tests-look-at "print (var_one)")
3646 (should (not (python-info-beginning-of-block-p)))))
3648 (ert-deftest python-info-beginning-of-block-p-2 ()
3649 (python-tests-with-temp-buffer
3651 if width == 0 and height == 0 and \\\\
3652 color == 'red' and emphasis == 'strong' or \\\\
3653 highlight > 100:
3654 raise ValueError(
3655 'sorry, you lose'
3659 (python-tests-look-at "if width == 0 and")
3660 (should (python-info-beginning-of-block-p))
3661 (python-tests-look-at "color == 'red' and emphasis")
3662 (should (not (python-info-beginning-of-block-p)))
3663 (python-tests-look-at "raise ValueError(")
3664 (should (not (python-info-beginning-of-block-p)))))
3666 (ert-deftest python-info-end-of-block-p-1 ()
3667 (python-tests-with-temp-buffer
3669 def long_function_name(
3670 var_one, var_two, var_three,
3671 var_four):
3672 print (var_one)
3674 (python-tests-look-at "def long_function_name")
3675 (should (not (python-info-end-of-block-p)))
3676 (python-tests-look-at "var_one, var_two, var_three,")
3677 (should (not (python-info-end-of-block-p)))
3678 (python-tests-look-at "var_four):")
3679 (end-of-line)
3680 (should (not (python-info-end-of-block-p)))
3681 (python-tests-look-at "print (var_one)")
3682 (should (not (python-info-end-of-block-p)))
3683 (end-of-line 1)
3684 (should (python-info-end-of-block-p))))
3686 (ert-deftest python-info-end-of-block-p-2 ()
3687 (python-tests-with-temp-buffer
3689 if width == 0 and height == 0 and \\\\
3690 color == 'red' and emphasis == 'strong' or \\\\
3691 highlight > 100:
3692 raise ValueError(
3693 'sorry, you lose'
3697 (python-tests-look-at "if width == 0 and")
3698 (should (not (python-info-end-of-block-p)))
3699 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3700 (should (not (python-info-end-of-block-p)))
3701 (python-tests-look-at "highlight > 100:")
3702 (end-of-line)
3703 (should (not (python-info-end-of-block-p)))
3704 (python-tests-look-at "raise ValueError(")
3705 (should (not (python-info-end-of-block-p)))
3706 (end-of-line 1)
3707 (should (not (python-info-end-of-block-p)))
3708 (goto-char (point-max))
3709 (python-util-forward-comment -1)
3710 (should (python-info-end-of-block-p))))
3712 (ert-deftest python-info-dedenter-opening-block-position-1 ()
3713 (python-tests-with-temp-buffer
3715 if request.user.is_authenticated():
3716 try:
3717 profile = request.user.get_profile()
3718 except Profile.DoesNotExist:
3719 profile = Profile.objects.create(user=request.user)
3720 else:
3721 if profile.stats:
3722 profile.recalculate_stats()
3723 else:
3724 profile.clear_stats()
3725 finally:
3726 profile.views += 1
3727 profile.save()
3729 (python-tests-look-at "try:")
3730 (should (not (python-info-dedenter-opening-block-position)))
3731 (python-tests-look-at "except Profile.DoesNotExist:")
3732 (should (= (python-tests-look-at "try:" -1 t)
3733 (python-info-dedenter-opening-block-position)))
3734 (python-tests-look-at "else:")
3735 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -1 t)
3736 (python-info-dedenter-opening-block-position)))
3737 (python-tests-look-at "if profile.stats:")
3738 (should (not (python-info-dedenter-opening-block-position)))
3739 (python-tests-look-at "else:")
3740 (should (= (python-tests-look-at "if profile.stats:" -1 t)
3741 (python-info-dedenter-opening-block-position)))
3742 (python-tests-look-at "finally:")
3743 (should (= (python-tests-look-at "else:" -2 t)
3744 (python-info-dedenter-opening-block-position)))))
3746 (ert-deftest python-info-dedenter-opening-block-position-2 ()
3747 (python-tests-with-temp-buffer
3749 if request.user.is_authenticated():
3750 profile = Profile.objects.get_or_create(user=request.user)
3751 if profile.stats:
3752 profile.recalculate_stats()
3754 data = {
3755 'else': 'do it'
3757 'else'
3759 (python-tests-look-at "'else': 'do it'")
3760 (should (not (python-info-dedenter-opening-block-position)))
3761 (python-tests-look-at "'else'")
3762 (should (not (python-info-dedenter-opening-block-position)))))
3764 (ert-deftest python-info-dedenter-opening-block-position-3 ()
3765 (python-tests-with-temp-buffer
3767 if save:
3768 try:
3769 write_to_disk(data)
3770 except IOError:
3771 msg = 'Error saving to disk'
3772 message(msg)
3773 logger.exception(msg)
3774 except Exception:
3775 if hide_details:
3776 logger.exception('Unhandled exception')
3777 else
3778 finally:
3779 data.free()
3781 (python-tests-look-at "try:")
3782 (should (not (python-info-dedenter-opening-block-position)))
3784 (python-tests-look-at "except IOError:")
3785 (should (= (python-tests-look-at "try:" -1 t)
3786 (python-info-dedenter-opening-block-position)))
3788 (python-tests-look-at "except Exception:")
3789 (should (= (python-tests-look-at "except IOError:" -1 t)
3790 (python-info-dedenter-opening-block-position)))
3792 (python-tests-look-at "if hide_details:")
3793 (should (not (python-info-dedenter-opening-block-position)))
3795 ;; check indentation modifies the detected opening block
3796 (python-tests-look-at "else")
3797 (should (= (python-tests-look-at "if hide_details:" -1 t)
3798 (python-info-dedenter-opening-block-position)))
3800 (indent-line-to 8)
3801 (should (= (python-tests-look-at "if hide_details:" -1 t)
3802 (python-info-dedenter-opening-block-position)))
3804 (indent-line-to 4)
3805 (should (= (python-tests-look-at "except Exception:" -1 t)
3806 (python-info-dedenter-opening-block-position)))
3808 (indent-line-to 0)
3809 (should (= (python-tests-look-at "if save:" -1 t)
3810 (python-info-dedenter-opening-block-position)))))
3812 (ert-deftest python-info-dedenter-opening-block-positions-1 ()
3813 (python-tests-with-temp-buffer
3815 if save:
3816 try:
3817 write_to_disk(data)
3818 except IOError:
3819 msg = 'Error saving to disk'
3820 message(msg)
3821 logger.exception(msg)
3822 except Exception:
3823 if hide_details:
3824 logger.exception('Unhandled exception')
3825 else
3826 finally:
3827 data.free()
3829 (python-tests-look-at "try:")
3830 (should (not (python-info-dedenter-opening-block-positions)))
3832 (python-tests-look-at "except IOError:")
3833 (should
3834 (equal (list
3835 (python-tests-look-at "try:" -1 t))
3836 (python-info-dedenter-opening-block-positions)))
3838 (python-tests-look-at "except Exception:")
3839 (should
3840 (equal (list
3841 (python-tests-look-at "except IOError:" -1 t))
3842 (python-info-dedenter-opening-block-positions)))
3844 (python-tests-look-at "if hide_details:")
3845 (should (not (python-info-dedenter-opening-block-positions)))
3847 ;; check indentation does not modify the detected opening blocks
3848 (python-tests-look-at "else")
3849 (should
3850 (equal (list
3851 (python-tests-look-at "if hide_details:" -1 t)
3852 (python-tests-look-at "except Exception:" -1 t)
3853 (python-tests-look-at "if save:" -1 t))
3854 (python-info-dedenter-opening-block-positions)))
3856 (indent-line-to 8)
3857 (should
3858 (equal (list
3859 (python-tests-look-at "if hide_details:" -1 t)
3860 (python-tests-look-at "except Exception:" -1 t)
3861 (python-tests-look-at "if save:" -1 t))
3862 (python-info-dedenter-opening-block-positions)))
3864 (indent-line-to 4)
3865 (should
3866 (equal (list
3867 (python-tests-look-at "if hide_details:" -1 t)
3868 (python-tests-look-at "except Exception:" -1 t)
3869 (python-tests-look-at "if save:" -1 t))
3870 (python-info-dedenter-opening-block-positions)))
3872 (indent-line-to 0)
3873 (should
3874 (equal (list
3875 (python-tests-look-at "if hide_details:" -1 t)
3876 (python-tests-look-at "except Exception:" -1 t)
3877 (python-tests-look-at "if save:" -1 t))
3878 (python-info-dedenter-opening-block-positions)))))
3880 (ert-deftest python-info-dedenter-opening-block-positions-2 ()
3881 "Test detection of opening blocks for elif."
3882 (python-tests-with-temp-buffer
3884 if var:
3885 if var2:
3886 something()
3887 elif var3:
3888 something_else()
3889 elif
3891 (python-tests-look-at "elif var3:")
3892 (should
3893 (equal (list
3894 (python-tests-look-at "if var2:" -1 t)
3895 (python-tests-look-at "if var:" -1 t))
3896 (python-info-dedenter-opening-block-positions)))
3898 (python-tests-look-at "elif\n")
3899 (should
3900 (equal (list
3901 (python-tests-look-at "elif var3:" -1 t)
3902 (python-tests-look-at "if var:" -1 t))
3903 (python-info-dedenter-opening-block-positions)))))
3905 (ert-deftest python-info-dedenter-opening-block-positions-3 ()
3906 "Test detection of opening blocks for else."
3907 (python-tests-with-temp-buffer
3909 try:
3910 something()
3911 except:
3912 if var:
3913 if var2:
3914 something()
3915 elif var3:
3916 something_else()
3917 else
3919 if var4:
3920 while var5:
3921 var4.pop()
3922 else
3924 for value in var6:
3925 if value > 0:
3926 print value
3927 else
3929 (python-tests-look-at "else\n")
3930 (should
3931 (equal (list
3932 (python-tests-look-at "elif var3:" -1 t)
3933 (python-tests-look-at "if var:" -1 t)
3934 (python-tests-look-at "except:" -1 t))
3935 (python-info-dedenter-opening-block-positions)))
3937 (python-tests-look-at "else\n")
3938 (should
3939 (equal (list
3940 (python-tests-look-at "while var5:" -1 t)
3941 (python-tests-look-at "if var4:" -1 t))
3942 (python-info-dedenter-opening-block-positions)))
3944 (python-tests-look-at "else\n")
3945 (should
3946 (equal (list
3947 (python-tests-look-at "if value > 0:" -1 t)
3948 (python-tests-look-at "for value in var6:" -1 t)
3949 (python-tests-look-at "if var4:" -1 t))
3950 (python-info-dedenter-opening-block-positions)))))
3952 (ert-deftest python-info-dedenter-opening-block-positions-4 ()
3953 "Test detection of opening blocks for except."
3954 (python-tests-with-temp-buffer
3956 try:
3957 something()
3958 except ValueError:
3959 something_else()
3960 except
3962 (python-tests-look-at "except ValueError:")
3963 (should
3964 (equal (list (python-tests-look-at "try:" -1 t))
3965 (python-info-dedenter-opening-block-positions)))
3967 (python-tests-look-at "except\n")
3968 (should
3969 (equal (list (python-tests-look-at "except ValueError:" -1 t))
3970 (python-info-dedenter-opening-block-positions)))))
3972 (ert-deftest python-info-dedenter-opening-block-positions-5 ()
3973 "Test detection of opening blocks for finally."
3974 (python-tests-with-temp-buffer
3976 try:
3977 something()
3978 finally
3980 try:
3981 something_else()
3982 except:
3983 logger.exception('something went wrong')
3984 finally
3986 try:
3987 something_else_else()
3988 except Exception:
3989 logger.exception('something else went wrong')
3990 else:
3991 print ('all good')
3992 finally
3994 (python-tests-look-at "finally\n")
3995 (should
3996 (equal (list (python-tests-look-at "try:" -1 t))
3997 (python-info-dedenter-opening-block-positions)))
3999 (python-tests-look-at "finally\n")
4000 (should
4001 (equal (list (python-tests-look-at "except:" -1 t))
4002 (python-info-dedenter-opening-block-positions)))
4004 (python-tests-look-at "finally\n")
4005 (should
4006 (equal (list (python-tests-look-at "else:" -1 t))
4007 (python-info-dedenter-opening-block-positions)))))
4009 (ert-deftest python-info-dedenter-opening-block-message-1 ()
4010 "Test dedenters inside strings are ignored."
4011 (python-tests-with-temp-buffer
4012 "'''
4013 try:
4014 something()
4015 except:
4016 logger.exception('something went wrong')
4019 (python-tests-look-at "except\n")
4020 (should (not (python-info-dedenter-opening-block-message)))))
4022 (ert-deftest python-info-dedenter-opening-block-message-2 ()
4023 "Test except keyword."
4024 (python-tests-with-temp-buffer
4026 try:
4027 something()
4028 except:
4029 logger.exception('something went wrong')
4031 (python-tests-look-at "except:")
4032 (should (string=
4033 "Closes try:"
4034 (substring-no-properties
4035 (python-info-dedenter-opening-block-message))))
4036 (end-of-line)
4037 (should (string=
4038 "Closes try:"
4039 (substring-no-properties
4040 (python-info-dedenter-opening-block-message))))))
4042 (ert-deftest python-info-dedenter-opening-block-message-3 ()
4043 "Test else keyword."
4044 (python-tests-with-temp-buffer
4046 try:
4047 something()
4048 except:
4049 logger.exception('something went wrong')
4050 else:
4051 logger.debug('all good')
4053 (python-tests-look-at "else:")
4054 (should (string=
4055 "Closes except:"
4056 (substring-no-properties
4057 (python-info-dedenter-opening-block-message))))
4058 (end-of-line)
4059 (should (string=
4060 "Closes except:"
4061 (substring-no-properties
4062 (python-info-dedenter-opening-block-message))))))
4064 (ert-deftest python-info-dedenter-opening-block-message-4 ()
4065 "Test finally keyword."
4066 (python-tests-with-temp-buffer
4068 try:
4069 something()
4070 except:
4071 logger.exception('something went wrong')
4072 else:
4073 logger.debug('all good')
4074 finally:
4075 clean()
4077 (python-tests-look-at "finally:")
4078 (should (string=
4079 "Closes else:"
4080 (substring-no-properties
4081 (python-info-dedenter-opening-block-message))))
4082 (end-of-line)
4083 (should (string=
4084 "Closes else:"
4085 (substring-no-properties
4086 (python-info-dedenter-opening-block-message))))))
4088 (ert-deftest python-info-dedenter-opening-block-message-5 ()
4089 "Test elif keyword."
4090 (python-tests-with-temp-buffer
4092 if a:
4093 something()
4094 elif b:
4096 (python-tests-look-at "elif b:")
4097 (should (string=
4098 "Closes if a:"
4099 (substring-no-properties
4100 (python-info-dedenter-opening-block-message))))
4101 (end-of-line)
4102 (should (string=
4103 "Closes if a:"
4104 (substring-no-properties
4105 (python-info-dedenter-opening-block-message))))))
4108 (ert-deftest python-info-dedenter-statement-p-1 ()
4109 "Test dedenters inside strings are ignored."
4110 (python-tests-with-temp-buffer
4111 "'''
4112 try:
4113 something()
4114 except:
4115 logger.exception('something went wrong')
4118 (python-tests-look-at "except\n")
4119 (should (not (python-info-dedenter-statement-p)))))
4121 (ert-deftest python-info-dedenter-statement-p-2 ()
4122 "Test except keyword."
4123 (python-tests-with-temp-buffer
4125 try:
4126 something()
4127 except:
4128 logger.exception('something went wrong')
4130 (python-tests-look-at "except:")
4131 (should (= (point) (python-info-dedenter-statement-p)))
4132 (end-of-line)
4133 (should (= (save-excursion
4134 (back-to-indentation)
4135 (point))
4136 (python-info-dedenter-statement-p)))))
4138 (ert-deftest python-info-dedenter-statement-p-3 ()
4139 "Test else keyword."
4140 (python-tests-with-temp-buffer
4142 try:
4143 something()
4144 except:
4145 logger.exception('something went wrong')
4146 else:
4147 logger.debug('all good')
4149 (python-tests-look-at "else:")
4150 (should (= (point) (python-info-dedenter-statement-p)))
4151 (end-of-line)
4152 (should (= (save-excursion
4153 (back-to-indentation)
4154 (point))
4155 (python-info-dedenter-statement-p)))))
4157 (ert-deftest python-info-dedenter-statement-p-4 ()
4158 "Test finally keyword."
4159 (python-tests-with-temp-buffer
4161 try:
4162 something()
4163 except:
4164 logger.exception('something went wrong')
4165 else:
4166 logger.debug('all good')
4167 finally:
4168 clean()
4170 (python-tests-look-at "finally:")
4171 (should (= (point) (python-info-dedenter-statement-p)))
4172 (end-of-line)
4173 (should (= (save-excursion
4174 (back-to-indentation)
4175 (point))
4176 (python-info-dedenter-statement-p)))))
4178 (ert-deftest python-info-dedenter-statement-p-5 ()
4179 "Test elif keyword."
4180 (python-tests-with-temp-buffer
4182 if a:
4183 something()
4184 elif b:
4186 (python-tests-look-at "elif b:")
4187 (should (= (point) (python-info-dedenter-statement-p)))
4188 (end-of-line)
4189 (should (= (save-excursion
4190 (back-to-indentation)
4191 (point))
4192 (python-info-dedenter-statement-p)))))
4194 (ert-deftest python-info-line-ends-backslash-p-1 ()
4195 (python-tests-with-temp-buffer
4197 objects = Thing.objects.all() \\\\
4198 .filter(
4199 type='toy',
4200 status='bought'
4201 ) \\\\
4202 .aggregate(
4203 Sum('amount')
4204 ) \\\\
4205 .values_list()
4207 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
4208 (should (python-info-line-ends-backslash-p 3))
4209 (should (python-info-line-ends-backslash-p 4))
4210 (should (python-info-line-ends-backslash-p 5))
4211 (should (python-info-line-ends-backslash-p 6)) ; ) \...
4212 (should (python-info-line-ends-backslash-p 7))
4213 (should (python-info-line-ends-backslash-p 8))
4214 (should (python-info-line-ends-backslash-p 9))
4215 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
4217 (ert-deftest python-info-beginning-of-backslash-1 ()
4218 (python-tests-with-temp-buffer
4220 objects = Thing.objects.all() \\\\
4221 .filter(
4222 type='toy',
4223 status='bought'
4224 ) \\\\
4225 .aggregate(
4226 Sum('amount')
4227 ) \\\\
4228 .values_list()
4230 (let ((first 2)
4231 (second (python-tests-look-at ".filter("))
4232 (third (python-tests-look-at ".aggregate(")))
4233 (should (= first (python-info-beginning-of-backslash 2)))
4234 (should (= second (python-info-beginning-of-backslash 3)))
4235 (should (= second (python-info-beginning-of-backslash 4)))
4236 (should (= second (python-info-beginning-of-backslash 5)))
4237 (should (= second (python-info-beginning-of-backslash 6)))
4238 (should (= third (python-info-beginning-of-backslash 7)))
4239 (should (= third (python-info-beginning-of-backslash 8)))
4240 (should (= third (python-info-beginning-of-backslash 9)))
4241 (should (not (python-info-beginning-of-backslash 10))))))
4243 (ert-deftest python-info-continuation-line-p-1 ()
4244 (python-tests-with-temp-buffer
4246 if width == 0 and height == 0 and \\\\
4247 color == 'red' and emphasis == 'strong' or \\\\
4248 highlight > 100:
4249 raise ValueError(
4250 'sorry, you lose'
4254 (python-tests-look-at "if width == 0 and height == 0 and")
4255 (should (not (python-info-continuation-line-p)))
4256 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
4257 (should (python-info-continuation-line-p))
4258 (python-tests-look-at "highlight > 100:")
4259 (should (python-info-continuation-line-p))
4260 (python-tests-look-at "raise ValueError(")
4261 (should (not (python-info-continuation-line-p)))
4262 (python-tests-look-at "'sorry, you lose'")
4263 (should (python-info-continuation-line-p))
4264 (forward-line 1)
4265 (should (python-info-continuation-line-p))
4266 (python-tests-look-at ")")
4267 (should (python-info-continuation-line-p))
4268 (forward-line 1)
4269 (should (not (python-info-continuation-line-p)))))
4271 (ert-deftest python-info-block-continuation-line-p-1 ()
4272 (python-tests-with-temp-buffer
4274 if width == 0 and height == 0 and \\\\
4275 color == 'red' and emphasis == 'strong' or \\\\
4276 highlight > 100:
4277 raise ValueError(
4278 'sorry, you lose'
4282 (python-tests-look-at "if width == 0 and")
4283 (should (not (python-info-block-continuation-line-p)))
4284 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
4285 (should (= (python-info-block-continuation-line-p)
4286 (python-tests-look-at "if width == 0 and" -1 t)))
4287 (python-tests-look-at "highlight > 100:")
4288 (should (not (python-info-block-continuation-line-p)))))
4290 (ert-deftest python-info-block-continuation-line-p-2 ()
4291 (python-tests-with-temp-buffer
4293 def foo(a,
4296 pass
4298 (python-tests-look-at "def foo(a,")
4299 (should (not (python-info-block-continuation-line-p)))
4300 (python-tests-look-at "b,")
4301 (should (= (python-info-block-continuation-line-p)
4302 (python-tests-look-at "def foo(a," -1 t)))
4303 (python-tests-look-at "c):")
4304 (should (not (python-info-block-continuation-line-p)))))
4306 (ert-deftest python-info-assignment-statement-p-1 ()
4307 (python-tests-with-temp-buffer
4309 data = foo(), bar() \\\\
4310 baz(), 4 \\\\
4311 5, 6
4313 (python-tests-look-at "data = foo(), bar()")
4314 (should (python-info-assignment-statement-p))
4315 (should (python-info-assignment-statement-p t))
4316 (python-tests-look-at "baz(), 4")
4317 (should (python-info-assignment-statement-p))
4318 (should (not (python-info-assignment-statement-p t)))
4319 (python-tests-look-at "5, 6")
4320 (should (python-info-assignment-statement-p))
4321 (should (not (python-info-assignment-statement-p t)))))
4323 (ert-deftest python-info-assignment-statement-p-2 ()
4324 (python-tests-with-temp-buffer
4326 data = (foo(), bar()
4327 baz(), 4
4328 5, 6)
4330 (python-tests-look-at "data = (foo(), bar()")
4331 (should (python-info-assignment-statement-p))
4332 (should (python-info-assignment-statement-p t))
4333 (python-tests-look-at "baz(), 4")
4334 (should (python-info-assignment-statement-p))
4335 (should (not (python-info-assignment-statement-p t)))
4336 (python-tests-look-at "5, 6)")
4337 (should (python-info-assignment-statement-p))
4338 (should (not (python-info-assignment-statement-p t)))))
4340 (ert-deftest python-info-assignment-statement-p-3 ()
4341 (python-tests-with-temp-buffer
4343 data '=' 42
4345 (python-tests-look-at "data '=' 42")
4346 (should (not (python-info-assignment-statement-p)))
4347 (should (not (python-info-assignment-statement-p t)))))
4349 (ert-deftest python-info-assignment-continuation-line-p-1 ()
4350 (python-tests-with-temp-buffer
4352 data = foo(), bar() \\\\
4353 baz(), 4 \\\\
4354 5, 6
4356 (python-tests-look-at "data = foo(), bar()")
4357 (should (not (python-info-assignment-continuation-line-p)))
4358 (python-tests-look-at "baz(), 4")
4359 (should (= (python-info-assignment-continuation-line-p)
4360 (python-tests-look-at "foo()," -1 t)))
4361 (python-tests-look-at "5, 6")
4362 (should (not (python-info-assignment-continuation-line-p)))))
4364 (ert-deftest python-info-assignment-continuation-line-p-2 ()
4365 (python-tests-with-temp-buffer
4367 data = (foo(), bar()
4368 baz(), 4
4369 5, 6)
4371 (python-tests-look-at "data = (foo(), bar()")
4372 (should (not (python-info-assignment-continuation-line-p)))
4373 (python-tests-look-at "baz(), 4")
4374 (should (= (python-info-assignment-continuation-line-p)
4375 (python-tests-look-at "(foo()," -1 t)))
4376 (python-tests-look-at "5, 6)")
4377 (should (not (python-info-assignment-continuation-line-p)))))
4379 (ert-deftest python-info-looking-at-beginning-of-defun-1 ()
4380 (python-tests-with-temp-buffer
4382 def decorat0r(deff):
4383 '''decorates stuff.
4385 @decorat0r
4386 def foo(arg):
4389 def wrap():
4390 deff()
4391 return wwrap
4393 (python-tests-look-at "def decorat0r(deff):")
4394 (should (python-info-looking-at-beginning-of-defun))
4395 (python-tests-look-at "def foo(arg):")
4396 (should (not (python-info-looking-at-beginning-of-defun)))
4397 (python-tests-look-at "def wrap():")
4398 (should (python-info-looking-at-beginning-of-defun))
4399 (python-tests-look-at "deff()")
4400 (should (not (python-info-looking-at-beginning-of-defun)))))
4402 (ert-deftest python-info-current-line-comment-p-1 ()
4403 (python-tests-with-temp-buffer
4405 # this is a comment
4406 foo = True # another comment
4407 '#this is a string'
4408 if foo:
4409 # more comments
4410 print ('bar') # print bar
4412 (python-tests-look-at "# this is a comment")
4413 (should (python-info-current-line-comment-p))
4414 (python-tests-look-at "foo = True # another comment")
4415 (should (not (python-info-current-line-comment-p)))
4416 (python-tests-look-at "'#this is a string'")
4417 (should (not (python-info-current-line-comment-p)))
4418 (python-tests-look-at "# more comments")
4419 (should (python-info-current-line-comment-p))
4420 (python-tests-look-at "print ('bar') # print bar")
4421 (should (not (python-info-current-line-comment-p)))))
4423 (ert-deftest python-info-current-line-empty-p ()
4424 (python-tests-with-temp-buffer
4426 # this is a comment
4428 foo = True # another comment
4430 (should (python-info-current-line-empty-p))
4431 (python-tests-look-at "# this is a comment")
4432 (should (not (python-info-current-line-empty-p)))
4433 (forward-line 1)
4434 (should (python-info-current-line-empty-p))))
4436 (ert-deftest python-info-docstring-p-1 ()
4437 "Test module docstring detection."
4438 (python-tests-with-temp-buffer
4439 "# -*- coding: utf-8 -*-
4440 #!/usr/bin/python
4443 Module Docstring Django style.
4445 u'''Additional module docstring.'''
4446 '''Not a module docstring.'''
4448 (python-tests-look-at "Module Docstring Django style.")
4449 (should (python-info-docstring-p))
4450 (python-tests-look-at "u'''Additional module docstring.'''")
4451 (should (python-info-docstring-p))
4452 (python-tests-look-at "'''Not a module docstring.'''")
4453 (should (not (python-info-docstring-p)))))
4455 (ert-deftest python-info-docstring-p-2 ()
4456 "Test variable docstring detection."
4457 (python-tests-with-temp-buffer
4459 variable = 42
4460 U'''Variable docstring.'''
4461 '''Additional variable docstring.'''
4462 '''Not a variable docstring.'''
4464 (python-tests-look-at "Variable docstring.")
4465 (should (python-info-docstring-p))
4466 (python-tests-look-at "u'''Additional variable docstring.'''")
4467 (should (python-info-docstring-p))
4468 (python-tests-look-at "'''Not a variable docstring.'''")
4469 (should (not (python-info-docstring-p)))))
4471 (ert-deftest python-info-docstring-p-3 ()
4472 "Test function docstring detection."
4473 (python-tests-with-temp-buffer
4475 def func(a, b):
4476 r'''
4477 Function docstring.
4479 onetwo style.
4481 R'''Additional function docstring.'''
4482 '''Not a function docstring.'''
4483 return a + b
4485 (python-tests-look-at "Function docstring.")
4486 (should (python-info-docstring-p))
4487 (python-tests-look-at "R'''Additional function docstring.'''")
4488 (should (python-info-docstring-p))
4489 (python-tests-look-at "'''Not a function docstring.'''")
4490 (should (not (python-info-docstring-p)))))
4492 (ert-deftest python-info-docstring-p-4 ()
4493 "Test class docstring detection."
4494 (python-tests-with-temp-buffer
4496 class Class:
4497 ur'''
4498 Class docstring.
4500 symmetric style.
4502 uR'''
4503 Additional class docstring.
4505 '''Not a class docstring.'''
4506 pass
4508 (python-tests-look-at "Class docstring.")
4509 (should (python-info-docstring-p))
4510 (python-tests-look-at "uR'''") ;; Additional class docstring
4511 (should (python-info-docstring-p))
4512 (python-tests-look-at "'''Not a class docstring.'''")
4513 (should (not (python-info-docstring-p)))))
4515 (ert-deftest python-info-docstring-p-5 ()
4516 "Test class attribute docstring detection."
4517 (python-tests-with-temp-buffer
4519 class Class:
4520 attribute = 42
4521 Ur'''
4522 Class attribute docstring.
4524 pep-257 style.
4527 UR'''
4528 Additional class attribute docstring.
4530 '''Not a class attribute docstring.'''
4531 pass
4533 (python-tests-look-at "Class attribute docstring.")
4534 (should (python-info-docstring-p))
4535 (python-tests-look-at "UR'''") ;; Additional class attr docstring
4536 (should (python-info-docstring-p))
4537 (python-tests-look-at "'''Not a class attribute docstring.'''")
4538 (should (not (python-info-docstring-p)))))
4540 (ert-deftest python-info-docstring-p-6 ()
4541 "Test class method docstring detection."
4542 (python-tests-with-temp-buffer
4544 class Class:
4546 def __init__(self, a, b):
4547 self.a = a
4548 self.b = b
4550 def __call__(self):
4551 '''Method docstring.
4553 pep-257-nn style.
4555 '''Additional method docstring.'''
4556 '''Not a method docstring.'''
4557 return self.a + self.b
4559 (python-tests-look-at "Method docstring.")
4560 (should (python-info-docstring-p))
4561 (python-tests-look-at "'''Additional method docstring.'''")
4562 (should (python-info-docstring-p))
4563 (python-tests-look-at "'''Not a method docstring.'''")
4564 (should (not (python-info-docstring-p)))))
4566 (ert-deftest python-info-encoding-from-cookie-1 ()
4567 "Should detect it on first line."
4568 (python-tests-with-temp-buffer
4569 "# coding=latin-1
4571 foo = True # another comment
4573 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4575 (ert-deftest python-info-encoding-from-cookie-2 ()
4576 "Should detect it on second line."
4577 (python-tests-with-temp-buffer
4579 # coding=latin-1
4581 foo = True # another comment
4583 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4585 (ert-deftest python-info-encoding-from-cookie-3 ()
4586 "Should not be detected on third line (and following ones)."
4587 (python-tests-with-temp-buffer
4590 # coding=latin-1
4591 foo = True # another comment
4593 (should (not (python-info-encoding-from-cookie)))))
4595 (ert-deftest python-info-encoding-from-cookie-4 ()
4596 "Should detect Emacs style."
4597 (python-tests-with-temp-buffer
4598 "# -*- coding: latin-1 -*-
4600 foo = True # another comment"
4601 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4603 (ert-deftest python-info-encoding-from-cookie-5 ()
4604 "Should detect Vim style."
4605 (python-tests-with-temp-buffer
4606 "# vim: set fileencoding=latin-1 :
4608 foo = True # another comment"
4609 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4611 (ert-deftest python-info-encoding-from-cookie-6 ()
4612 "First cookie wins."
4613 (python-tests-with-temp-buffer
4614 "# -*- coding: iso-8859-1 -*-
4615 # vim: set fileencoding=latin-1 :
4617 foo = True # another comment"
4618 (should (eq (python-info-encoding-from-cookie) 'iso-8859-1))))
4620 (ert-deftest python-info-encoding-from-cookie-7 ()
4621 "First cookie wins."
4622 (python-tests-with-temp-buffer
4623 "# vim: set fileencoding=latin-1 :
4624 # -*- coding: iso-8859-1 -*-
4626 foo = True # another comment"
4627 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4629 (ert-deftest python-info-encoding-1 ()
4630 "Should return the detected encoding from cookie."
4631 (python-tests-with-temp-buffer
4632 "# vim: set fileencoding=latin-1 :
4634 foo = True # another comment"
4635 (should (eq (python-info-encoding) 'latin-1))))
4637 (ert-deftest python-info-encoding-2 ()
4638 "Should default to utf-8."
4639 (python-tests-with-temp-buffer
4640 "# No encoding for you
4642 foo = True # another comment"
4643 (should (eq (python-info-encoding) 'utf-8))))
4646 ;;; Utility functions
4648 (ert-deftest python-util-goto-line-1 ()
4649 (python-tests-with-temp-buffer
4650 (concat
4651 "# a comment
4652 # another comment
4653 def foo(a, b, c):
4654 pass" (make-string 20 ?\n))
4655 (python-util-goto-line 10)
4656 (should (= (line-number-at-pos) 10))
4657 (python-util-goto-line 20)
4658 (should (= (line-number-at-pos) 20))))
4660 (ert-deftest python-util-clone-local-variables-1 ()
4661 (let ((buffer (generate-new-buffer
4662 "python-util-clone-local-variables-1"))
4663 (varcons
4664 '((python-fill-docstring-style . django)
4665 (python-shell-interpreter . "python")
4666 (python-shell-interpreter-args . "manage.py shell")
4667 (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
4668 (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
4669 (python-shell-extra-pythonpaths "/home/user/pylib/")
4670 (python-shell-completion-setup-code
4671 . "from IPython.core.completerlib import module_completion")
4672 (python-shell-completion-string-code
4673 . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
4674 (python-shell-virtualenv-root
4675 . "/home/user/.virtualenvs/project"))))
4676 (with-current-buffer buffer
4677 (kill-all-local-variables)
4678 (dolist (ccons varcons)
4679 (set (make-local-variable (car ccons)) (cdr ccons))))
4680 (python-tests-with-temp-buffer
4682 (python-util-clone-local-variables buffer)
4683 (dolist (ccons varcons)
4684 (should
4685 (equal (symbol-value (car ccons)) (cdr ccons)))))
4686 (kill-buffer buffer)))
4688 (ert-deftest python-util-strip-string-1 ()
4689 (should (string= (python-util-strip-string "\t\r\n str") "str"))
4690 (should (string= (python-util-strip-string "str \n\r") "str"))
4691 (should (string= (python-util-strip-string "\t\r\n str \n\r ") "str"))
4692 (should
4693 (string= (python-util-strip-string "\n str \nin \tg \n\r") "str \nin \tg"))
4694 (should (string= (python-util-strip-string "\n \t \n\r ") ""))
4695 (should (string= (python-util-strip-string "") "")))
4697 (ert-deftest python-util-forward-comment-1 ()
4698 (python-tests-with-temp-buffer
4699 (concat
4700 "# a comment
4701 # another comment
4702 # bad indented comment
4703 # more comments" (make-string 9999 ?\n))
4704 (python-util-forward-comment 1)
4705 (should (= (point) (point-max)))
4706 (python-util-forward-comment -1)
4707 (should (= (point) (point-min)))))
4709 (ert-deftest python-util-valid-regexp-p-1 ()
4710 (should (python-util-valid-regexp-p ""))
4711 (should (python-util-valid-regexp-p python-shell-prompt-regexp))
4712 (should (not (python-util-valid-regexp-p "\\("))))
4715 ;;; Electricity
4717 (ert-deftest python-parens-electric-indent-1 ()
4718 (let ((eim electric-indent-mode))
4719 (unwind-protect
4720 (progn
4721 (python-tests-with-temp-buffer
4723 from django.conf.urls import patterns, include, url
4725 from django.contrib import admin
4727 from myapp import views
4730 urlpatterns = patterns('',
4731 url(r'^$', views.index
4734 (electric-indent-mode 1)
4735 (python-tests-look-at "views.index")
4736 (end-of-line)
4738 ;; Inserting commas within the same line should leave
4739 ;; indentation unchanged.
4740 (python-tests-self-insert ",")
4741 (should (= (current-indentation) 4))
4743 ;; As well as any other input happening within the same
4744 ;; set of parens.
4745 (python-tests-self-insert " name='index')")
4746 (should (= (current-indentation) 4))
4748 ;; But a comma outside it, should trigger indentation.
4749 (python-tests-self-insert ",")
4750 (should (= (current-indentation) 23))
4752 ;; Newline indents to the first argument column
4753 (python-tests-self-insert "\n")
4754 (should (= (current-indentation) 23))
4756 ;; All this input must not change indentation
4757 (indent-line-to 4)
4758 (python-tests-self-insert "url(r'^/login$', views.login)")
4759 (should (= (current-indentation) 4))
4761 ;; But this comma does
4762 (python-tests-self-insert ",")
4763 (should (= (current-indentation) 23))))
4764 (or eim (electric-indent-mode -1)))))
4766 (ert-deftest python-triple-quote-pairing ()
4767 (let ((epm electric-pair-mode))
4768 (unwind-protect
4769 (progn
4770 (python-tests-with-temp-buffer
4771 "\"\"\n"
4772 (or epm (electric-pair-mode 1))
4773 (goto-char (1- (point-max)))
4774 (python-tests-self-insert ?\")
4775 (should (string= (buffer-string)
4776 "\"\"\"\"\"\"\n"))
4777 (should (= (point) 4)))
4778 (python-tests-with-temp-buffer
4779 "\n"
4780 (python-tests-self-insert (list ?\" ?\" ?\"))
4781 (should (string= (buffer-string)
4782 "\"\"\"\"\"\"\n"))
4783 (should (= (point) 4)))
4784 (python-tests-with-temp-buffer
4785 "\"\n\"\"\n"
4786 (goto-char (1- (point-max)))
4787 (python-tests-self-insert ?\")
4788 (should (= (point) (1- (point-max))))
4789 (should (string= (buffer-string)
4790 "\"\n\"\"\"\n"))))
4791 (or epm (electric-pair-mode -1)))))
4794 ;;; Hideshow support
4796 (ert-deftest python-hideshow-hide-levels-1 ()
4797 "Should hide all methods when called after class start."
4798 (let ((enabled hs-minor-mode))
4799 (unwind-protect
4800 (progn
4801 (python-tests-with-temp-buffer
4803 class SomeClass:
4805 def __init__(self, arg, kwarg=1):
4806 self.arg = arg
4807 self.kwarg = kwarg
4809 def filter(self, nums):
4810 def fn(item):
4811 return item in [self.arg, self.kwarg]
4812 return filter(fn, nums)
4814 def __str__(self):
4815 return '%s-%s' % (self.arg, self.kwarg)
4817 (hs-minor-mode 1)
4818 (python-tests-look-at "class SomeClass:")
4819 (forward-line)
4820 (hs-hide-level 1)
4821 (should
4822 (string=
4823 (python-tests-visible-string)
4825 class SomeClass:
4827 def __init__(self, arg, kwarg=1):
4828 def filter(self, nums):
4829 def __str__(self):"))))
4830 (or enabled (hs-minor-mode -1)))))
4832 (ert-deftest python-hideshow-hide-levels-2 ()
4833 "Should hide nested methods and parens at end of defun."
4834 (let ((enabled hs-minor-mode))
4835 (unwind-protect
4836 (progn
4837 (python-tests-with-temp-buffer
4839 class SomeClass:
4841 def __init__(self, arg, kwarg=1):
4842 self.arg = arg
4843 self.kwarg = kwarg
4845 def filter(self, nums):
4846 def fn(item):
4847 return item in [self.arg, self.kwarg]
4848 return filter(fn, nums)
4850 def __str__(self):
4851 return '%s-%s' % (self.arg, self.kwarg)
4853 (hs-minor-mode 1)
4854 (python-tests-look-at "def fn(item):")
4855 (hs-hide-block)
4856 (should
4857 (string=
4858 (python-tests-visible-string)
4860 class SomeClass:
4862 def __init__(self, arg, kwarg=1):
4863 self.arg = arg
4864 self.kwarg = kwarg
4866 def filter(self, nums):
4867 def fn(item):
4868 return filter(fn, nums)
4870 def __str__(self):
4871 return '%s-%s' % (self.arg, self.kwarg)
4872 "))))
4873 (or enabled (hs-minor-mode -1)))))
4877 (provide 'python-tests)
4879 ;; Local Variables:
4880 ;; coding: utf-8
4881 ;; indent-tabs-mode: nil
4882 ;; End:
4884 ;;; python-tests.el ends here