]> code.delx.au - gnu-emacs/blob - test/automated/python-tests.el
Merge from origin/emacs-24
[gnu-emacs] / test / automated / python-tests.el
1 ;;; python-tests.el --- Test suite for python.el
2
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
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.
11
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.
16
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/>.
19
20 ;;; Commentary:
21
22 ;;; Code:
23
24 (require 'ert)
25 (require 'python)
26
27 (defmacro python-tests-with-temp-buffer (contents &rest body)
28 "Create a `python-mode' enabled temp buffer with CONTENTS.
29 BODY is code to be executed within the temp buffer. Point is
30 always located at the beginning of buffer."
31 (declare (indent 1) (debug t))
32 `(with-temp-buffer
33 (python-mode)
34 (insert ,contents)
35 (goto-char (point-min))
36 ,@body))
37
38 (defmacro python-tests-with-temp-file (contents &rest body)
39 "Create a `python-mode' enabled file with CONTENTS.
40 BODY is code to be executed within the temp buffer. Point is
41 always located at the beginning of buffer."
42 (declare (indent 1) (debug t))
43 ;; temp-file never actually used for anything?
44 `(let* ((temp-file (make-temp-file "python-tests" nil ".py"))
45 (buffer (find-file-noselect temp-file)))
46 (unwind-protect
47 (with-current-buffer buffer
48 (python-mode)
49 (insert ,contents)
50 (goto-char (point-min))
51 ,@body)
52 (and buffer (kill-buffer buffer))
53 (delete-file temp-file))))
54
55 (defun python-tests-look-at (string &optional num restore-point)
56 "Move point at beginning of STRING in the current buffer.
57 Optional argument NUM defaults to 1 and is an integer indicating
58 how many occurrences must be found, when positive the search is
59 done forwards, otherwise backwards. When RESTORE-POINT is
60 non-nil the point is not moved but the position found is still
61 returned. When searching forward and point is already looking at
62 STRING, it is skipped so the next STRING occurrence is selected."
63 (let* ((num (or num 1))
64 (starting-point (point))
65 (string (regexp-quote string))
66 (search-fn (if (> num 0) #'re-search-forward #'re-search-backward))
67 (deinc-fn (if (> num 0) #'1- #'1+))
68 (found-point))
69 (prog2
70 (catch 'exit
71 (while (not (= num 0))
72 (when (and (> num 0)
73 (looking-at string))
74 ;; Moving forward and already looking at STRING, skip it.
75 (forward-char (length (match-string-no-properties 0))))
76 (and (not (funcall search-fn string nil t))
77 (throw 'exit t))
78 (when (> num 0)
79 ;; `re-search-forward' leaves point at the end of the
80 ;; occurrence, move back so point is at the beginning
81 ;; instead.
82 (forward-char (- (length (match-string-no-properties 0)))))
83 (setq
84 num (funcall deinc-fn num)
85 found-point (point))))
86 found-point
87 (and restore-point (goto-char starting-point)))))
88
89 (defun python-tests-self-insert (char-or-str)
90 "Call `self-insert-command' for chars in CHAR-OR-STR."
91 (let ((chars
92 (cond
93 ((characterp char-or-str)
94 (list char-or-str))
95 ((stringp char-or-str)
96 (string-to-list char-or-str))
97 ((not
98 (cl-remove-if #'characterp char-or-str))
99 char-or-str)
100 (t (error "CHAR-OR-STR must be a char, string, or list of char")))))
101 (mapc
102 (lambda (char)
103 (let ((last-command-event char))
104 (call-interactively 'self-insert-command)))
105 chars)))
106
107 \f
108 ;;; Tests for your tests, so you can test while you test.
109
110 (ert-deftest python-tests-look-at-1 ()
111 "Test forward movement."
112 (python-tests-with-temp-buffer
113 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
114 sed do eiusmod tempor incididunt ut labore et dolore magna
115 aliqua."
116 (let ((expected (save-excursion
117 (dotimes (i 3)
118 (re-search-forward "et" nil t))
119 (forward-char -2)
120 (point))))
121 (should (= (python-tests-look-at "et" 3 t) expected))
122 ;; Even if NUM is bigger than found occurrences the point of last
123 ;; one should be returned.
124 (should (= (python-tests-look-at "et" 6 t) expected))
125 ;; If already looking at STRING, it should skip it.
126 (dotimes (i 2) (re-search-forward "et"))
127 (forward-char -2)
128 (should (= (python-tests-look-at "et") expected)))))
129
130 (ert-deftest python-tests-look-at-2 ()
131 "Test backward movement."
132 (python-tests-with-temp-buffer
133 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
134 sed do eiusmod tempor incididunt ut labore et dolore magna
135 aliqua."
136 (let ((expected
137 (save-excursion
138 (re-search-forward "et" nil t)
139 (forward-char -2)
140 (point))))
141 (dotimes (i 3)
142 (re-search-forward "et" nil t))
143 (should (= (python-tests-look-at "et" -3 t) expected))
144 (should (= (python-tests-look-at "et" -6 t) expected)))))
145
146 \f
147 ;;; Bindings
148
149 \f
150 ;;; Python specialized rx
151
152 \f
153 ;;; Font-lock and syntax
154
155 (ert-deftest python-syntax-after-python-backspace ()
156 ;; `python-indent-dedent-line-backspace' garbles syntax
157 :expected-result :failed
158 (python-tests-with-temp-buffer
159 "\"\"\""
160 (goto-char (point-max))
161 (python-indent-dedent-line-backspace 1)
162 (should (string= (buffer-string) "\"\""))
163 (should (null (nth 3 (syntax-ppss))))))
164
165 \f
166 ;;; Indentation
167
168 ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
169
170 (ert-deftest python-indent-pep8-1 ()
171 "First pep8 case."
172 (python-tests-with-temp-buffer
173 "# Aligned with opening delimiter
174 foo = long_function_name(var_one, var_two,
175 var_three, var_four)
176 "
177 (should (eq (car (python-indent-context)) 'no-indent))
178 (should (= (python-indent-calculate-indentation) 0))
179 (python-tests-look-at "foo = long_function_name(var_one, var_two,")
180 (should (eq (car (python-indent-context)) 'after-line))
181 (should (= (python-indent-calculate-indentation) 0))
182 (python-tests-look-at "var_three, var_four)")
183 (should (eq (car (python-indent-context)) 'inside-paren))
184 (should (= (python-indent-calculate-indentation) 25))))
185
186 (ert-deftest python-indent-pep8-2 ()
187 "Second pep8 case."
188 (python-tests-with-temp-buffer
189 "# More indentation included to distinguish this from the rest.
190 def long_function_name(
191 var_one, var_two, var_three,
192 var_four):
193 print (var_one)
194 "
195 (should (eq (car (python-indent-context)) 'no-indent))
196 (should (= (python-indent-calculate-indentation) 0))
197 (python-tests-look-at "def long_function_name(")
198 (should (eq (car (python-indent-context)) 'after-line))
199 (should (= (python-indent-calculate-indentation) 0))
200 (python-tests-look-at "var_one, var_two, var_three,")
201 (should (eq (car (python-indent-context)) 'inside-paren))
202 (should (= (python-indent-calculate-indentation) 8))
203 (python-tests-look-at "var_four):")
204 (should (eq (car (python-indent-context)) 'inside-paren))
205 (should (= (python-indent-calculate-indentation) 8))
206 (python-tests-look-at "print (var_one)")
207 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
208 (should (= (python-indent-calculate-indentation) 4))))
209
210 (ert-deftest python-indent-pep8-3 ()
211 "Third pep8 case."
212 (python-tests-with-temp-buffer
213 "# Extra indentation is not necessary.
214 foo = long_function_name(
215 var_one, var_two,
216 var_three, var_four)
217 "
218 (should (eq (car (python-indent-context)) 'no-indent))
219 (should (= (python-indent-calculate-indentation) 0))
220 (python-tests-look-at "foo = long_function_name(")
221 (should (eq (car (python-indent-context)) 'after-line))
222 (should (= (python-indent-calculate-indentation) 0))
223 (python-tests-look-at "var_one, var_two,")
224 (should (eq (car (python-indent-context)) 'inside-paren))
225 (should (= (python-indent-calculate-indentation) 4))
226 (python-tests-look-at "var_three, var_four)")
227 (should (eq (car (python-indent-context)) 'inside-paren))
228 (should (= (python-indent-calculate-indentation) 4))))
229
230 (ert-deftest python-indent-after-comment-1 ()
231 "The most simple after-comment case that shouldn't fail."
232 (python-tests-with-temp-buffer
233 "# Contents will be modified to correct indentation
234 class Blag(object):
235 def _on_child_complete(self, child_future):
236 if self.in_terminal_state():
237 pass
238 # We only complete when all our async children have entered a
239 # terminal state. At that point, if any child failed, we fail
240 # with the exception with which the first child failed.
241 "
242 (python-tests-look-at "# We only complete")
243 (should (eq (car (python-indent-context)) 'after-line))
244 (should (= (python-indent-calculate-indentation) 8))
245 (python-tests-look-at "# terminal state")
246 (should (eq (car (python-indent-context)) 'after-comment))
247 (should (= (python-indent-calculate-indentation) 8))
248 (python-tests-look-at "# with the exception")
249 (should (eq (car (python-indent-context)) 'after-comment))
250 ;; This one indents relative to previous block, even given the fact
251 ;; that it was under-indented.
252 (should (= (python-indent-calculate-indentation) 4))
253 (python-tests-look-at "# terminal state" -1)
254 ;; It doesn't hurt to check again.
255 (should (eq (car (python-indent-context)) 'after-comment))
256 (python-indent-line)
257 (should (= (current-indentation) 8))
258 (python-tests-look-at "# with the exception")
259 (should (eq (car (python-indent-context)) 'after-comment))
260 ;; Now everything should be lined up.
261 (should (= (python-indent-calculate-indentation) 8))))
262
263 (ert-deftest python-indent-after-comment-2 ()
264 "Test after-comment in weird cases."
265 (python-tests-with-temp-buffer
266 "# Contents will be modified to correct indentation
267 def func(arg):
268 # I don't do much
269 return arg
270 # This comment is badly indented just because.
271 # But we won't mess with the user in this line.
272
273 now_we_do_mess_cause_this_is_not_a_comment = 1
274
275 # yeah, that.
276 "
277 (python-tests-look-at "# I don't do much")
278 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
279 (should (= (python-indent-calculate-indentation) 4))
280 (python-tests-look-at "return arg")
281 ;; Comment here just gets ignored, this line is not a comment so
282 ;; the rules won't apply here.
283 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
284 (should (= (python-indent-calculate-indentation) 4))
285 (python-tests-look-at "# This comment is badly")
286 (should (eq (car (python-indent-context)) 'after-line))
287 ;; The return keyword moves indentation backwards 4 spaces, but
288 ;; let's assume this comment was placed there because the user
289 ;; wanted to (manually adding spaces or whatever).
290 (should (= (python-indent-calculate-indentation) 0))
291 (python-tests-look-at "# but we won't mess")
292 (should (eq (car (python-indent-context)) 'after-comment))
293 (should (= (python-indent-calculate-indentation) 4))
294 ;; Behave the same for blank lines: potentially a comment.
295 (forward-line 1)
296 (should (eq (car (python-indent-context)) 'after-comment))
297 (should (= (python-indent-calculate-indentation) 4))
298 (python-tests-look-at "now_we_do_mess")
299 ;; Here is where comment indentation starts to get ignored and
300 ;; where the user can't freely indent anymore.
301 (should (eq (car (python-indent-context)) 'after-line))
302 (should (= (python-indent-calculate-indentation) 0))
303 (python-tests-look-at "# yeah, that.")
304 (should (eq (car (python-indent-context)) 'after-line))
305 (should (= (python-indent-calculate-indentation) 0))))
306
307 (ert-deftest python-indent-inside-paren-1 ()
308 "The most simple inside-paren case that shouldn't fail."
309 (python-tests-with-temp-buffer
310 "
311 data = {
312 'key':
313 {
314 'objlist': [
315 {
316 'pk': 1,
317 'name': 'first',
318 },
319 {
320 'pk': 2,
321 'name': 'second',
322 }
323 ]
324 }
325 }
326 "
327 (python-tests-look-at "data = {")
328 (should (eq (car (python-indent-context)) 'after-line))
329 (should (= (python-indent-calculate-indentation) 0))
330 (python-tests-look-at "'key':")
331 (should (eq (car (python-indent-context)) 'inside-paren))
332 (should (= (python-indent-calculate-indentation) 4))
333 (python-tests-look-at "{")
334 (should (eq (car (python-indent-context)) 'inside-paren))
335 (should (= (python-indent-calculate-indentation) 4))
336 (python-tests-look-at "'objlist': [")
337 (should (eq (car (python-indent-context)) 'inside-paren))
338 (should (= (python-indent-calculate-indentation) 8))
339 (python-tests-look-at "{")
340 (should (eq (car (python-indent-context)) 'inside-paren))
341 (should (= (python-indent-calculate-indentation) 12))
342 (python-tests-look-at "'pk': 1,")
343 (should (eq (car (python-indent-context)) 'inside-paren))
344 (should (= (python-indent-calculate-indentation) 16))
345 (python-tests-look-at "'name': 'first',")
346 (should (eq (car (python-indent-context)) 'inside-paren))
347 (should (= (python-indent-calculate-indentation) 16))
348 (python-tests-look-at "},")
349 (should (eq (car (python-indent-context)) 'inside-paren))
350 (should (= (python-indent-calculate-indentation) 12))
351 (python-tests-look-at "{")
352 (should (eq (car (python-indent-context)) 'inside-paren))
353 (should (= (python-indent-calculate-indentation) 12))
354 (python-tests-look-at "'pk': 2,")
355 (should (eq (car (python-indent-context)) 'inside-paren))
356 (should (= (python-indent-calculate-indentation) 16))
357 (python-tests-look-at "'name': 'second',")
358 (should (eq (car (python-indent-context)) 'inside-paren))
359 (should (= (python-indent-calculate-indentation) 16))
360 (python-tests-look-at "}")
361 (should (eq (car (python-indent-context)) 'inside-paren))
362 (should (= (python-indent-calculate-indentation) 12))
363 (python-tests-look-at "]")
364 (should (eq (car (python-indent-context)) 'inside-paren))
365 (should (= (python-indent-calculate-indentation) 8))
366 (python-tests-look-at "}")
367 (should (eq (car (python-indent-context)) 'inside-paren))
368 (should (= (python-indent-calculate-indentation) 4))
369 (python-tests-look-at "}")
370 (should (eq (car (python-indent-context)) 'inside-paren))
371 (should (= (python-indent-calculate-indentation) 0))))
372
373 (ert-deftest python-indent-inside-paren-2 ()
374 "Another more compact paren group style."
375 (python-tests-with-temp-buffer
376 "
377 data = {'key': {
378 'objlist': [
379 {'pk': 1,
380 'name': 'first'},
381 {'pk': 2,
382 'name': 'second'}
383 ]
384 }}
385 "
386 (python-tests-look-at "data = {")
387 (should (eq (car (python-indent-context)) 'after-line))
388 (should (= (python-indent-calculate-indentation) 0))
389 (python-tests-look-at "'objlist': [")
390 (should (eq (car (python-indent-context)) 'inside-paren))
391 (should (= (python-indent-calculate-indentation) 4))
392 (python-tests-look-at "{'pk': 1,")
393 (should (eq (car (python-indent-context)) 'inside-paren))
394 (should (= (python-indent-calculate-indentation) 8))
395 (python-tests-look-at "'name': 'first'},")
396 (should (eq (car (python-indent-context)) 'inside-paren))
397 (should (= (python-indent-calculate-indentation) 9))
398 (python-tests-look-at "{'pk': 2,")
399 (should (eq (car (python-indent-context)) 'inside-paren))
400 (should (= (python-indent-calculate-indentation) 8))
401 (python-tests-look-at "'name': 'second'}")
402 (should (eq (car (python-indent-context)) 'inside-paren))
403 (should (= (python-indent-calculate-indentation) 9))
404 (python-tests-look-at "]")
405 (should (eq (car (python-indent-context)) 'inside-paren))
406 (should (= (python-indent-calculate-indentation) 4))
407 (python-tests-look-at "}}")
408 (should (eq (car (python-indent-context)) 'inside-paren))
409 (should (= (python-indent-calculate-indentation) 0))
410 (python-tests-look-at "}")
411 (should (eq (car (python-indent-context)) 'inside-paren))
412 (should (= (python-indent-calculate-indentation) 0))))
413
414 (ert-deftest python-indent-after-block-1 ()
415 "The most simple after-block case that shouldn't fail."
416 (python-tests-with-temp-buffer
417 "
418 def foo(a, b, c=True):
419 "
420 (should (eq (car (python-indent-context)) 'no-indent))
421 (should (= (python-indent-calculate-indentation) 0))
422 (goto-char (point-max))
423 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
424 (should (= (python-indent-calculate-indentation) 4))))
425
426 (ert-deftest python-indent-after-block-2 ()
427 "A weird (malformed) multiline block statement."
428 (python-tests-with-temp-buffer
429 "
430 def foo(a, b, c={
431 'a':
432 }):
433 "
434 (goto-char (point-max))
435 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
436 (should (= (python-indent-calculate-indentation) 4))))
437
438 (ert-deftest python-indent-after-backslash-1 ()
439 "The most common case."
440 (python-tests-with-temp-buffer
441 "
442 from foo.bar.baz import something, something_1 \\\\
443 something_2 something_3, \\\\
444 something_4, something_5
445 "
446 (python-tests-look-at "from foo.bar.baz import something, something_1")
447 (should (eq (car (python-indent-context)) 'after-line))
448 (should (= (python-indent-calculate-indentation) 0))
449 (python-tests-look-at "something_2 something_3,")
450 (should (eq (car (python-indent-context)) 'after-backslash))
451 (should (= (python-indent-calculate-indentation) 4))
452 (python-tests-look-at "something_4, something_5")
453 (should (eq (car (python-indent-context)) 'after-backslash))
454 (should (= (python-indent-calculate-indentation) 4))
455 (goto-char (point-max))
456 (should (eq (car (python-indent-context)) 'after-line))
457 (should (= (python-indent-calculate-indentation) 0))))
458
459 (ert-deftest python-indent-after-backslash-2 ()
460 "A pretty extreme complicated case."
461 (python-tests-with-temp-buffer
462 "
463 objects = Thing.objects.all() \\\\
464 .filter(
465 type='toy',
466 status='bought'
467 ) \\\\
468 .aggregate(
469 Sum('amount')
470 ) \\\\
471 .values_list()
472 "
473 (python-tests-look-at "objects = Thing.objects.all()")
474 (should (eq (car (python-indent-context)) 'after-line))
475 (should (= (python-indent-calculate-indentation) 0))
476 (python-tests-look-at ".filter(")
477 (should (eq (car (python-indent-context)) 'after-backslash))
478 (should (= (python-indent-calculate-indentation) 23))
479 (python-tests-look-at "type='toy',")
480 (should (eq (car (python-indent-context)) 'inside-paren))
481 (should (= (python-indent-calculate-indentation) 27))
482 (python-tests-look-at "status='bought'")
483 (should (eq (car (python-indent-context)) 'inside-paren))
484 (should (= (python-indent-calculate-indentation) 27))
485 (python-tests-look-at ") \\\\")
486 (should (eq (car (python-indent-context)) 'inside-paren))
487 (should (= (python-indent-calculate-indentation) 23))
488 (python-tests-look-at ".aggregate(")
489 (should (eq (car (python-indent-context)) 'after-backslash))
490 (should (= (python-indent-calculate-indentation) 23))
491 (python-tests-look-at "Sum('amount')")
492 (should (eq (car (python-indent-context)) 'inside-paren))
493 (should (= (python-indent-calculate-indentation) 27))
494 (python-tests-look-at ") \\\\")
495 (should (eq (car (python-indent-context)) 'inside-paren))
496 (should (= (python-indent-calculate-indentation) 23))
497 (python-tests-look-at ".values_list()")
498 (should (eq (car (python-indent-context)) 'after-backslash))
499 (should (= (python-indent-calculate-indentation) 23))
500 (forward-line 1)
501 (should (eq (car (python-indent-context)) 'after-line))
502 (should (= (python-indent-calculate-indentation) 0))))
503
504 (ert-deftest python-indent-block-enders-1 ()
505 "Test de-indentation for pass keyword."
506 (python-tests-with-temp-buffer
507 "
508 Class foo(object):
509
510 def bar(self):
511 if self.baz:
512 return (1,
513 2,
514 3)
515
516 else:
517 pass
518 "
519 (python-tests-look-at "3)")
520 (forward-line 1)
521 (should (= (python-indent-calculate-indentation) 8))
522 (python-tests-look-at "pass")
523 (forward-line 1)
524 (should (= (python-indent-calculate-indentation) 8))))
525
526 (ert-deftest python-indent-block-enders-2 ()
527 "Test de-indentation for return keyword."
528 (python-tests-with-temp-buffer
529 "
530 Class foo(object):
531 '''raise lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
532
533 eiusmod tempor incididunt ut labore et dolore magna aliqua.
534 '''
535 def bar(self):
536 \"return (1, 2, 3).\"
537 if self.baz:
538 return (1,
539 2,
540 3)
541 "
542 (python-tests-look-at "def")
543 (should (= (python-indent-calculate-indentation) 4))
544 (python-tests-look-at "if")
545 (should (= (python-indent-calculate-indentation) 8))
546 (python-tests-look-at "return")
547 (should (= (python-indent-calculate-indentation) 12))
548 (goto-char (point-max))
549 (should (= (python-indent-calculate-indentation) 8))))
550
551 (ert-deftest python-indent-block-enders-3 ()
552 "Test de-indentation for continue keyword."
553 (python-tests-with-temp-buffer
554 "
555 for element in lst:
556 if element is None:
557 continue
558 "
559 (python-tests-look-at "if")
560 (should (= (python-indent-calculate-indentation) 4))
561 (python-tests-look-at "continue")
562 (should (= (python-indent-calculate-indentation) 8))
563 (forward-line 1)
564 (should (= (python-indent-calculate-indentation) 4))))
565
566 (ert-deftest python-indent-block-enders-4 ()
567 "Test de-indentation for break keyword."
568 (python-tests-with-temp-buffer
569 "
570 for element in lst:
571 if element is None:
572 break
573 "
574 (python-tests-look-at "if")
575 (should (= (python-indent-calculate-indentation) 4))
576 (python-tests-look-at "break")
577 (should (= (python-indent-calculate-indentation) 8))
578 (forward-line 1)
579 (should (= (python-indent-calculate-indentation) 4))))
580
581 (ert-deftest python-indent-block-enders-5 ()
582 "Test de-indentation for raise keyword."
583 (python-tests-with-temp-buffer
584 "
585 for element in lst:
586 if element is None:
587 raise ValueError('Element cannot be None')
588 "
589 (python-tests-look-at "if")
590 (should (= (python-indent-calculate-indentation) 4))
591 (python-tests-look-at "raise")
592 (should (= (python-indent-calculate-indentation) 8))
593 (forward-line 1)
594 (should (= (python-indent-calculate-indentation) 4))))
595
596 (ert-deftest python-indent-dedenters-1 ()
597 "Test de-indentation for the elif keyword."
598 (python-tests-with-temp-buffer
599 "
600 if save:
601 try:
602 write_to_disk(data)
603 finally:
604 cleanup()
605 elif
606 "
607 (python-tests-look-at "elif\n")
608 (should (eq (car (python-indent-context)) 'dedenter-statement))
609 (should (= (python-indent-calculate-indentation) 0))
610 (should (equal (python-indent-calculate-levels) '(0)))))
611
612 (ert-deftest python-indent-dedenters-2 ()
613 "Test de-indentation for the else keyword."
614 (python-tests-with-temp-buffer
615 "
616 if save:
617 try:
618 write_to_disk(data)
619 except IOError:
620 msg = 'Error saving to disk'
621 message(msg)
622 logger.exception(msg)
623 except Exception:
624 if hide_details:
625 logger.exception('Unhandled exception')
626 else
627 finally:
628 data.free()
629 "
630 (python-tests-look-at "else\n")
631 (should (eq (car (python-indent-context)) 'dedenter-statement))
632 (should (= (python-indent-calculate-indentation) 8))
633 (should (equal (python-indent-calculate-levels) '(0 4 8)))))
634
635 (ert-deftest python-indent-dedenters-3 ()
636 "Test de-indentation for the except keyword."
637 (python-tests-with-temp-buffer
638 "
639 if save:
640 try:
641 write_to_disk(data)
642 except
643 "
644 (python-tests-look-at "except\n")
645 (should (eq (car (python-indent-context)) 'dedenter-statement))
646 (should (= (python-indent-calculate-indentation) 4))
647 (should (equal (python-indent-calculate-levels) '(4)))))
648
649 (ert-deftest python-indent-dedenters-4 ()
650 "Test de-indentation for the finally keyword."
651 (python-tests-with-temp-buffer
652 "
653 if save:
654 try:
655 write_to_disk(data)
656 finally
657 "
658 (python-tests-look-at "finally\n")
659 (should (eq (car (python-indent-context)) 'dedenter-statement))
660 (should (= (python-indent-calculate-indentation) 4))
661 (should (equal (python-indent-calculate-levels) '(4)))))
662
663 (ert-deftest python-indent-dedenters-5 ()
664 "Test invalid levels are skipped in a complex example."
665 (python-tests-with-temp-buffer
666 "
667 if save:
668 try:
669 write_to_disk(data)
670 except IOError:
671 msg = 'Error saving to disk'
672 message(msg)
673 logger.exception(msg)
674 finally:
675 if cleanup:
676 do_cleanup()
677 else
678 "
679 (python-tests-look-at "else\n")
680 (should (eq (car (python-indent-context)) 'dedenter-statement))
681 (should (= (python-indent-calculate-indentation) 8))
682 (should (equal (python-indent-calculate-levels) '(0 8)))))
683
684 (ert-deftest python-indent-dedenters-6 ()
685 "Test indentation is zero when no opening block for dedenter."
686 (python-tests-with-temp-buffer
687 "
688 try:
689 # if save:
690 write_to_disk(data)
691 else
692 "
693 (python-tests-look-at "else\n")
694 (should (eq (car (python-indent-context)) 'dedenter-statement))
695 (should (= (python-indent-calculate-indentation) 0))
696 (should (equal (python-indent-calculate-levels) '(0)))))
697
698 (ert-deftest python-indent-dedenters-7 ()
699 "Test indentation case from Bug#15163."
700 (python-tests-with-temp-buffer
701 "
702 if a:
703 if b:
704 pass
705 else:
706 pass
707 else:
708 "
709 (python-tests-look-at "else:" 2)
710 (should (eq (car (python-indent-context)) 'dedenter-statement))
711 (should (= (python-indent-calculate-indentation) 0))
712 (should (equal (python-indent-calculate-levels) '(0)))))
713
714 (ert-deftest python-indent-dedenters-8 ()
715 "Test indentation for Bug#18432."
716 (python-tests-with-temp-buffer
717 "
718 if (a == 1 or
719 a == 2):
720 pass
721 elif (a == 3 or
722 a == 4):
723 "
724 (python-tests-look-at "a == 4):\n")
725 (should (eq (car (python-indent-context)) 'inside-paren))
726 (should (= (python-indent-calculate-indentation) 6))
727 (should (equal (python-indent-calculate-levels) '(0 4 6)))))
728
729 (ert-deftest python-indent-electric-colon-1 ()
730 "Test indentation case from Bug#18228."
731 (python-tests-with-temp-buffer
732 "
733 def a():
734 pass
735
736 def b()
737 "
738 (python-tests-look-at "def b()")
739 (goto-char (line-end-position))
740 (python-tests-self-insert ":")
741 (should (= (current-indentation) 0))))
742
743 (ert-deftest python-indent-electric-colon-2 ()
744 "Test indentation case for dedenter."
745 (python-tests-with-temp-buffer
746 "
747 if do:
748 something()
749 else
750 "
751 (python-tests-look-at "else")
752 (goto-char (line-end-position))
753 (python-tests-self-insert ":")
754 (should (= (current-indentation) 0))))
755
756 (ert-deftest python-indent-electric-colon-3 ()
757 "Test indentation case for multi-line dedenter."
758 (python-tests-with-temp-buffer
759 "
760 if do:
761 something()
762 elif (this
763 and
764 that)
765 "
766 (python-tests-look-at "that)")
767 (goto-char (line-end-position))
768 (python-tests-self-insert ":")
769 (python-tests-look-at "elif" -1)
770 (should (= (current-indentation) 0))
771 (python-tests-look-at "and")
772 (should (= (current-indentation) 6))
773 (python-tests-look-at "that)")
774 (should (= (current-indentation) 6))))
775
776 (ert-deftest python-indent-region-1 ()
777 "Test indentation case from Bug#18843."
778 (let ((contents "
779 def foo ():
780 try:
781 pass
782 except:
783 pass
784 "))
785 (python-tests-with-temp-buffer
786 contents
787 (python-indent-region (point-min) (point-max))
788 (should (string= (buffer-substring-no-properties (point-min) (point-max))
789 contents)))))
790
791 (ert-deftest python-indent-region-2 ()
792 "Test region indentation on comments."
793 (let ((contents "
794 def f():
795 if True:
796 pass
797
798 # This is
799 # some multiline
800 # comment
801 "))
802 (python-tests-with-temp-buffer
803 contents
804 (python-indent-region (point-min) (point-max))
805 (should (string= (buffer-substring-no-properties (point-min) (point-max))
806 contents)))))
807
808 (ert-deftest python-indent-region-3 ()
809 "Test region indentation on comments."
810 (let ((contents "
811 def f():
812 if True:
813 pass
814 # This is
815 # some multiline
816 # comment
817 ")
818 (expected "
819 def f():
820 if True:
821 pass
822 # This is
823 # some multiline
824 # comment
825 "))
826 (python-tests-with-temp-buffer
827 contents
828 (python-indent-region (point-min) (point-max))
829 (should (string= (buffer-substring-no-properties (point-min) (point-max))
830 expected)))))
831
832 (ert-deftest python-indent-region-4 ()
833 "Test region indentation block starts, dedenters and enders."
834 (let ((contents "
835 def f():
836 if True:
837 a = 5
838 else:
839 a = 10
840 return a
841 ")
842 (expected "
843 def f():
844 if True:
845 a = 5
846 else:
847 a = 10
848 return a
849 "))
850 (python-tests-with-temp-buffer
851 contents
852 (python-indent-region (point-min) (point-max))
853 (should (string= (buffer-substring-no-properties (point-min) (point-max))
854 expected)))))
855
856 (ert-deftest python-indent-region-5 ()
857 "Test region indentation leaves strings untouched (start delimiter)."
858 (let ((contents "
859 def f():
860 '''
861 this is
862 a multiline
863 string
864 '''
865 ")
866 (expected "
867 def f():
868 '''
869 this is
870 a multiline
871 string
872 '''
873 "))
874 (python-tests-with-temp-buffer
875 contents
876 (python-indent-region (point-min) (point-max))
877 (should (string= (buffer-substring-no-properties (point-min) (point-max))
878 expected)))))
879
880 \f
881 ;;; Navigation
882
883 (ert-deftest python-nav-beginning-of-defun-1 ()
884 (python-tests-with-temp-buffer
885 "
886 def decoratorFunctionWithArguments(arg1, arg2, arg3):
887 '''print decorated function call data to stdout.
888
889 Usage:
890
891 @decoratorFunctionWithArguments('arg1', 'arg2')
892 def func(a, b, c=True):
893 pass
894 '''
895
896 def wwrap(f):
897 print 'Inside wwrap()'
898 def wrapped_f(*args):
899 print 'Inside wrapped_f()'
900 print 'Decorator arguments:', arg1, arg2, arg3
901 f(*args)
902 print 'After f(*args)'
903 return wrapped_f
904 return wwrap
905 "
906 (python-tests-look-at "return wrap")
907 (should (= (save-excursion
908 (python-nav-beginning-of-defun)
909 (point))
910 (save-excursion
911 (python-tests-look-at "def wrapped_f(*args):" -1)
912 (beginning-of-line)
913 (point))))
914 (python-tests-look-at "def wrapped_f(*args):" -1)
915 (should (= (save-excursion
916 (python-nav-beginning-of-defun)
917 (point))
918 (save-excursion
919 (python-tests-look-at "def wwrap(f):" -1)
920 (beginning-of-line)
921 (point))))
922 (python-tests-look-at "def wwrap(f):" -1)
923 (should (= (save-excursion
924 (python-nav-beginning-of-defun)
925 (point))
926 (save-excursion
927 (python-tests-look-at "def decoratorFunctionWithArguments" -1)
928 (beginning-of-line)
929 (point))))))
930
931 (ert-deftest python-nav-beginning-of-defun-2 ()
932 (python-tests-with-temp-buffer
933 "
934 class C(object):
935
936 def m(self):
937 self.c()
938
939 def b():
940 pass
941
942 def a():
943 pass
944
945 def c(self):
946 pass
947 "
948 ;; Nested defuns, are handled with care.
949 (python-tests-look-at "def c(self):")
950 (should (= (save-excursion
951 (python-nav-beginning-of-defun)
952 (point))
953 (save-excursion
954 (python-tests-look-at "def m(self):" -1)
955 (beginning-of-line)
956 (point))))
957 ;; Defuns on same levels should be respected.
958 (python-tests-look-at "def a():" -1)
959 (should (= (save-excursion
960 (python-nav-beginning-of-defun)
961 (point))
962 (save-excursion
963 (python-tests-look-at "def b():" -1)
964 (beginning-of-line)
965 (point))))
966 ;; Jump to a top level defun.
967 (python-tests-look-at "def b():" -1)
968 (should (= (save-excursion
969 (python-nav-beginning-of-defun)
970 (point))
971 (save-excursion
972 (python-tests-look-at "def m(self):" -1)
973 (beginning-of-line)
974 (point))))
975 ;; Jump to a top level defun again.
976 (python-tests-look-at "def m(self):" -1)
977 (should (= (save-excursion
978 (python-nav-beginning-of-defun)
979 (point))
980 (save-excursion
981 (python-tests-look-at "class C(object):" -1)
982 (beginning-of-line)
983 (point))))))
984
985 (ert-deftest python-nav-end-of-defun-1 ()
986 (python-tests-with-temp-buffer
987 "
988 class C(object):
989
990 def m(self):
991 self.c()
992
993 def b():
994 pass
995
996 def a():
997 pass
998
999 def c(self):
1000 pass
1001 "
1002 (should (= (save-excursion
1003 (python-tests-look-at "class C(object):")
1004 (python-nav-end-of-defun)
1005 (point))
1006 (save-excursion
1007 (point-max))))
1008 (should (= (save-excursion
1009 (python-tests-look-at "def m(self):")
1010 (python-nav-end-of-defun)
1011 (point))
1012 (save-excursion
1013 (python-tests-look-at "def c(self):")
1014 (forward-line -1)
1015 (point))))
1016 (should (= (save-excursion
1017 (python-tests-look-at "def b():")
1018 (python-nav-end-of-defun)
1019 (point))
1020 (save-excursion
1021 (python-tests-look-at "def b():")
1022 (forward-line 2)
1023 (point))))
1024 (should (= (save-excursion
1025 (python-tests-look-at "def c(self):")
1026 (python-nav-end-of-defun)
1027 (point))
1028 (save-excursion
1029 (point-max))))))
1030
1031 (ert-deftest python-nav-end-of-defun-2 ()
1032 (python-tests-with-temp-buffer
1033 "
1034 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1035 '''print decorated function call data to stdout.
1036
1037 Usage:
1038
1039 @decoratorFunctionWithArguments('arg1', 'arg2')
1040 def func(a, b, c=True):
1041 pass
1042 '''
1043
1044 def wwrap(f):
1045 print 'Inside wwrap()'
1046 def wrapped_f(*args):
1047 print 'Inside wrapped_f()'
1048 print 'Decorator arguments:', arg1, arg2, arg3
1049 f(*args)
1050 print 'After f(*args)'
1051 return wrapped_f
1052 return wwrap
1053 "
1054 (should (= (save-excursion
1055 (python-tests-look-at "def decoratorFunctionWithArguments")
1056 (python-nav-end-of-defun)
1057 (point))
1058 (save-excursion
1059 (point-max))))
1060 (should (= (save-excursion
1061 (python-tests-look-at "@decoratorFunctionWithArguments")
1062 (python-nav-end-of-defun)
1063 (point))
1064 (save-excursion
1065 (point-max))))
1066 (should (= (save-excursion
1067 (python-tests-look-at "def wwrap(f):")
1068 (python-nav-end-of-defun)
1069 (point))
1070 (save-excursion
1071 (python-tests-look-at "return wwrap")
1072 (line-beginning-position))))
1073 (should (= (save-excursion
1074 (python-tests-look-at "def wrapped_f(*args):")
1075 (python-nav-end-of-defun)
1076 (point))
1077 (save-excursion
1078 (python-tests-look-at "return wrapped_f")
1079 (line-beginning-position))))
1080 (should (= (save-excursion
1081 (python-tests-look-at "f(*args)")
1082 (python-nav-end-of-defun)
1083 (point))
1084 (save-excursion
1085 (python-tests-look-at "return wrapped_f")
1086 (line-beginning-position))))))
1087
1088 (ert-deftest python-nav-backward-defun-1 ()
1089 (python-tests-with-temp-buffer
1090 "
1091 class A(object): # A
1092
1093 def a(self): # a
1094 pass
1095
1096 def b(self): # b
1097 pass
1098
1099 class B(object): # B
1100
1101 class C(object): # C
1102
1103 def d(self): # d
1104 pass
1105
1106 # def e(self): # e
1107 # pass
1108
1109 def c(self): # c
1110 pass
1111
1112 # def d(self): # d
1113 # pass
1114 "
1115 (goto-char (point-max))
1116 (should (= (save-excursion (python-nav-backward-defun))
1117 (python-tests-look-at " def c(self): # c" -1)))
1118 (should (= (save-excursion (python-nav-backward-defun))
1119 (python-tests-look-at " def d(self): # d" -1)))
1120 (should (= (save-excursion (python-nav-backward-defun))
1121 (python-tests-look-at " class C(object): # C" -1)))
1122 (should (= (save-excursion (python-nav-backward-defun))
1123 (python-tests-look-at " class B(object): # B" -1)))
1124 (should (= (save-excursion (python-nav-backward-defun))
1125 (python-tests-look-at " def b(self): # b" -1)))
1126 (should (= (save-excursion (python-nav-backward-defun))
1127 (python-tests-look-at " def a(self): # a" -1)))
1128 (should (= (save-excursion (python-nav-backward-defun))
1129 (python-tests-look-at "class A(object): # A" -1)))
1130 (should (not (python-nav-backward-defun)))))
1131
1132 (ert-deftest python-nav-backward-defun-2 ()
1133 (python-tests-with-temp-buffer
1134 "
1135 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1136 '''print decorated function call data to stdout.
1137
1138 Usage:
1139
1140 @decoratorFunctionWithArguments('arg1', 'arg2')
1141 def func(a, b, c=True):
1142 pass
1143 '''
1144
1145 def wwrap(f):
1146 print 'Inside wwrap()'
1147 def wrapped_f(*args):
1148 print 'Inside wrapped_f()'
1149 print 'Decorator arguments:', arg1, arg2, arg3
1150 f(*args)
1151 print 'After f(*args)'
1152 return wrapped_f
1153 return wwrap
1154 "
1155 (goto-char (point-max))
1156 (should (= (save-excursion (python-nav-backward-defun))
1157 (python-tests-look-at " def wrapped_f(*args):" -1)))
1158 (should (= (save-excursion (python-nav-backward-defun))
1159 (python-tests-look-at " def wwrap(f):" -1)))
1160 (should (= (save-excursion (python-nav-backward-defun))
1161 (python-tests-look-at "def decoratorFunctionWithArguments(arg1, arg2, arg3):" -1)))
1162 (should (not (python-nav-backward-defun)))))
1163
1164 (ert-deftest python-nav-backward-defun-3 ()
1165 (python-tests-with-temp-buffer
1166 "
1167 '''
1168 def u(self):
1169 pass
1170
1171 def v(self):
1172 pass
1173
1174 def w(self):
1175 pass
1176 '''
1177
1178 class A(object):
1179 pass
1180 "
1181 (goto-char (point-min))
1182 (let ((point (python-tests-look-at "class A(object):")))
1183 (should (not (python-nav-backward-defun)))
1184 (should (= point (point))))))
1185
1186 (ert-deftest python-nav-forward-defun-1 ()
1187 (python-tests-with-temp-buffer
1188 "
1189 class A(object): # A
1190
1191 def a(self): # a
1192 pass
1193
1194 def b(self): # b
1195 pass
1196
1197 class B(object): # B
1198
1199 class C(object): # C
1200
1201 def d(self): # d
1202 pass
1203
1204 # def e(self): # e
1205 # pass
1206
1207 def c(self): # c
1208 pass
1209
1210 # def d(self): # d
1211 # pass
1212 "
1213 (goto-char (point-min))
1214 (should (= (save-excursion (python-nav-forward-defun))
1215 (python-tests-look-at "(object): # A")))
1216 (should (= (save-excursion (python-nav-forward-defun))
1217 (python-tests-look-at "(self): # a")))
1218 (should (= (save-excursion (python-nav-forward-defun))
1219 (python-tests-look-at "(self): # b")))
1220 (should (= (save-excursion (python-nav-forward-defun))
1221 (python-tests-look-at "(object): # B")))
1222 (should (= (save-excursion (python-nav-forward-defun))
1223 (python-tests-look-at "(object): # C")))
1224 (should (= (save-excursion (python-nav-forward-defun))
1225 (python-tests-look-at "(self): # d")))
1226 (should (= (save-excursion (python-nav-forward-defun))
1227 (python-tests-look-at "(self): # c")))
1228 (should (not (python-nav-forward-defun)))))
1229
1230 (ert-deftest python-nav-forward-defun-2 ()
1231 (python-tests-with-temp-buffer
1232 "
1233 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1234 '''print decorated function call data to stdout.
1235
1236 Usage:
1237
1238 @decoratorFunctionWithArguments('arg1', 'arg2')
1239 def func(a, b, c=True):
1240 pass
1241 '''
1242
1243 def wwrap(f):
1244 print 'Inside wwrap()'
1245 def wrapped_f(*args):
1246 print 'Inside wrapped_f()'
1247 print 'Decorator arguments:', arg1, arg2, arg3
1248 f(*args)
1249 print 'After f(*args)'
1250 return wrapped_f
1251 return wwrap
1252 "
1253 (goto-char (point-min))
1254 (should (= (save-excursion (python-nav-forward-defun))
1255 (python-tests-look-at "(arg1, arg2, arg3):")))
1256 (should (= (save-excursion (python-nav-forward-defun))
1257 (python-tests-look-at "(f):")))
1258 (should (= (save-excursion (python-nav-forward-defun))
1259 (python-tests-look-at "(*args):")))
1260 (should (not (python-nav-forward-defun)))))
1261
1262 (ert-deftest python-nav-forward-defun-3 ()
1263 (python-tests-with-temp-buffer
1264 "
1265 class A(object):
1266 pass
1267
1268 '''
1269 def u(self):
1270 pass
1271
1272 def v(self):
1273 pass
1274
1275 def w(self):
1276 pass
1277 '''
1278 "
1279 (goto-char (point-min))
1280 (let ((point (python-tests-look-at "(object):")))
1281 (should (not (python-nav-forward-defun)))
1282 (should (= point (point))))))
1283
1284 (ert-deftest python-nav-beginning-of-statement-1 ()
1285 (python-tests-with-temp-buffer
1286 "
1287 v1 = 123 + \
1288 456 + \
1289 789
1290 v2 = (value1,
1291 value2,
1292
1293 value3,
1294 value4)
1295 v3 = ('this is a string'
1296
1297 'that is continued'
1298 'between lines'
1299 'within a paren',
1300 # this is a comment, yo
1301 'continue previous line')
1302 v4 = '''
1303 a very long
1304 string
1305 '''
1306 "
1307 (python-tests-look-at "v2 =")
1308 (python-util-forward-comment -1)
1309 (should (= (save-excursion
1310 (python-nav-beginning-of-statement)
1311 (point))
1312 (python-tests-look-at "v1 =" -1 t)))
1313 (python-tests-look-at "v3 =")
1314 (python-util-forward-comment -1)
1315 (should (= (save-excursion
1316 (python-nav-beginning-of-statement)
1317 (point))
1318 (python-tests-look-at "v2 =" -1 t)))
1319 (python-tests-look-at "v4 =")
1320 (python-util-forward-comment -1)
1321 (should (= (save-excursion
1322 (python-nav-beginning-of-statement)
1323 (point))
1324 (python-tests-look-at "v3 =" -1 t)))
1325 (goto-char (point-max))
1326 (python-util-forward-comment -1)
1327 (should (= (save-excursion
1328 (python-nav-beginning-of-statement)
1329 (point))
1330 (python-tests-look-at "v4 =" -1 t)))))
1331
1332 (ert-deftest python-nav-end-of-statement-1 ()
1333 (python-tests-with-temp-buffer
1334 "
1335 v1 = 123 + \
1336 456 + \
1337 789
1338 v2 = (value1,
1339 value2,
1340
1341 value3,
1342 value4)
1343 v3 = ('this is a string'
1344
1345 'that is continued'
1346 'between lines'
1347 'within a paren',
1348 # this is a comment, yo
1349 'continue previous line')
1350 v4 = '''
1351 a very long
1352 string
1353 '''
1354 "
1355 (python-tests-look-at "v1 =")
1356 (should (= (save-excursion
1357 (python-nav-end-of-statement)
1358 (point))
1359 (save-excursion
1360 (python-tests-look-at "789")
1361 (line-end-position))))
1362 (python-tests-look-at "v2 =")
1363 (should (= (save-excursion
1364 (python-nav-end-of-statement)
1365 (point))
1366 (save-excursion
1367 (python-tests-look-at "value4)")
1368 (line-end-position))))
1369 (python-tests-look-at "v3 =")
1370 (should (= (save-excursion
1371 (python-nav-end-of-statement)
1372 (point))
1373 (save-excursion
1374 (python-tests-look-at
1375 "'continue previous line')")
1376 (line-end-position))))
1377 (python-tests-look-at "v4 =")
1378 (should (= (save-excursion
1379 (python-nav-end-of-statement)
1380 (point))
1381 (save-excursion
1382 (goto-char (point-max))
1383 (python-util-forward-comment -1)
1384 (point))))))
1385
1386 (ert-deftest python-nav-forward-statement-1 ()
1387 (python-tests-with-temp-buffer
1388 "
1389 v1 = 123 + \
1390 456 + \
1391 789
1392 v2 = (value1,
1393 value2,
1394
1395 value3,
1396 value4)
1397 v3 = ('this is a string'
1398
1399 'that is continued'
1400 'between lines'
1401 'within a paren',
1402 # this is a comment, yo
1403 'continue previous line')
1404 v4 = '''
1405 a very long
1406 string
1407 '''
1408 "
1409 (python-tests-look-at "v1 =")
1410 (should (= (save-excursion
1411 (python-nav-forward-statement)
1412 (point))
1413 (python-tests-look-at "v2 =")))
1414 (should (= (save-excursion
1415 (python-nav-forward-statement)
1416 (point))
1417 (python-tests-look-at "v3 =")))
1418 (should (= (save-excursion
1419 (python-nav-forward-statement)
1420 (point))
1421 (python-tests-look-at "v4 =")))
1422 (should (= (save-excursion
1423 (python-nav-forward-statement)
1424 (point))
1425 (point-max)))))
1426
1427 (ert-deftest python-nav-backward-statement-1 ()
1428 (python-tests-with-temp-buffer
1429 "
1430 v1 = 123 + \
1431 456 + \
1432 789
1433 v2 = (value1,
1434 value2,
1435
1436 value3,
1437 value4)
1438 v3 = ('this is a string'
1439
1440 'that is continued'
1441 'between lines'
1442 'within a paren',
1443 # this is a comment, yo
1444 'continue previous line')
1445 v4 = '''
1446 a very long
1447 string
1448 '''
1449 "
1450 (goto-char (point-max))
1451 (should (= (save-excursion
1452 (python-nav-backward-statement)
1453 (point))
1454 (python-tests-look-at "v4 =" -1)))
1455 (should (= (save-excursion
1456 (python-nav-backward-statement)
1457 (point))
1458 (python-tests-look-at "v3 =" -1)))
1459 (should (= (save-excursion
1460 (python-nav-backward-statement)
1461 (point))
1462 (python-tests-look-at "v2 =" -1)))
1463 (should (= (save-excursion
1464 (python-nav-backward-statement)
1465 (point))
1466 (python-tests-look-at "v1 =" -1)))))
1467
1468 (ert-deftest python-nav-backward-statement-2 ()
1469 :expected-result :failed
1470 (python-tests-with-temp-buffer
1471 "
1472 v1 = 123 + \
1473 456 + \
1474 789
1475 v2 = (value1,
1476 value2,
1477
1478 value3,
1479 value4)
1480 "
1481 ;; FIXME: For some reason `python-nav-backward-statement' is moving
1482 ;; back two sentences when starting from 'value4)'.
1483 (goto-char (point-max))
1484 (python-util-forward-comment -1)
1485 (should (= (save-excursion
1486 (python-nav-backward-statement)
1487 (point))
1488 (python-tests-look-at "v2 =" -1 t)))))
1489
1490 (ert-deftest python-nav-beginning-of-block-1 ()
1491 (python-tests-with-temp-buffer
1492 "
1493 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1494 '''print decorated function call data to stdout.
1495
1496 Usage:
1497
1498 @decoratorFunctionWithArguments('arg1', 'arg2')
1499 def func(a, b, c=True):
1500 pass
1501 '''
1502
1503 def wwrap(f):
1504 print 'Inside wwrap()'
1505 def wrapped_f(*args):
1506 print 'Inside wrapped_f()'
1507 print 'Decorator arguments:', arg1, arg2, arg3
1508 f(*args)
1509 print 'After f(*args)'
1510 return wrapped_f
1511 return wwrap
1512 "
1513 (python-tests-look-at "return wwrap")
1514 (should (= (save-excursion
1515 (python-nav-beginning-of-block)
1516 (point))
1517 (python-tests-look-at "def decoratorFunctionWithArguments" -1)))
1518 (python-tests-look-at "print 'Inside wwrap()'")
1519 (should (= (save-excursion
1520 (python-nav-beginning-of-block)
1521 (point))
1522 (python-tests-look-at "def wwrap(f):" -1)))
1523 (python-tests-look-at "print 'After f(*args)'")
1524 (end-of-line)
1525 (should (= (save-excursion
1526 (python-nav-beginning-of-block)
1527 (point))
1528 (python-tests-look-at "def wrapped_f(*args):" -1)))
1529 (python-tests-look-at "return wrapped_f")
1530 (should (= (save-excursion
1531 (python-nav-beginning-of-block)
1532 (point))
1533 (python-tests-look-at "def wwrap(f):" -1)))))
1534
1535 (ert-deftest python-nav-end-of-block-1 ()
1536 (python-tests-with-temp-buffer
1537 "
1538 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1539 '''print decorated function call data to stdout.
1540
1541 Usage:
1542
1543 @decoratorFunctionWithArguments('arg1', 'arg2')
1544 def func(a, b, c=True):
1545 pass
1546 '''
1547
1548 def wwrap(f):
1549 print 'Inside wwrap()'
1550 def wrapped_f(*args):
1551 print 'Inside wrapped_f()'
1552 print 'Decorator arguments:', arg1, arg2, arg3
1553 f(*args)
1554 print 'After f(*args)'
1555 return wrapped_f
1556 return wwrap
1557 "
1558 (python-tests-look-at "def decoratorFunctionWithArguments")
1559 (should (= (save-excursion
1560 (python-nav-end-of-block)
1561 (point))
1562 (save-excursion
1563 (goto-char (point-max))
1564 (python-util-forward-comment -1)
1565 (point))))
1566 (python-tests-look-at "def wwrap(f):")
1567 (should (= (save-excursion
1568 (python-nav-end-of-block)
1569 (point))
1570 (save-excursion
1571 (python-tests-look-at "return wrapped_f")
1572 (line-end-position))))
1573 (end-of-line)
1574 (should (= (save-excursion
1575 (python-nav-end-of-block)
1576 (point))
1577 (save-excursion
1578 (python-tests-look-at "return wrapped_f")
1579 (line-end-position))))
1580 (python-tests-look-at "f(*args)")
1581 (should (= (save-excursion
1582 (python-nav-end-of-block)
1583 (point))
1584 (save-excursion
1585 (python-tests-look-at "print 'After f(*args)'")
1586 (line-end-position))))))
1587
1588 (ert-deftest python-nav-forward-block-1 ()
1589 "This also accounts as a test for `python-nav-backward-block'."
1590 (python-tests-with-temp-buffer
1591 "
1592 if request.user.is_authenticated():
1593 # def block():
1594 # pass
1595 try:
1596 profile = request.user.get_profile()
1597 except Profile.DoesNotExist:
1598 profile = Profile.objects.create(user=request.user)
1599 else:
1600 if profile.stats:
1601 profile.recalculate_stats()
1602 else:
1603 profile.clear_stats()
1604 finally:
1605 profile.views += 1
1606 profile.save()
1607 "
1608 (should (= (save-excursion (python-nav-forward-block))
1609 (python-tests-look-at "if request.user.is_authenticated():")))
1610 (should (= (save-excursion (python-nav-forward-block))
1611 (python-tests-look-at "try:")))
1612 (should (= (save-excursion (python-nav-forward-block))
1613 (python-tests-look-at "except Profile.DoesNotExist:")))
1614 (should (= (save-excursion (python-nav-forward-block))
1615 (python-tests-look-at "else:")))
1616 (should (= (save-excursion (python-nav-forward-block))
1617 (python-tests-look-at "if profile.stats:")))
1618 (should (= (save-excursion (python-nav-forward-block))
1619 (python-tests-look-at "else:")))
1620 (should (= (save-excursion (python-nav-forward-block))
1621 (python-tests-look-at "finally:")))
1622 ;; When point is at the last block, leave it there and return nil
1623 (should (not (save-excursion (python-nav-forward-block))))
1624 ;; Move backwards, and even if the number of moves is less than the
1625 ;; provided argument return the point.
1626 (should (= (save-excursion (python-nav-forward-block -10))
1627 (python-tests-look-at
1628 "if request.user.is_authenticated():" -1)))))
1629
1630 (ert-deftest python-nav-forward-sexp-1 ()
1631 (python-tests-with-temp-buffer
1632 "
1633 a()
1634 b()
1635 c()
1636 "
1637 (python-tests-look-at "a()")
1638 (python-nav-forward-sexp)
1639 (should (looking-at "$"))
1640 (should (save-excursion
1641 (beginning-of-line)
1642 (looking-at "a()")))
1643 (python-nav-forward-sexp)
1644 (should (looking-at "$"))
1645 (should (save-excursion
1646 (beginning-of-line)
1647 (looking-at "b()")))
1648 (python-nav-forward-sexp)
1649 (should (looking-at "$"))
1650 (should (save-excursion
1651 (beginning-of-line)
1652 (looking-at "c()")))
1653 ;; Movement next to a paren should do what lisp does and
1654 ;; unfortunately It can't change, because otherwise
1655 ;; `blink-matching-open' breaks.
1656 (python-nav-forward-sexp -1)
1657 (should (looking-at "()"))
1658 (should (save-excursion
1659 (beginning-of-line)
1660 (looking-at "c()")))
1661 (python-nav-forward-sexp -1)
1662 (should (looking-at "c()"))
1663 (python-nav-forward-sexp -1)
1664 (should (looking-at "b()"))
1665 (python-nav-forward-sexp -1)
1666 (should (looking-at "a()"))))
1667
1668 (ert-deftest python-nav-forward-sexp-2 ()
1669 (python-tests-with-temp-buffer
1670 "
1671 def func():
1672 if True:
1673 aaa = bbb
1674 ccc = ddd
1675 eee = fff
1676 return ggg
1677 "
1678 (python-tests-look-at "aa =")
1679 (python-nav-forward-sexp)
1680 (should (looking-at " = bbb"))
1681 (python-nav-forward-sexp)
1682 (should (looking-at "$"))
1683 (should (save-excursion
1684 (back-to-indentation)
1685 (looking-at "aaa = bbb")))
1686 (python-nav-forward-sexp)
1687 (should (looking-at "$"))
1688 (should (save-excursion
1689 (back-to-indentation)
1690 (looking-at "ccc = ddd")))
1691 (python-nav-forward-sexp)
1692 (should (looking-at "$"))
1693 (should (save-excursion
1694 (back-to-indentation)
1695 (looking-at "eee = fff")))
1696 (python-nav-forward-sexp)
1697 (should (looking-at "$"))
1698 (should (save-excursion
1699 (back-to-indentation)
1700 (looking-at "return ggg")))
1701 (python-nav-forward-sexp -1)
1702 (should (looking-at "def func():"))))
1703
1704 (ert-deftest python-nav-forward-sexp-3 ()
1705 (python-tests-with-temp-buffer
1706 "
1707 from some_module import some_sub_module
1708 from another_module import another_sub_module
1709
1710 def another_statement():
1711 pass
1712 "
1713 (python-tests-look-at "some_module")
1714 (python-nav-forward-sexp)
1715 (should (looking-at " import"))
1716 (python-nav-forward-sexp)
1717 (should (looking-at " some_sub_module"))
1718 (python-nav-forward-sexp)
1719 (should (looking-at "$"))
1720 (should
1721 (save-excursion
1722 (back-to-indentation)
1723 (looking-at
1724 "from some_module import some_sub_module")))
1725 (python-nav-forward-sexp)
1726 (should (looking-at "$"))
1727 (should
1728 (save-excursion
1729 (back-to-indentation)
1730 (looking-at
1731 "from another_module import another_sub_module")))
1732 (python-nav-forward-sexp)
1733 (should (looking-at "$"))
1734 (should
1735 (save-excursion
1736 (back-to-indentation)
1737 (looking-at
1738 "pass")))
1739 (python-nav-forward-sexp -1)
1740 (should (looking-at "def another_statement():"))
1741 (python-nav-forward-sexp -1)
1742 (should (looking-at "from another_module import another_sub_module"))
1743 (python-nav-forward-sexp -1)
1744 (should (looking-at "from some_module import some_sub_module"))))
1745
1746 (ert-deftest python-nav-forward-sexp-safe-1 ()
1747 (python-tests-with-temp-buffer
1748 "
1749 profile = Profile.objects.create(user=request.user)
1750 profile.notify()
1751 "
1752 (python-tests-look-at "profile =")
1753 (python-nav-forward-sexp-safe 1)
1754 (should (looking-at "$"))
1755 (beginning-of-line 1)
1756 (python-tests-look-at "user=request.user")
1757 (python-nav-forward-sexp-safe -1)
1758 (should (looking-at "(user=request.user)"))
1759 (python-nav-forward-sexp-safe -4)
1760 (should (looking-at "profile ="))
1761 (python-tests-look-at "user=request.user")
1762 (python-nav-forward-sexp-safe 3)
1763 (should (looking-at ")"))
1764 (python-nav-forward-sexp-safe 1)
1765 (should (looking-at "$"))
1766 (python-nav-forward-sexp-safe 1)
1767 (should (looking-at "$"))))
1768
1769 (ert-deftest python-nav-up-list-1 ()
1770 (python-tests-with-temp-buffer
1771 "
1772 def f():
1773 if True:
1774 return [i for i in range(3)]
1775 "
1776 (python-tests-look-at "3)]")
1777 (python-nav-up-list)
1778 (should (looking-at "]"))
1779 (python-nav-up-list)
1780 (should (looking-at "$"))))
1781
1782 (ert-deftest python-nav-backward-up-list-1 ()
1783 :expected-result :failed
1784 (python-tests-with-temp-buffer
1785 "
1786 def f():
1787 if True:
1788 return [i for i in range(3)]
1789 "
1790 (python-tests-look-at "3)]")
1791 (python-nav-backward-up-list)
1792 (should (looking-at "(3)\\]"))
1793 (python-nav-backward-up-list)
1794 (should (looking-at
1795 "\\[i for i in range(3)\\]"))
1796 ;; FIXME: Need to move to beginning-of-statement.
1797 (python-nav-backward-up-list)
1798 (should (looking-at
1799 "return \\[i for i in range(3)\\]"))
1800 (python-nav-backward-up-list)
1801 (should (looking-at "if True:"))
1802 (python-nav-backward-up-list)
1803 (should (looking-at "def f():"))))
1804
1805 \f
1806 ;;; Shell integration
1807
1808 (defvar python-tests-shell-interpreter "python")
1809
1810 (ert-deftest python-shell-get-process-name-1 ()
1811 "Check process name calculation sans `buffer-file-name'."
1812 (python-tests-with-temp-buffer
1813 ""
1814 (should (string= (python-shell-get-process-name nil)
1815 python-shell-buffer-name))
1816 (should (string= (python-shell-get-process-name t)
1817 (format "%s[%s]" python-shell-buffer-name (buffer-name))))))
1818
1819 (ert-deftest python-shell-get-process-name-2 ()
1820 "Check process name calculation with `buffer-file-name'."
1821 (python-tests-with-temp-file
1822 ""
1823 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1824 ;; should be respected.
1825 (should (string= (python-shell-get-process-name nil)
1826 python-shell-buffer-name))
1827 (should (string=
1828 (python-shell-get-process-name t)
1829 (format "%s[%s]" python-shell-buffer-name (buffer-name))))))
1830
1831 (ert-deftest python-shell-internal-get-process-name-1 ()
1832 "Check the internal process name is buffer-unique sans `buffer-file-name'."
1833 (python-tests-with-temp-buffer
1834 ""
1835 (should (string= (python-shell-internal-get-process-name)
1836 (format "%s[%s]" python-shell-internal-buffer-name (buffer-name))))))
1837
1838 (ert-deftest python-shell-internal-get-process-name-2 ()
1839 "Check the internal process name is buffer-unique with `buffer-file-name'."
1840 (python-tests-with-temp-file
1841 ""
1842 (should (string= (python-shell-internal-get-process-name)
1843 (format "%s[%s]" python-shell-internal-buffer-name (buffer-name))))))
1844
1845 (ert-deftest python-shell-calculate-command-1 ()
1846 "Check the command to execute is calculated correctly.
1847 Using `python-shell-interpreter' and
1848 `python-shell-interpreter-args'."
1849 (skip-unless (executable-find python-tests-shell-interpreter))
1850 (let ((python-shell-interpreter (executable-find
1851 python-tests-shell-interpreter))
1852 (python-shell-interpreter-args "-B"))
1853 (should (string=
1854 (format "%s %s"
1855 python-shell-interpreter
1856 python-shell-interpreter-args)
1857 (python-shell-calculate-command)))))
1858
1859 (ert-deftest python-shell-calculate-process-environment-1 ()
1860 "Test `python-shell-process-environment' modification."
1861 (let* ((python-shell-process-environment
1862 '("TESTVAR1=value1" "TESTVAR2=value2"))
1863 (process-environment
1864 (python-shell-calculate-process-environment)))
1865 (should (equal (getenv "TESTVAR1") "value1"))
1866 (should (equal (getenv "TESTVAR2") "value2"))))
1867
1868 (ert-deftest python-shell-calculate-process-environment-2 ()
1869 "Test `python-shell-extra-pythonpaths' modification."
1870 (let* ((process-environment process-environment)
1871 (original-pythonpath (setenv "PYTHONPATH" "path3"))
1872 (paths '("path1" "path2"))
1873 (python-shell-extra-pythonpaths paths)
1874 (process-environment
1875 (python-shell-calculate-process-environment)))
1876 (should (equal (getenv "PYTHONPATH")
1877 (concat
1878 (mapconcat 'identity paths path-separator)
1879 path-separator original-pythonpath)))))
1880
1881 (ert-deftest python-shell-calculate-process-environment-3 ()
1882 "Test `python-shell-virtualenv-root' modification."
1883 (let* ((original-path (or (getenv "PATH") ""))
1884 (python-shell-virtualenv-root
1885 (directory-file-name user-emacs-directory))
1886 (process-environment
1887 (python-shell-calculate-process-environment)))
1888 (should (not (getenv "PYTHONHOME")))
1889 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-root))
1890 (should (equal (getenv "PATH")
1891 (format "%s/bin%s%s"
1892 python-shell-virtualenv-root
1893 path-separator original-path)))))
1894
1895 (ert-deftest python-shell-calculate-process-environment-4 ()
1896 "Test `python-shell-unbuffered' modification."
1897 (setenv "PYTHONUNBUFFERED")
1898 (let* ((process-environment
1899 (python-shell-calculate-process-environment)))
1900 ;; Defaults to t
1901 (should python-shell-unbuffered)
1902 (should (string= (getenv "PYTHONUNBUFFERED") "1"))))
1903
1904 (ert-deftest python-shell-calculate-process-environment-5 ()
1905 (setenv "PYTHONUNBUFFERED")
1906 "Test `python-shell-unbuffered' modification."
1907 (let* ((python-shell-unbuffered nil)
1908 (process-environment
1909 (python-shell-calculate-process-environment)))
1910 (should (not (getenv "PYTHONUNBUFFERED")))))
1911
1912 (ert-deftest python-shell-calculate-exec-path-1 ()
1913 "Test `python-shell-exec-path' modification."
1914 (let* ((original-exec-path exec-path)
1915 (python-shell-exec-path '("path1" "path2"))
1916 (exec-path (python-shell-calculate-exec-path)))
1917 (should (equal
1918 exec-path
1919 (append python-shell-exec-path
1920 original-exec-path)))))
1921
1922 (ert-deftest python-shell-calculate-exec-path-2 ()
1923 "Test `python-shell-exec-path' modification."
1924 (let* ((original-exec-path exec-path)
1925 (python-shell-virtualenv-root
1926 (directory-file-name (expand-file-name user-emacs-directory)))
1927 (exec-path (python-shell-calculate-exec-path)))
1928 (should (equal
1929 exec-path
1930 (append (cons
1931 (format "%s/bin" python-shell-virtualenv-root)
1932 original-exec-path))))))
1933
1934 (ert-deftest python-shell-make-comint-1 ()
1935 "Check comint creation for global shell buffer."
1936 (skip-unless (executable-find python-tests-shell-interpreter))
1937 ;; The interpreter can get killed too quickly to allow it to clean
1938 ;; up the tempfiles that the default python-shell-setup-codes create,
1939 ;; so it leaves tempfiles behind, which is a minor irritation.
1940 (let* ((python-shell-setup-codes nil)
1941 (python-shell-interpreter
1942 (executable-find python-tests-shell-interpreter))
1943 (proc-name (python-shell-get-process-name nil))
1944 (shell-buffer
1945 (python-tests-with-temp-buffer
1946 "" (python-shell-make-comint
1947 (python-shell-calculate-command) proc-name)))
1948 (process (get-buffer-process shell-buffer)))
1949 (unwind-protect
1950 (progn
1951 (set-process-query-on-exit-flag process nil)
1952 (should (process-live-p process))
1953 (with-current-buffer shell-buffer
1954 (should (eq major-mode 'inferior-python-mode))
1955 (should (string= (buffer-name) (format "*%s*" proc-name)))))
1956 (kill-buffer shell-buffer))))
1957
1958 (ert-deftest python-shell-make-comint-2 ()
1959 "Check comint creation for internal shell buffer."
1960 (skip-unless (executable-find python-tests-shell-interpreter))
1961 (let* ((python-shell-setup-codes nil)
1962 (python-shell-interpreter
1963 (executable-find python-tests-shell-interpreter))
1964 (proc-name (python-shell-internal-get-process-name))
1965 (shell-buffer
1966 (python-tests-with-temp-buffer
1967 "" (python-shell-make-comint
1968 (python-shell-calculate-command) proc-name nil t)))
1969 (process (get-buffer-process shell-buffer)))
1970 (unwind-protect
1971 (progn
1972 (set-process-query-on-exit-flag process nil)
1973 (should (process-live-p process))
1974 (with-current-buffer shell-buffer
1975 (should (eq major-mode 'inferior-python-mode))
1976 (should (string= (buffer-name) (format " *%s*" proc-name)))))
1977 (kill-buffer shell-buffer))))
1978
1979 (ert-deftest python-shell-make-comint-3 ()
1980 "Check comint creation with overridden python interpreter and args.
1981 The command passed to `python-shell-make-comint' as argument must
1982 locally override global values set in `python-shell-interpreter'
1983 and `python-shell-interpreter-args' in the new shell buffer."
1984 (skip-unless (executable-find python-tests-shell-interpreter))
1985 (let* ((python-shell-setup-codes nil)
1986 (python-shell-interpreter "interpreter")
1987 (python-shell-interpreter-args "--some-args")
1988 (proc-name (python-shell-get-process-name nil))
1989 (interpreter-override
1990 (concat (executable-find python-tests-shell-interpreter) " " "-i"))
1991 (shell-buffer
1992 (python-tests-with-temp-buffer
1993 "" (python-shell-make-comint interpreter-override proc-name nil)))
1994 (process (get-buffer-process shell-buffer)))
1995 (unwind-protect
1996 (progn
1997 (set-process-query-on-exit-flag process nil)
1998 (should (process-live-p process))
1999 (with-current-buffer shell-buffer
2000 (should (eq major-mode 'inferior-python-mode))
2001 (should (file-equal-p
2002 python-shell-interpreter
2003 (executable-find python-tests-shell-interpreter)))
2004 (should (string= python-shell-interpreter-args "-i"))))
2005 (kill-buffer shell-buffer))))
2006
2007 (ert-deftest python-shell-make-comint-4 ()
2008 "Check shell calculated prompts regexps are set."
2009 (skip-unless (executable-find python-tests-shell-interpreter))
2010 (let* ((process-environment process-environment)
2011 (python-shell-setup-codes nil)
2012 (python-shell-interpreter
2013 (executable-find python-tests-shell-interpreter))
2014 (python-shell-interpreter-args "-i")
2015 (python-shell--prompt-calculated-input-regexp nil)
2016 (python-shell--prompt-calculated-output-regexp nil)
2017 (python-shell-prompt-detect-enabled t)
2018 (python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2019 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2020 (python-shell-prompt-regexp "in")
2021 (python-shell-prompt-block-regexp "block")
2022 (python-shell-prompt-pdb-regexp "pdf")
2023 (python-shell-prompt-output-regexp "output")
2024 (startup-code (concat "import sys\n"
2025 "sys.ps1 = 'py> '\n"
2026 "sys.ps2 = '..> '\n"
2027 "sys.ps3 = 'out '\n"))
2028 (startup-file (python-shell--save-temp-file startup-code))
2029 (proc-name (python-shell-get-process-name nil))
2030 (shell-buffer
2031 (progn
2032 (setenv "PYTHONSTARTUP" startup-file)
2033 (python-tests-with-temp-buffer
2034 "" (python-shell-make-comint
2035 (python-shell-calculate-command) proc-name nil))))
2036 (process (get-buffer-process shell-buffer)))
2037 (unwind-protect
2038 (progn
2039 (set-process-query-on-exit-flag process nil)
2040 (should (process-live-p process))
2041 (with-current-buffer shell-buffer
2042 (should (eq major-mode 'inferior-python-mode))
2043 (should (string=
2044 python-shell--prompt-calculated-input-regexp
2045 (concat "^\\(extralargeinputprompt\\|\\.\\.> \\|"
2046 "block\\|py> \\|pdf\\|sml\\|in\\)")))
2047 (should (string=
2048 python-shell--prompt-calculated-output-regexp
2049 "^\\(extralargeoutputprompt\\|output\\|out \\|sml\\)"))))
2050 (delete-file startup-file)
2051 (kill-buffer shell-buffer))))
2052
2053 (ert-deftest python-shell-get-process-1 ()
2054 "Check dedicated shell process preference over global."
2055 (skip-unless (executable-find python-tests-shell-interpreter))
2056 (python-tests-with-temp-file
2057 ""
2058 (let* ((python-shell-setup-codes nil)
2059 (python-shell-interpreter
2060 (executable-find python-tests-shell-interpreter))
2061 (global-proc-name (python-shell-get-process-name nil))
2062 (dedicated-proc-name (python-shell-get-process-name t))
2063 (global-shell-buffer
2064 (python-shell-make-comint
2065 (python-shell-calculate-command) global-proc-name))
2066 (dedicated-shell-buffer
2067 (python-shell-make-comint
2068 (python-shell-calculate-command) dedicated-proc-name))
2069 (global-process (get-buffer-process global-shell-buffer))
2070 (dedicated-process (get-buffer-process dedicated-shell-buffer)))
2071 (unwind-protect
2072 (progn
2073 (set-process-query-on-exit-flag global-process nil)
2074 (set-process-query-on-exit-flag dedicated-process nil)
2075 ;; Prefer dedicated if global also exists.
2076 (should (equal (python-shell-get-process) dedicated-process))
2077 (kill-buffer dedicated-shell-buffer)
2078 ;; If there's only global, use it.
2079 (should (equal (python-shell-get-process) global-process))
2080 (kill-buffer global-shell-buffer)
2081 ;; No buffer available.
2082 (should (not (python-shell-get-process))))
2083 (ignore-errors (kill-buffer global-shell-buffer))
2084 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
2085
2086 (ert-deftest python-shell-get-or-create-process-1 ()
2087 "Check shell dedicated process creation."
2088 (skip-unless (executable-find python-tests-shell-interpreter))
2089 (python-tests-with-temp-file
2090 ""
2091 (let* ((cmd
2092 (concat (executable-find python-tests-shell-interpreter) " -i"))
2093 (use-dialog-box)
2094 (dedicated-process-name (python-shell-get-process-name t))
2095 (dedicated-process (python-shell-get-or-create-process cmd t))
2096 (dedicated-shell-buffer (process-buffer dedicated-process)))
2097 (unwind-protect
2098 (progn
2099 (set-process-query-on-exit-flag dedicated-process nil)
2100 ;; should be dedicated.
2101 (should (equal (process-name dedicated-process)
2102 dedicated-process-name))
2103 (kill-buffer dedicated-shell-buffer)
2104 ;; Check there are no processes for current buffer.
2105 (should (not (python-shell-get-process))))
2106 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
2107
2108 (ert-deftest python-shell-get-or-create-process-2 ()
2109 "Check shell global process creation."
2110 (skip-unless (executable-find python-tests-shell-interpreter))
2111 (python-tests-with-temp-file
2112 ""
2113 (let* ((cmd
2114 (concat (executable-find python-tests-shell-interpreter) " -i"))
2115 (use-dialog-box)
2116 (process-name (python-shell-get-process-name nil))
2117 (process (python-shell-get-or-create-process cmd))
2118 (shell-buffer (process-buffer process)))
2119 (unwind-protect
2120 (progn
2121 (set-process-query-on-exit-flag process nil)
2122 ;; should be global.
2123 (should (equal (process-name process) process-name))
2124 (kill-buffer shell-buffer)
2125 ;; Check there are no processes for current buffer.
2126 (should (not (python-shell-get-process))))
2127 (ignore-errors (kill-buffer shell-buffer))))))
2128
2129 (ert-deftest python-shell-get-or-create-process-3 ()
2130 "Check shell dedicated/global process preference."
2131 (skip-unless (executable-find python-tests-shell-interpreter))
2132 (python-tests-with-temp-file
2133 ""
2134 (let* ((cmd
2135 (concat (executable-find python-tests-shell-interpreter) " -i"))
2136 (python-shell-interpreter python-tests-shell-interpreter)
2137 (use-dialog-box)
2138 (dedicated-process-name (python-shell-get-process-name t))
2139 (global-process)
2140 (dedicated-process))
2141 (progn
2142 ;; Create global process
2143 (run-python cmd nil)
2144 (setq global-process (get-buffer-process "*Python*"))
2145 (should global-process)
2146 (set-process-query-on-exit-flag global-process nil)
2147 ;; Create dedicated process
2148 (run-python cmd t)
2149 (setq dedicated-process (get-process dedicated-process-name))
2150 (should dedicated-process)
2151 (set-process-query-on-exit-flag dedicated-process nil)
2152 ;; Prefer dedicated.
2153 (should (equal (python-shell-get-or-create-process)
2154 dedicated-process))
2155 ;; Kill the dedicated so the global takes over.
2156 (kill-buffer (process-buffer dedicated-process))
2157 ;; Detect global.
2158 (should (equal (python-shell-get-or-create-process) global-process))
2159 ;; Kill the global.
2160 (kill-buffer (process-buffer global-process))
2161 ;; Check there are no processes for current buffer.
2162 (should (not (python-shell-get-process)))))))
2163
2164 (ert-deftest python-shell-internal-get-or-create-process-1 ()
2165 "Check internal shell process creation fallback."
2166 (skip-unless (executable-find python-tests-shell-interpreter))
2167 (python-tests-with-temp-file
2168 ""
2169 (should (not (process-live-p (python-shell-internal-get-process-name))))
2170 (let* ((python-shell-interpreter
2171 (executable-find python-tests-shell-interpreter))
2172 (internal-process-name (python-shell-internal-get-process-name))
2173 (internal-process (python-shell-internal-get-or-create-process))
2174 (internal-shell-buffer (process-buffer internal-process)))
2175 (unwind-protect
2176 (progn
2177 (set-process-query-on-exit-flag internal-process nil)
2178 (should (equal (process-name internal-process)
2179 internal-process-name))
2180 (should (equal internal-process
2181 (python-shell-internal-get-or-create-process)))
2182 ;; Assert the internal process is not a user process
2183 (should (not (python-shell-get-process)))
2184 (kill-buffer internal-shell-buffer))
2185 (ignore-errors (kill-buffer internal-shell-buffer))))))
2186
2187 (ert-deftest python-shell-prompt-detect-1 ()
2188 "Check prompt autodetection."
2189 (skip-unless (executable-find python-tests-shell-interpreter))
2190 (let ((process-environment process-environment))
2191 ;; Ensure no startup file is enabled
2192 (setenv "PYTHONSTARTUP" "")
2193 (should python-shell-prompt-detect-enabled)
2194 (should (equal (python-shell-prompt-detect) '(">>> " "... " "")))))
2195
2196 (ert-deftest python-shell-prompt-detect-2 ()
2197 "Check prompt autodetection with startup file. Bug#17370."
2198 (skip-unless (executable-find python-tests-shell-interpreter))
2199 (let* ((process-environment process-environment)
2200 (startup-code (concat "import sys\n"
2201 "sys.ps1 = 'py> '\n"
2202 "sys.ps2 = '..> '\n"
2203 "sys.ps3 = 'out '\n"))
2204 (startup-file (python-shell--save-temp-file startup-code)))
2205 (unwind-protect
2206 (progn
2207 ;; Ensure startup file is enabled
2208 (setenv "PYTHONSTARTUP" startup-file)
2209 (should python-shell-prompt-detect-enabled)
2210 (should (equal (python-shell-prompt-detect) '("py> " "..> " "out "))))
2211 (ignore-errors (delete-file startup-file)))))
2212
2213 (ert-deftest python-shell-prompt-detect-3 ()
2214 "Check prompts are not autodetected when feature is disabled."
2215 (skip-unless (executable-find python-tests-shell-interpreter))
2216 (let ((process-environment process-environment)
2217 (python-shell-prompt-detect-enabled nil))
2218 ;; Ensure no startup file is enabled
2219 (should (not python-shell-prompt-detect-enabled))
2220 (should (not (python-shell-prompt-detect)))))
2221
2222 (ert-deftest python-shell-prompt-detect-4 ()
2223 "Check warning is shown when detection fails."
2224 (skip-unless (executable-find python-tests-shell-interpreter))
2225 (let* ((process-environment process-environment)
2226 ;; Trigger failure by removing prompts in the startup file
2227 (startup-code (concat "import sys\n"
2228 "sys.ps1 = ''\n"
2229 "sys.ps2 = ''\n"
2230 "sys.ps3 = ''\n"))
2231 (startup-file (python-shell--save-temp-file startup-code)))
2232 (unwind-protect
2233 (progn
2234 (kill-buffer (get-buffer-create "*Warnings*"))
2235 (should (not (get-buffer "*Warnings*")))
2236 (setenv "PYTHONSTARTUP" startup-file)
2237 (should python-shell-prompt-detect-failure-warning)
2238 (should python-shell-prompt-detect-enabled)
2239 (should (not (python-shell-prompt-detect)))
2240 (should (get-buffer "*Warnings*")))
2241 (ignore-errors (delete-file startup-file)))))
2242
2243 (ert-deftest python-shell-prompt-detect-5 ()
2244 "Check disabled warnings are not shown when detection fails."
2245 (skip-unless (executable-find python-tests-shell-interpreter))
2246 (let* ((process-environment process-environment)
2247 (startup-code (concat "import sys\n"
2248 "sys.ps1 = ''\n"
2249 "sys.ps2 = ''\n"
2250 "sys.ps3 = ''\n"))
2251 (startup-file (python-shell--save-temp-file startup-code))
2252 (python-shell-prompt-detect-failure-warning nil))
2253 (unwind-protect
2254 (progn
2255 (kill-buffer (get-buffer-create "*Warnings*"))
2256 (should (not (get-buffer "*Warnings*")))
2257 (setenv "PYTHONSTARTUP" startup-file)
2258 (should (not python-shell-prompt-detect-failure-warning))
2259 (should python-shell-prompt-detect-enabled)
2260 (should (not (python-shell-prompt-detect)))
2261 (should (not (get-buffer "*Warnings*"))))
2262 (ignore-errors (delete-file startup-file)))))
2263
2264 (ert-deftest python-shell-prompt-detect-6 ()
2265 "Warnings are not shown when detection is disabled."
2266 (skip-unless (executable-find python-tests-shell-interpreter))
2267 (let* ((process-environment process-environment)
2268 (startup-code (concat "import sys\n"
2269 "sys.ps1 = ''\n"
2270 "sys.ps2 = ''\n"
2271 "sys.ps3 = ''\n"))
2272 (startup-file (python-shell--save-temp-file startup-code))
2273 (python-shell-prompt-detect-failure-warning t)
2274 (python-shell-prompt-detect-enabled nil))
2275 (unwind-protect
2276 (progn
2277 (kill-buffer (get-buffer-create "*Warnings*"))
2278 (should (not (get-buffer "*Warnings*")))
2279 (setenv "PYTHONSTARTUP" startup-file)
2280 (should python-shell-prompt-detect-failure-warning)
2281 (should (not python-shell-prompt-detect-enabled))
2282 (should (not (python-shell-prompt-detect)))
2283 (should (not (get-buffer "*Warnings*"))))
2284 (ignore-errors (delete-file startup-file)))))
2285
2286 (ert-deftest python-shell-prompt-validate-regexps-1 ()
2287 "Check `python-shell-prompt-input-regexps' are validated."
2288 (let* ((python-shell-prompt-input-regexps '("\\("))
2289 (error-data (should-error (python-shell-prompt-validate-regexps)
2290 :type 'user-error)))
2291 (should
2292 (string= (cadr error-data)
2293 "Invalid regexp \\( in `python-shell-prompt-input-regexps'"))))
2294
2295 (ert-deftest python-shell-prompt-validate-regexps-2 ()
2296 "Check `python-shell-prompt-output-regexps' are validated."
2297 (let* ((python-shell-prompt-output-regexps '("\\("))
2298 (error-data (should-error (python-shell-prompt-validate-regexps)
2299 :type 'user-error)))
2300 (should
2301 (string= (cadr error-data)
2302 "Invalid regexp \\( in `python-shell-prompt-output-regexps'"))))
2303
2304 (ert-deftest python-shell-prompt-validate-regexps-3 ()
2305 "Check `python-shell-prompt-regexp' is validated."
2306 (let* ((python-shell-prompt-regexp "\\(")
2307 (error-data (should-error (python-shell-prompt-validate-regexps)
2308 :type 'user-error)))
2309 (should
2310 (string= (cadr error-data)
2311 "Invalid regexp \\( in `python-shell-prompt-regexp'"))))
2312
2313 (ert-deftest python-shell-prompt-validate-regexps-4 ()
2314 "Check `python-shell-prompt-block-regexp' is validated."
2315 (let* ((python-shell-prompt-block-regexp "\\(")
2316 (error-data (should-error (python-shell-prompt-validate-regexps)
2317 :type 'user-error)))
2318 (should
2319 (string= (cadr error-data)
2320 "Invalid regexp \\( in `python-shell-prompt-block-regexp'"))))
2321
2322 (ert-deftest python-shell-prompt-validate-regexps-5 ()
2323 "Check `python-shell-prompt-pdb-regexp' is validated."
2324 (let* ((python-shell-prompt-pdb-regexp "\\(")
2325 (error-data (should-error (python-shell-prompt-validate-regexps)
2326 :type 'user-error)))
2327 (should
2328 (string= (cadr error-data)
2329 "Invalid regexp \\( in `python-shell-prompt-pdb-regexp'"))))
2330
2331 (ert-deftest python-shell-prompt-validate-regexps-6 ()
2332 "Check `python-shell-prompt-output-regexp' is validated."
2333 (let* ((python-shell-prompt-output-regexp "\\(")
2334 (error-data (should-error (python-shell-prompt-validate-regexps)
2335 :type 'user-error)))
2336 (should
2337 (string= (cadr error-data)
2338 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2339
2340 (ert-deftest python-shell-prompt-validate-regexps-7 ()
2341 "Check default regexps are valid."
2342 ;; should not signal error
2343 (python-shell-prompt-validate-regexps))
2344
2345 (ert-deftest python-shell-prompt-set-calculated-regexps-1 ()
2346 "Check regexps are validated."
2347 (let* ((python-shell-prompt-output-regexp '("\\("))
2348 (python-shell--prompt-calculated-input-regexp nil)
2349 (python-shell--prompt-calculated-output-regexp nil)
2350 (python-shell-prompt-detect-enabled nil)
2351 (error-data (should-error (python-shell-prompt-set-calculated-regexps)
2352 :type 'user-error)))
2353 (should
2354 (string= (cadr error-data)
2355 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2356
2357 (ert-deftest python-shell-prompt-set-calculated-regexps-2 ()
2358 "Check `python-shell-prompt-input-regexps' are set."
2359 (let* ((python-shell-prompt-input-regexps '("my" "prompt"))
2360 (python-shell-prompt-output-regexps '(""))
2361 (python-shell-prompt-regexp "")
2362 (python-shell-prompt-block-regexp "")
2363 (python-shell-prompt-pdb-regexp "")
2364 (python-shell-prompt-output-regexp "")
2365 (python-shell--prompt-calculated-input-regexp nil)
2366 (python-shell--prompt-calculated-output-regexp nil)
2367 (python-shell-prompt-detect-enabled nil))
2368 (python-shell-prompt-set-calculated-regexps)
2369 (should (string= python-shell--prompt-calculated-input-regexp
2370 "^\\(prompt\\|my\\|\\)"))))
2371
2372 (ert-deftest python-shell-prompt-set-calculated-regexps-3 ()
2373 "Check `python-shell-prompt-output-regexps' are set."
2374 (let* ((python-shell-prompt-input-regexps '(""))
2375 (python-shell-prompt-output-regexps '("my" "prompt"))
2376 (python-shell-prompt-regexp "")
2377 (python-shell-prompt-block-regexp "")
2378 (python-shell-prompt-pdb-regexp "")
2379 (python-shell-prompt-output-regexp "")
2380 (python-shell--prompt-calculated-input-regexp nil)
2381 (python-shell--prompt-calculated-output-regexp nil)
2382 (python-shell-prompt-detect-enabled nil))
2383 (python-shell-prompt-set-calculated-regexps)
2384 (should (string= python-shell--prompt-calculated-output-regexp
2385 "^\\(prompt\\|my\\|\\)"))))
2386
2387 (ert-deftest python-shell-prompt-set-calculated-regexps-4 ()
2388 "Check user defined prompts are set."
2389 (let* ((python-shell-prompt-input-regexps '(""))
2390 (python-shell-prompt-output-regexps '(""))
2391 (python-shell-prompt-regexp "prompt")
2392 (python-shell-prompt-block-regexp "block")
2393 (python-shell-prompt-pdb-regexp "pdb")
2394 (python-shell-prompt-output-regexp "output")
2395 (python-shell--prompt-calculated-input-regexp nil)
2396 (python-shell--prompt-calculated-output-regexp nil)
2397 (python-shell-prompt-detect-enabled nil))
2398 (python-shell-prompt-set-calculated-regexps)
2399 (should (string= python-shell--prompt-calculated-input-regexp
2400 "^\\(prompt\\|block\\|pdb\\|\\)"))
2401 (should (string= python-shell--prompt-calculated-output-regexp
2402 "^\\(output\\|\\)"))))
2403
2404 (ert-deftest python-shell-prompt-set-calculated-regexps-5 ()
2405 "Check order of regexps (larger first)."
2406 (let* ((python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2407 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2408 (python-shell-prompt-regexp "in")
2409 (python-shell-prompt-block-regexp "block")
2410 (python-shell-prompt-pdb-regexp "pdf")
2411 (python-shell-prompt-output-regexp "output")
2412 (python-shell--prompt-calculated-input-regexp nil)
2413 (python-shell--prompt-calculated-output-regexp nil)
2414 (python-shell-prompt-detect-enabled nil))
2415 (python-shell-prompt-set-calculated-regexps)
2416 (should (string= python-shell--prompt-calculated-input-regexp
2417 "^\\(extralargeinputprompt\\|block\\|pdf\\|sml\\|in\\)"))
2418 (should (string= python-shell--prompt-calculated-output-regexp
2419 "^\\(extralargeoutputprompt\\|output\\|sml\\)"))))
2420
2421 (ert-deftest python-shell-prompt-set-calculated-regexps-6 ()
2422 "Check detected prompts are included `regexp-quote'd."
2423 (skip-unless (executable-find python-tests-shell-interpreter))
2424 (let* ((python-shell-prompt-input-regexps '(""))
2425 (python-shell-prompt-output-regexps '(""))
2426 (python-shell-prompt-regexp "")
2427 (python-shell-prompt-block-regexp "")
2428 (python-shell-prompt-pdb-regexp "")
2429 (python-shell-prompt-output-regexp "")
2430 (python-shell--prompt-calculated-input-regexp nil)
2431 (python-shell--prompt-calculated-output-regexp nil)
2432 (python-shell-prompt-detect-enabled t)
2433 (process-environment process-environment)
2434 (startup-code (concat "import sys\n"
2435 "sys.ps1 = 'p.> '\n"
2436 "sys.ps2 = '..> '\n"
2437 "sys.ps3 = 'o.t '\n"))
2438 (startup-file (python-shell--save-temp-file startup-code)))
2439 (unwind-protect
2440 (progn
2441 (setenv "PYTHONSTARTUP" startup-file)
2442 (python-shell-prompt-set-calculated-regexps)
2443 (should (string= python-shell--prompt-calculated-input-regexp
2444 "^\\(\\.\\.> \\|p\\.> \\|\\)"))
2445 (should (string= python-shell--prompt-calculated-output-regexp
2446 "^\\(o\\.t \\|\\)")))
2447 (ignore-errors (delete-file startup-file)))))
2448
2449 (ert-deftest python-shell-buffer-substring-1 ()
2450 "Selecting a substring of the whole buffer must match its contents."
2451 (python-tests-with-temp-buffer
2452 "
2453 class Foo(models.Model):
2454 pass
2455
2456
2457 class Bar(models.Model):
2458 pass
2459 "
2460 (should (string= (buffer-string)
2461 (python-shell-buffer-substring (point-min) (point-max))))))
2462
2463 (ert-deftest python-shell-buffer-substring-2 ()
2464 "Main block should be removed if NOMAIN is non-nil."
2465 (python-tests-with-temp-buffer
2466 "
2467 class Foo(models.Model):
2468 pass
2469
2470 class Bar(models.Model):
2471 pass
2472
2473 if __name__ == \"__main__\":
2474 foo = Foo()
2475 print (foo)
2476 "
2477 (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
2478 "
2479 class Foo(models.Model):
2480 pass
2481
2482 class Bar(models.Model):
2483 pass
2484
2485
2486
2487
2488 "))))
2489
2490 (ert-deftest python-shell-buffer-substring-3 ()
2491 "Main block should be removed if NOMAIN is non-nil."
2492 (python-tests-with-temp-buffer
2493 "
2494 class Foo(models.Model):
2495 pass
2496
2497 if __name__ == \"__main__\":
2498 foo = Foo()
2499 print (foo)
2500
2501 class Bar(models.Model):
2502 pass
2503 "
2504 (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
2505 "
2506 class Foo(models.Model):
2507 pass
2508
2509
2510
2511
2512
2513 class Bar(models.Model):
2514 pass
2515 "))))
2516
2517 (ert-deftest python-shell-buffer-substring-4 ()
2518 "Coding cookie should be added for substrings."
2519 (python-tests-with-temp-buffer
2520 "# coding: latin-1
2521
2522 class Foo(models.Model):
2523 pass
2524
2525 if __name__ == \"__main__\":
2526 foo = Foo()
2527 print (foo)
2528
2529 class Bar(models.Model):
2530 pass
2531 "
2532 (should (string= (python-shell-buffer-substring
2533 (python-tests-look-at "class Foo(models.Model):")
2534 (progn (python-nav-forward-sexp) (point)))
2535 "# -*- coding: latin-1 -*-
2536
2537 class Foo(models.Model):
2538 pass"))))
2539
2540 (ert-deftest python-shell-buffer-substring-5 ()
2541 "The proper amount of blank lines is added for a substring."
2542 (python-tests-with-temp-buffer
2543 "# coding: latin-1
2544
2545 class Foo(models.Model):
2546 pass
2547
2548 if __name__ == \"__main__\":
2549 foo = Foo()
2550 print (foo)
2551
2552 class Bar(models.Model):
2553 pass
2554 "
2555 (should (string= (python-shell-buffer-substring
2556 (python-tests-look-at "class Bar(models.Model):")
2557 (progn (python-nav-forward-sexp) (point)))
2558 "# -*- coding: latin-1 -*-
2559
2560
2561
2562
2563
2564
2565
2566
2567 class Bar(models.Model):
2568 pass"))))
2569
2570 (ert-deftest python-shell-buffer-substring-6 ()
2571 "Handle substring with coding cookie in the second line."
2572 (python-tests-with-temp-buffer
2573 "
2574 # coding: latin-1
2575
2576 class Foo(models.Model):
2577 pass
2578
2579 if __name__ == \"__main__\":
2580 foo = Foo()
2581 print (foo)
2582
2583 class Bar(models.Model):
2584 pass
2585 "
2586 (should (string= (python-shell-buffer-substring
2587 (python-tests-look-at "# coding: latin-1")
2588 (python-tests-look-at "if __name__ == \"__main__\":"))
2589 "# -*- coding: latin-1 -*-
2590
2591
2592 class Foo(models.Model):
2593 pass
2594
2595 "))))
2596
2597 (ert-deftest python-shell-buffer-substring-7 ()
2598 "Ensure first coding cookie gets precedence."
2599 (python-tests-with-temp-buffer
2600 "# coding: utf-8
2601 # coding: latin-1
2602
2603 class Foo(models.Model):
2604 pass
2605
2606 if __name__ == \"__main__\":
2607 foo = Foo()
2608 print (foo)
2609
2610 class Bar(models.Model):
2611 pass
2612 "
2613 (should (string= (python-shell-buffer-substring
2614 (python-tests-look-at "# coding: latin-1")
2615 (python-tests-look-at "if __name__ == \"__main__\":"))
2616 "# -*- coding: utf-8 -*-
2617
2618
2619 class Foo(models.Model):
2620 pass
2621
2622 "))))
2623
2624 (ert-deftest python-shell-buffer-substring-8 ()
2625 "Ensure first coding cookie gets precedence when sending whole buffer."
2626 (python-tests-with-temp-buffer
2627 "# coding: utf-8
2628 # coding: latin-1
2629
2630 class Foo(models.Model):
2631 pass
2632 "
2633 (should (string= (python-shell-buffer-substring (point-min) (point-max))
2634 "# coding: utf-8
2635
2636
2637 class Foo(models.Model):
2638 pass
2639 "))))
2640
2641 (ert-deftest python-shell-buffer-substring-9 ()
2642 "Check substring starting from `point-min'."
2643 (python-tests-with-temp-buffer
2644 "# coding: utf-8
2645
2646 class Foo(models.Model):
2647 pass
2648
2649 class Bar(models.Model):
2650 pass
2651 "
2652 (should (string= (python-shell-buffer-substring
2653 (point-min)
2654 (python-tests-look-at "class Bar(models.Model):"))
2655 "# coding: utf-8
2656
2657 class Foo(models.Model):
2658 pass
2659
2660 "))))
2661
2662 \f
2663 ;;; Shell completion
2664
2665 \f
2666 ;;; PDB Track integration
2667
2668 \f
2669 ;;; Symbol completion
2670
2671 \f
2672 ;;; Fill paragraph
2673
2674 \f
2675 ;;; Skeletons
2676
2677 \f
2678 ;;; FFAP
2679
2680 \f
2681 ;;; Code check
2682
2683 \f
2684 ;;; Eldoc
2685
2686 \f
2687 ;;; Imenu
2688
2689 (ert-deftest python-imenu-create-index-1 ()
2690 (python-tests-with-temp-buffer
2691 "
2692 class Foo(models.Model):
2693 pass
2694
2695
2696 class Bar(models.Model):
2697 pass
2698
2699
2700 def decorator(arg1, arg2, arg3):
2701 '''print decorated function call data to stdout.
2702
2703 Usage:
2704
2705 @decorator('arg1', 'arg2')
2706 def func(a, b, c=True):
2707 pass
2708 '''
2709
2710 def wrap(f):
2711 print ('wrap')
2712 def wrapped_f(*args):
2713 print ('wrapped_f')
2714 print ('Decorator arguments:', arg1, arg2, arg3)
2715 f(*args)
2716 print ('called f(*args)')
2717 return wrapped_f
2718 return wrap
2719
2720
2721 class Baz(object):
2722
2723 def a(self):
2724 pass
2725
2726 def b(self):
2727 pass
2728
2729 class Frob(object):
2730
2731 def c(self):
2732 pass
2733 "
2734 (goto-char (point-max))
2735 (should (equal
2736 (list
2737 (cons "Foo (class)" (copy-marker 2))
2738 (cons "Bar (class)" (copy-marker 38))
2739 (list
2740 "decorator (def)"
2741 (cons "*function definition*" (copy-marker 74))
2742 (list
2743 "wrap (def)"
2744 (cons "*function definition*" (copy-marker 254))
2745 (cons "wrapped_f (def)" (copy-marker 294))))
2746 (list
2747 "Baz (class)"
2748 (cons "*class definition*" (copy-marker 519))
2749 (cons "a (def)" (copy-marker 539))
2750 (cons "b (def)" (copy-marker 570))
2751 (list
2752 "Frob (class)"
2753 (cons "*class definition*" (copy-marker 601))
2754 (cons "c (def)" (copy-marker 626)))))
2755 (python-imenu-create-index)))))
2756
2757 (ert-deftest python-imenu-create-index-2 ()
2758 (python-tests-with-temp-buffer
2759 "
2760 class Foo(object):
2761 def foo(self):
2762 def foo1():
2763 pass
2764
2765 def foobar(self):
2766 pass
2767 "
2768 (goto-char (point-max))
2769 (should (equal
2770 (list
2771 (list
2772 "Foo (class)"
2773 (cons "*class definition*" (copy-marker 2))
2774 (list
2775 "foo (def)"
2776 (cons "*function definition*" (copy-marker 21))
2777 (cons "foo1 (def)" (copy-marker 40)))
2778 (cons "foobar (def)" (copy-marker 78))))
2779 (python-imenu-create-index)))))
2780
2781 (ert-deftest python-imenu-create-index-3 ()
2782 (python-tests-with-temp-buffer
2783 "
2784 class Foo(object):
2785 def foo(self):
2786 def foo1():
2787 pass
2788 def foo2():
2789 pass
2790 "
2791 (goto-char (point-max))
2792 (should (equal
2793 (list
2794 (list
2795 "Foo (class)"
2796 (cons "*class definition*" (copy-marker 2))
2797 (list
2798 "foo (def)"
2799 (cons "*function definition*" (copy-marker 21))
2800 (cons "foo1 (def)" (copy-marker 40))
2801 (cons "foo2 (def)" (copy-marker 77)))))
2802 (python-imenu-create-index)))))
2803
2804 (ert-deftest python-imenu-create-index-4 ()
2805 (python-tests-with-temp-buffer
2806 "
2807 class Foo(object):
2808 class Bar(object):
2809 def __init__(self):
2810 pass
2811
2812 def __str__(self):
2813 pass
2814
2815 def __init__(self):
2816 pass
2817 "
2818 (goto-char (point-max))
2819 (should (equal
2820 (list
2821 (list
2822 "Foo (class)"
2823 (cons "*class definition*" (copy-marker 2))
2824 (list
2825 "Bar (class)"
2826 (cons "*class definition*" (copy-marker 21))
2827 (cons "__init__ (def)" (copy-marker 44))
2828 (cons "__str__ (def)" (copy-marker 90)))
2829 (cons "__init__ (def)" (copy-marker 135))))
2830 (python-imenu-create-index)))))
2831
2832 (ert-deftest python-imenu-create-flat-index-1 ()
2833 (python-tests-with-temp-buffer
2834 "
2835 class Foo(models.Model):
2836 pass
2837
2838
2839 class Bar(models.Model):
2840 pass
2841
2842
2843 def decorator(arg1, arg2, arg3):
2844 '''print decorated function call data to stdout.
2845
2846 Usage:
2847
2848 @decorator('arg1', 'arg2')
2849 def func(a, b, c=True):
2850 pass
2851 '''
2852
2853 def wrap(f):
2854 print ('wrap')
2855 def wrapped_f(*args):
2856 print ('wrapped_f')
2857 print ('Decorator arguments:', arg1, arg2, arg3)
2858 f(*args)
2859 print ('called f(*args)')
2860 return wrapped_f
2861 return wrap
2862
2863
2864 class Baz(object):
2865
2866 def a(self):
2867 pass
2868
2869 def b(self):
2870 pass
2871
2872 class Frob(object):
2873
2874 def c(self):
2875 pass
2876 "
2877 (goto-char (point-max))
2878 (should (equal
2879 (list (cons "Foo" (copy-marker 2))
2880 (cons "Bar" (copy-marker 38))
2881 (cons "decorator" (copy-marker 74))
2882 (cons "decorator.wrap" (copy-marker 254))
2883 (cons "decorator.wrap.wrapped_f" (copy-marker 294))
2884 (cons "Baz" (copy-marker 519))
2885 (cons "Baz.a" (copy-marker 539))
2886 (cons "Baz.b" (copy-marker 570))
2887 (cons "Baz.Frob" (copy-marker 601))
2888 (cons "Baz.Frob.c" (copy-marker 626)))
2889 (python-imenu-create-flat-index)))))
2890
2891 (ert-deftest python-imenu-create-flat-index-2 ()
2892 (python-tests-with-temp-buffer
2893 "
2894 class Foo(object):
2895 class Bar(object):
2896 def __init__(self):
2897 pass
2898
2899 def __str__(self):
2900 pass
2901
2902 def __init__(self):
2903 pass
2904 "
2905 (goto-char (point-max))
2906 (should (equal
2907 (list
2908 (cons "Foo" (copy-marker 2))
2909 (cons "Foo.Bar" (copy-marker 21))
2910 (cons "Foo.Bar.__init__" (copy-marker 44))
2911 (cons "Foo.Bar.__str__" (copy-marker 90))
2912 (cons "Foo.__init__" (copy-marker 135)))
2913 (python-imenu-create-flat-index)))))
2914
2915 \f
2916 ;;; Misc helpers
2917
2918 (ert-deftest python-info-current-defun-1 ()
2919 (python-tests-with-temp-buffer
2920 "
2921 def foo(a, b):
2922 "
2923 (forward-line 1)
2924 (should (string= "foo" (python-info-current-defun)))
2925 (should (string= "def foo" (python-info-current-defun t)))
2926 (forward-line 1)
2927 (should (not (python-info-current-defun)))
2928 (indent-for-tab-command)
2929 (should (string= "foo" (python-info-current-defun)))
2930 (should (string= "def foo" (python-info-current-defun t)))))
2931
2932 (ert-deftest python-info-current-defun-2 ()
2933 (python-tests-with-temp-buffer
2934 "
2935 class C(object):
2936
2937 def m(self):
2938 if True:
2939 return [i for i in range(3)]
2940 else:
2941 return []
2942
2943 def b():
2944 do_b()
2945
2946 def a():
2947 do_a()
2948
2949 def c(self):
2950 do_c()
2951 "
2952 (forward-line 1)
2953 (should (string= "C" (python-info-current-defun)))
2954 (should (string= "class C" (python-info-current-defun t)))
2955 (python-tests-look-at "return [i for ")
2956 (should (string= "C.m" (python-info-current-defun)))
2957 (should (string= "def C.m" (python-info-current-defun t)))
2958 (python-tests-look-at "def b():")
2959 (should (string= "C.m.b" (python-info-current-defun)))
2960 (should (string= "def C.m.b" (python-info-current-defun t)))
2961 (forward-line 2)
2962 (indent-for-tab-command)
2963 (python-indent-dedent-line-backspace 1)
2964 (should (string= "C.m" (python-info-current-defun)))
2965 (should (string= "def C.m" (python-info-current-defun t)))
2966 (python-tests-look-at "def c(self):")
2967 (forward-line -1)
2968 (indent-for-tab-command)
2969 (should (string= "C.m.a" (python-info-current-defun)))
2970 (should (string= "def C.m.a" (python-info-current-defun t)))
2971 (python-indent-dedent-line-backspace 1)
2972 (should (string= "C.m" (python-info-current-defun)))
2973 (should (string= "def C.m" (python-info-current-defun t)))
2974 (python-indent-dedent-line-backspace 1)
2975 (should (string= "C" (python-info-current-defun)))
2976 (should (string= "class C" (python-info-current-defun t)))
2977 (python-tests-look-at "def c(self):")
2978 (should (string= "C.c" (python-info-current-defun)))
2979 (should (string= "def C.c" (python-info-current-defun t)))
2980 (python-tests-look-at "do_c()")
2981 (should (string= "C.c" (python-info-current-defun)))
2982 (should (string= "def C.c" (python-info-current-defun t)))))
2983
2984 (ert-deftest python-info-current-defun-3 ()
2985 (python-tests-with-temp-buffer
2986 "
2987 def decoratorFunctionWithArguments(arg1, arg2, arg3):
2988 '''print decorated function call data to stdout.
2989
2990 Usage:
2991
2992 @decoratorFunctionWithArguments('arg1', 'arg2')
2993 def func(a, b, c=True):
2994 pass
2995 '''
2996
2997 def wwrap(f):
2998 print 'Inside wwrap()'
2999 def wrapped_f(*args):
3000 print 'Inside wrapped_f()'
3001 print 'Decorator arguments:', arg1, arg2, arg3
3002 f(*args)
3003 print 'After f(*args)'
3004 return wrapped_f
3005 return wwrap
3006 "
3007 (python-tests-look-at "def wwrap(f):")
3008 (forward-line -1)
3009 (should (not (python-info-current-defun)))
3010 (indent-for-tab-command 1)
3011 (should (string= (python-info-current-defun)
3012 "decoratorFunctionWithArguments"))
3013 (should (string= (python-info-current-defun t)
3014 "def decoratorFunctionWithArguments"))
3015 (python-tests-look-at "def wrapped_f(*args):")
3016 (should (string= (python-info-current-defun)
3017 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
3018 (should (string= (python-info-current-defun t)
3019 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
3020 (python-tests-look-at "return wrapped_f")
3021 (should (string= (python-info-current-defun)
3022 "decoratorFunctionWithArguments.wwrap"))
3023 (should (string= (python-info-current-defun t)
3024 "def decoratorFunctionWithArguments.wwrap"))
3025 (end-of-line 1)
3026 (python-tests-look-at "return wwrap")
3027 (should (string= (python-info-current-defun)
3028 "decoratorFunctionWithArguments"))
3029 (should (string= (python-info-current-defun t)
3030 "def decoratorFunctionWithArguments"))))
3031
3032 (ert-deftest python-info-current-symbol-1 ()
3033 (python-tests-with-temp-buffer
3034 "
3035 class C(object):
3036
3037 def m(self):
3038 self.c()
3039
3040 def c(self):
3041 print ('a')
3042 "
3043 (python-tests-look-at "self.c()")
3044 (should (string= "self.c" (python-info-current-symbol)))
3045 (should (string= "C.c" (python-info-current-symbol t)))))
3046
3047 (ert-deftest python-info-current-symbol-2 ()
3048 (python-tests-with-temp-buffer
3049 "
3050 class C(object):
3051
3052 class M(object):
3053
3054 def a(self):
3055 self.c()
3056
3057 def c(self):
3058 pass
3059 "
3060 (python-tests-look-at "self.c()")
3061 (should (string= "self.c" (python-info-current-symbol)))
3062 (should (string= "C.M.c" (python-info-current-symbol t)))))
3063
3064 (ert-deftest python-info-current-symbol-3 ()
3065 "Keywords should not be considered symbols."
3066 :expected-result :failed
3067 (python-tests-with-temp-buffer
3068 "
3069 class C(object):
3070 pass
3071 "
3072 ;; FIXME: keywords are not symbols.
3073 (python-tests-look-at "class C")
3074 (should (not (python-info-current-symbol)))
3075 (should (not (python-info-current-symbol t)))
3076 (python-tests-look-at "C(object)")
3077 (should (string= "C" (python-info-current-symbol)))
3078 (should (string= "class C" (python-info-current-symbol t)))))
3079
3080 (ert-deftest python-info-statement-starts-block-p-1 ()
3081 (python-tests-with-temp-buffer
3082 "
3083 def long_function_name(
3084 var_one, var_two, var_three,
3085 var_four):
3086 print (var_one)
3087 "
3088 (python-tests-look-at "def long_function_name")
3089 (should (python-info-statement-starts-block-p))
3090 (python-tests-look-at "print (var_one)")
3091 (python-util-forward-comment -1)
3092 (should (python-info-statement-starts-block-p))))
3093
3094 (ert-deftest python-info-statement-starts-block-p-2 ()
3095 (python-tests-with-temp-buffer
3096 "
3097 if width == 0 and height == 0 and \\\\
3098 color == 'red' and emphasis == 'strong' or \\\\
3099 highlight > 100:
3100 raise ValueError('sorry, you lose')
3101 "
3102 (python-tests-look-at "if width == 0 and")
3103 (should (python-info-statement-starts-block-p))
3104 (python-tests-look-at "raise ValueError(")
3105 (python-util-forward-comment -1)
3106 (should (python-info-statement-starts-block-p))))
3107
3108 (ert-deftest python-info-statement-ends-block-p-1 ()
3109 (python-tests-with-temp-buffer
3110 "
3111 def long_function_name(
3112 var_one, var_two, var_three,
3113 var_four):
3114 print (var_one)
3115 "
3116 (python-tests-look-at "print (var_one)")
3117 (should (python-info-statement-ends-block-p))))
3118
3119 (ert-deftest python-info-statement-ends-block-p-2 ()
3120 (python-tests-with-temp-buffer
3121 "
3122 if width == 0 and height == 0 and \\\\
3123 color == 'red' and emphasis == 'strong' or \\\\
3124 highlight > 100:
3125 raise ValueError(
3126 'sorry, you lose'
3127
3128 )
3129 "
3130 (python-tests-look-at "raise ValueError(")
3131 (should (python-info-statement-ends-block-p))))
3132
3133 (ert-deftest python-info-beginning-of-statement-p-1 ()
3134 (python-tests-with-temp-buffer
3135 "
3136 def long_function_name(
3137 var_one, var_two, var_three,
3138 var_four):
3139 print (var_one)
3140 "
3141 (python-tests-look-at "def long_function_name")
3142 (should (python-info-beginning-of-statement-p))
3143 (forward-char 10)
3144 (should (not (python-info-beginning-of-statement-p)))
3145 (python-tests-look-at "print (var_one)")
3146 (should (python-info-beginning-of-statement-p))
3147 (goto-char (line-beginning-position))
3148 (should (not (python-info-beginning-of-statement-p)))))
3149
3150 (ert-deftest python-info-beginning-of-statement-p-2 ()
3151 (python-tests-with-temp-buffer
3152 "
3153 if width == 0 and height == 0 and \\\\
3154 color == 'red' and emphasis == 'strong' or \\\\
3155 highlight > 100:
3156 raise ValueError(
3157 'sorry, you lose'
3158
3159 )
3160 "
3161 (python-tests-look-at "if width == 0 and")
3162 (should (python-info-beginning-of-statement-p))
3163 (forward-char 10)
3164 (should (not (python-info-beginning-of-statement-p)))
3165 (python-tests-look-at "raise ValueError(")
3166 (should (python-info-beginning-of-statement-p))
3167 (goto-char (line-beginning-position))
3168 (should (not (python-info-beginning-of-statement-p)))))
3169
3170 (ert-deftest python-info-end-of-statement-p-1 ()
3171 (python-tests-with-temp-buffer
3172 "
3173 def long_function_name(
3174 var_one, var_two, var_three,
3175 var_four):
3176 print (var_one)
3177 "
3178 (python-tests-look-at "def long_function_name")
3179 (should (not (python-info-end-of-statement-p)))
3180 (end-of-line)
3181 (should (not (python-info-end-of-statement-p)))
3182 (python-tests-look-at "print (var_one)")
3183 (python-util-forward-comment -1)
3184 (should (python-info-end-of-statement-p))
3185 (python-tests-look-at "print (var_one)")
3186 (should (not (python-info-end-of-statement-p)))
3187 (end-of-line)
3188 (should (python-info-end-of-statement-p))))
3189
3190 (ert-deftest python-info-end-of-statement-p-2 ()
3191 (python-tests-with-temp-buffer
3192 "
3193 if width == 0 and height == 0 and \\\\
3194 color == 'red' and emphasis == 'strong' or \\\\
3195 highlight > 100:
3196 raise ValueError(
3197 'sorry, you lose'
3198
3199 )
3200 "
3201 (python-tests-look-at "if width == 0 and")
3202 (should (not (python-info-end-of-statement-p)))
3203 (end-of-line)
3204 (should (not (python-info-end-of-statement-p)))
3205 (python-tests-look-at "raise ValueError(")
3206 (python-util-forward-comment -1)
3207 (should (python-info-end-of-statement-p))
3208 (python-tests-look-at "raise ValueError(")
3209 (should (not (python-info-end-of-statement-p)))
3210 (end-of-line)
3211 (should (not (python-info-end-of-statement-p)))
3212 (goto-char (point-max))
3213 (python-util-forward-comment -1)
3214 (should (python-info-end-of-statement-p))))
3215
3216 (ert-deftest python-info-beginning-of-block-p-1 ()
3217 (python-tests-with-temp-buffer
3218 "
3219 def long_function_name(
3220 var_one, var_two, var_three,
3221 var_four):
3222 print (var_one)
3223 "
3224 (python-tests-look-at "def long_function_name")
3225 (should (python-info-beginning-of-block-p))
3226 (python-tests-look-at "var_one, var_two, var_three,")
3227 (should (not (python-info-beginning-of-block-p)))
3228 (python-tests-look-at "print (var_one)")
3229 (should (not (python-info-beginning-of-block-p)))))
3230
3231 (ert-deftest python-info-beginning-of-block-p-2 ()
3232 (python-tests-with-temp-buffer
3233 "
3234 if width == 0 and height == 0 and \\\\
3235 color == 'red' and emphasis == 'strong' or \\\\
3236 highlight > 100:
3237 raise ValueError(
3238 'sorry, you lose'
3239
3240 )
3241 "
3242 (python-tests-look-at "if width == 0 and")
3243 (should (python-info-beginning-of-block-p))
3244 (python-tests-look-at "color == 'red' and emphasis")
3245 (should (not (python-info-beginning-of-block-p)))
3246 (python-tests-look-at "raise ValueError(")
3247 (should (not (python-info-beginning-of-block-p)))))
3248
3249 (ert-deftest python-info-end-of-block-p-1 ()
3250 (python-tests-with-temp-buffer
3251 "
3252 def long_function_name(
3253 var_one, var_two, var_three,
3254 var_four):
3255 print (var_one)
3256 "
3257 (python-tests-look-at "def long_function_name")
3258 (should (not (python-info-end-of-block-p)))
3259 (python-tests-look-at "var_one, var_two, var_three,")
3260 (should (not (python-info-end-of-block-p)))
3261 (python-tests-look-at "var_four):")
3262 (end-of-line)
3263 (should (not (python-info-end-of-block-p)))
3264 (python-tests-look-at "print (var_one)")
3265 (should (not (python-info-end-of-block-p)))
3266 (end-of-line 1)
3267 (should (python-info-end-of-block-p))))
3268
3269 (ert-deftest python-info-end-of-block-p-2 ()
3270 (python-tests-with-temp-buffer
3271 "
3272 if width == 0 and height == 0 and \\\\
3273 color == 'red' and emphasis == 'strong' or \\\\
3274 highlight > 100:
3275 raise ValueError(
3276 'sorry, you lose'
3277
3278 )
3279 "
3280 (python-tests-look-at "if width == 0 and")
3281 (should (not (python-info-end-of-block-p)))
3282 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3283 (should (not (python-info-end-of-block-p)))
3284 (python-tests-look-at "highlight > 100:")
3285 (end-of-line)
3286 (should (not (python-info-end-of-block-p)))
3287 (python-tests-look-at "raise ValueError(")
3288 (should (not (python-info-end-of-block-p)))
3289 (end-of-line 1)
3290 (should (not (python-info-end-of-block-p)))
3291 (goto-char (point-max))
3292 (python-util-forward-comment -1)
3293 (should (python-info-end-of-block-p))))
3294
3295 (ert-deftest python-info-dedenter-opening-block-position-1 ()
3296 (python-tests-with-temp-buffer
3297 "
3298 if request.user.is_authenticated():
3299 try:
3300 profile = request.user.get_profile()
3301 except Profile.DoesNotExist:
3302 profile = Profile.objects.create(user=request.user)
3303 else:
3304 if profile.stats:
3305 profile.recalculate_stats()
3306 else:
3307 profile.clear_stats()
3308 finally:
3309 profile.views += 1
3310 profile.save()
3311 "
3312 (python-tests-look-at "try:")
3313 (should (not (python-info-dedenter-opening-block-position)))
3314 (python-tests-look-at "except Profile.DoesNotExist:")
3315 (should (= (python-tests-look-at "try:" -1 t)
3316 (python-info-dedenter-opening-block-position)))
3317 (python-tests-look-at "else:")
3318 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -1 t)
3319 (python-info-dedenter-opening-block-position)))
3320 (python-tests-look-at "if profile.stats:")
3321 (should (not (python-info-dedenter-opening-block-position)))
3322 (python-tests-look-at "else:")
3323 (should (= (python-tests-look-at "if profile.stats:" -1 t)
3324 (python-info-dedenter-opening-block-position)))
3325 (python-tests-look-at "finally:")
3326 (should (= (python-tests-look-at "else:" -2 t)
3327 (python-info-dedenter-opening-block-position)))))
3328
3329 (ert-deftest python-info-dedenter-opening-block-position-2 ()
3330 (python-tests-with-temp-buffer
3331 "
3332 if request.user.is_authenticated():
3333 profile = Profile.objects.get_or_create(user=request.user)
3334 if profile.stats:
3335 profile.recalculate_stats()
3336
3337 data = {
3338 'else': 'do it'
3339 }
3340 'else'
3341 "
3342 (python-tests-look-at "'else': 'do it'")
3343 (should (not (python-info-dedenter-opening-block-position)))
3344 (python-tests-look-at "'else'")
3345 (should (not (python-info-dedenter-opening-block-position)))))
3346
3347 (ert-deftest python-info-dedenter-opening-block-position-3 ()
3348 (python-tests-with-temp-buffer
3349 "
3350 if save:
3351 try:
3352 write_to_disk(data)
3353 except IOError:
3354 msg = 'Error saving to disk'
3355 message(msg)
3356 logger.exception(msg)
3357 except Exception:
3358 if hide_details:
3359 logger.exception('Unhandled exception')
3360 else
3361 finally:
3362 data.free()
3363 "
3364 (python-tests-look-at "try:")
3365 (should (not (python-info-dedenter-opening-block-position)))
3366
3367 (python-tests-look-at "except IOError:")
3368 (should (= (python-tests-look-at "try:" -1 t)
3369 (python-info-dedenter-opening-block-position)))
3370
3371 (python-tests-look-at "except Exception:")
3372 (should (= (python-tests-look-at "except IOError:" -1 t)
3373 (python-info-dedenter-opening-block-position)))
3374
3375 (python-tests-look-at "if hide_details:")
3376 (should (not (python-info-dedenter-opening-block-position)))
3377
3378 ;; check indentation modifies the detected opening block
3379 (python-tests-look-at "else")
3380 (should (= (python-tests-look-at "if hide_details:" -1 t)
3381 (python-info-dedenter-opening-block-position)))
3382
3383 (indent-line-to 8)
3384 (should (= (python-tests-look-at "if hide_details:" -1 t)
3385 (python-info-dedenter-opening-block-position)))
3386
3387 (indent-line-to 4)
3388 (should (= (python-tests-look-at "except Exception:" -1 t)
3389 (python-info-dedenter-opening-block-position)))
3390
3391 (indent-line-to 0)
3392 (should (= (python-tests-look-at "if save:" -1 t)
3393 (python-info-dedenter-opening-block-position)))))
3394
3395 (ert-deftest python-info-dedenter-opening-block-positions-1 ()
3396 (python-tests-with-temp-buffer
3397 "
3398 if save:
3399 try:
3400 write_to_disk(data)
3401 except IOError:
3402 msg = 'Error saving to disk'
3403 message(msg)
3404 logger.exception(msg)
3405 except Exception:
3406 if hide_details:
3407 logger.exception('Unhandled exception')
3408 else
3409 finally:
3410 data.free()
3411 "
3412 (python-tests-look-at "try:")
3413 (should (not (python-info-dedenter-opening-block-positions)))
3414
3415 (python-tests-look-at "except IOError:")
3416 (should
3417 (equal (list
3418 (python-tests-look-at "try:" -1 t))
3419 (python-info-dedenter-opening-block-positions)))
3420
3421 (python-tests-look-at "except Exception:")
3422 (should
3423 (equal (list
3424 (python-tests-look-at "except IOError:" -1 t))
3425 (python-info-dedenter-opening-block-positions)))
3426
3427 (python-tests-look-at "if hide_details:")
3428 (should (not (python-info-dedenter-opening-block-positions)))
3429
3430 ;; check indentation does not modify the detected opening blocks
3431 (python-tests-look-at "else")
3432 (should
3433 (equal (list
3434 (python-tests-look-at "if hide_details:" -1 t)
3435 (python-tests-look-at "except Exception:" -1 t)
3436 (python-tests-look-at "if save:" -1 t))
3437 (python-info-dedenter-opening-block-positions)))
3438
3439 (indent-line-to 8)
3440 (should
3441 (equal (list
3442 (python-tests-look-at "if hide_details:" -1 t)
3443 (python-tests-look-at "except Exception:" -1 t)
3444 (python-tests-look-at "if save:" -1 t))
3445 (python-info-dedenter-opening-block-positions)))
3446
3447 (indent-line-to 4)
3448 (should
3449 (equal (list
3450 (python-tests-look-at "if hide_details:" -1 t)
3451 (python-tests-look-at "except Exception:" -1 t)
3452 (python-tests-look-at "if save:" -1 t))
3453 (python-info-dedenter-opening-block-positions)))
3454
3455 (indent-line-to 0)
3456 (should
3457 (equal (list
3458 (python-tests-look-at "if hide_details:" -1 t)
3459 (python-tests-look-at "except Exception:" -1 t)
3460 (python-tests-look-at "if save:" -1 t))
3461 (python-info-dedenter-opening-block-positions)))))
3462
3463 (ert-deftest python-info-dedenter-opening-block-positions-2 ()
3464 "Test detection of opening blocks for elif."
3465 (python-tests-with-temp-buffer
3466 "
3467 if var:
3468 if var2:
3469 something()
3470 elif var3:
3471 something_else()
3472 elif
3473 "
3474 (python-tests-look-at "elif var3:")
3475 (should
3476 (equal (list
3477 (python-tests-look-at "if var2:" -1 t)
3478 (python-tests-look-at "if var:" -1 t))
3479 (python-info-dedenter-opening-block-positions)))
3480
3481 (python-tests-look-at "elif\n")
3482 (should
3483 (equal (list
3484 (python-tests-look-at "elif var3:" -1 t)
3485 (python-tests-look-at "if var:" -1 t))
3486 (python-info-dedenter-opening-block-positions)))))
3487
3488 (ert-deftest python-info-dedenter-opening-block-positions-3 ()
3489 "Test detection of opening blocks for else."
3490 (python-tests-with-temp-buffer
3491 "
3492 try:
3493 something()
3494 except:
3495 if var:
3496 if var2:
3497 something()
3498 elif var3:
3499 something_else()
3500 else
3501
3502 if var4:
3503 while var5:
3504 var4.pop()
3505 else
3506
3507 for value in var6:
3508 if value > 0:
3509 print value
3510 else
3511 "
3512 (python-tests-look-at "else\n")
3513 (should
3514 (equal (list
3515 (python-tests-look-at "elif var3:" -1 t)
3516 (python-tests-look-at "if var:" -1 t)
3517 (python-tests-look-at "except:" -1 t))
3518 (python-info-dedenter-opening-block-positions)))
3519
3520 (python-tests-look-at "else\n")
3521 (should
3522 (equal (list
3523 (python-tests-look-at "while var5:" -1 t)
3524 (python-tests-look-at "if var4:" -1 t))
3525 (python-info-dedenter-opening-block-positions)))
3526
3527 (python-tests-look-at "else\n")
3528 (should
3529 (equal (list
3530 (python-tests-look-at "if value > 0:" -1 t)
3531 (python-tests-look-at "for value in var6:" -1 t)
3532 (python-tests-look-at "if var4:" -1 t))
3533 (python-info-dedenter-opening-block-positions)))))
3534
3535 (ert-deftest python-info-dedenter-opening-block-positions-4 ()
3536 "Test detection of opening blocks for except."
3537 (python-tests-with-temp-buffer
3538 "
3539 try:
3540 something()
3541 except ValueError:
3542 something_else()
3543 except
3544 "
3545 (python-tests-look-at "except ValueError:")
3546 (should
3547 (equal (list (python-tests-look-at "try:" -1 t))
3548 (python-info-dedenter-opening-block-positions)))
3549
3550 (python-tests-look-at "except\n")
3551 (should
3552 (equal (list (python-tests-look-at "except ValueError:" -1 t))
3553 (python-info-dedenter-opening-block-positions)))))
3554
3555 (ert-deftest python-info-dedenter-opening-block-positions-5 ()
3556 "Test detection of opening blocks for finally."
3557 (python-tests-with-temp-buffer
3558 "
3559 try:
3560 something()
3561 finally
3562
3563 try:
3564 something_else()
3565 except:
3566 logger.exception('something went wrong')
3567 finally
3568
3569 try:
3570 something_else_else()
3571 except Exception:
3572 logger.exception('something else went wrong')
3573 else:
3574 print ('all good')
3575 finally
3576 "
3577 (python-tests-look-at "finally\n")
3578 (should
3579 (equal (list (python-tests-look-at "try:" -1 t))
3580 (python-info-dedenter-opening-block-positions)))
3581
3582 (python-tests-look-at "finally\n")
3583 (should
3584 (equal (list (python-tests-look-at "except:" -1 t))
3585 (python-info-dedenter-opening-block-positions)))
3586
3587 (python-tests-look-at "finally\n")
3588 (should
3589 (equal (list (python-tests-look-at "else:" -1 t))
3590 (python-info-dedenter-opening-block-positions)))))
3591
3592 (ert-deftest python-info-dedenter-opening-block-message-1 ()
3593 "Test dedenters inside strings are ignored."
3594 (python-tests-with-temp-buffer
3595 "'''
3596 try:
3597 something()
3598 except:
3599 logger.exception('something went wrong')
3600 '''
3601 "
3602 (python-tests-look-at "except\n")
3603 (should (not (python-info-dedenter-opening-block-message)))))
3604
3605 (ert-deftest python-info-dedenter-opening-block-message-2 ()
3606 "Test except keyword."
3607 (python-tests-with-temp-buffer
3608 "
3609 try:
3610 something()
3611 except:
3612 logger.exception('something went wrong')
3613 "
3614 (python-tests-look-at "except:")
3615 (should (string=
3616 "Closes try:"
3617 (substring-no-properties
3618 (python-info-dedenter-opening-block-message))))
3619 (end-of-line)
3620 (should (string=
3621 "Closes try:"
3622 (substring-no-properties
3623 (python-info-dedenter-opening-block-message))))))
3624
3625 (ert-deftest python-info-dedenter-opening-block-message-3 ()
3626 "Test else keyword."
3627 (python-tests-with-temp-buffer
3628 "
3629 try:
3630 something()
3631 except:
3632 logger.exception('something went wrong')
3633 else:
3634 logger.debug('all good')
3635 "
3636 (python-tests-look-at "else:")
3637 (should (string=
3638 "Closes except:"
3639 (substring-no-properties
3640 (python-info-dedenter-opening-block-message))))
3641 (end-of-line)
3642 (should (string=
3643 "Closes except:"
3644 (substring-no-properties
3645 (python-info-dedenter-opening-block-message))))))
3646
3647 (ert-deftest python-info-dedenter-opening-block-message-4 ()
3648 "Test finally keyword."
3649 (python-tests-with-temp-buffer
3650 "
3651 try:
3652 something()
3653 except:
3654 logger.exception('something went wrong')
3655 else:
3656 logger.debug('all good')
3657 finally:
3658 clean()
3659 "
3660 (python-tests-look-at "finally:")
3661 (should (string=
3662 "Closes else:"
3663 (substring-no-properties
3664 (python-info-dedenter-opening-block-message))))
3665 (end-of-line)
3666 (should (string=
3667 "Closes else:"
3668 (substring-no-properties
3669 (python-info-dedenter-opening-block-message))))))
3670
3671 (ert-deftest python-info-dedenter-opening-block-message-5 ()
3672 "Test elif keyword."
3673 (python-tests-with-temp-buffer
3674 "
3675 if a:
3676 something()
3677 elif b:
3678 "
3679 (python-tests-look-at "elif b:")
3680 (should (string=
3681 "Closes if a:"
3682 (substring-no-properties
3683 (python-info-dedenter-opening-block-message))))
3684 (end-of-line)
3685 (should (string=
3686 "Closes if a:"
3687 (substring-no-properties
3688 (python-info-dedenter-opening-block-message))))))
3689
3690
3691 (ert-deftest python-info-dedenter-statement-p-1 ()
3692 "Test dedenters inside strings are ignored."
3693 (python-tests-with-temp-buffer
3694 "'''
3695 try:
3696 something()
3697 except:
3698 logger.exception('something went wrong')
3699 '''
3700 "
3701 (python-tests-look-at "except\n")
3702 (should (not (python-info-dedenter-statement-p)))))
3703
3704 (ert-deftest python-info-dedenter-statement-p-2 ()
3705 "Test except keyword."
3706 (python-tests-with-temp-buffer
3707 "
3708 try:
3709 something()
3710 except:
3711 logger.exception('something went wrong')
3712 "
3713 (python-tests-look-at "except:")
3714 (should (= (point) (python-info-dedenter-statement-p)))
3715 (end-of-line)
3716 (should (= (save-excursion
3717 (back-to-indentation)
3718 (point))
3719 (python-info-dedenter-statement-p)))))
3720
3721 (ert-deftest python-info-dedenter-statement-p-3 ()
3722 "Test else keyword."
3723 (python-tests-with-temp-buffer
3724 "
3725 try:
3726 something()
3727 except:
3728 logger.exception('something went wrong')
3729 else:
3730 logger.debug('all good')
3731 "
3732 (python-tests-look-at "else:")
3733 (should (= (point) (python-info-dedenter-statement-p)))
3734 (end-of-line)
3735 (should (= (save-excursion
3736 (back-to-indentation)
3737 (point))
3738 (python-info-dedenter-statement-p)))))
3739
3740 (ert-deftest python-info-dedenter-statement-p-4 ()
3741 "Test finally keyword."
3742 (python-tests-with-temp-buffer
3743 "
3744 try:
3745 something()
3746 except:
3747 logger.exception('something went wrong')
3748 else:
3749 logger.debug('all good')
3750 finally:
3751 clean()
3752 "
3753 (python-tests-look-at "finally:")
3754 (should (= (point) (python-info-dedenter-statement-p)))
3755 (end-of-line)
3756 (should (= (save-excursion
3757 (back-to-indentation)
3758 (point))
3759 (python-info-dedenter-statement-p)))))
3760
3761 (ert-deftest python-info-dedenter-statement-p-5 ()
3762 "Test elif keyword."
3763 (python-tests-with-temp-buffer
3764 "
3765 if a:
3766 something()
3767 elif b:
3768 "
3769 (python-tests-look-at "elif b:")
3770 (should (= (point) (python-info-dedenter-statement-p)))
3771 (end-of-line)
3772 (should (= (save-excursion
3773 (back-to-indentation)
3774 (point))
3775 (python-info-dedenter-statement-p)))))
3776
3777 (ert-deftest python-info-line-ends-backslash-p-1 ()
3778 (python-tests-with-temp-buffer
3779 "
3780 objects = Thing.objects.all() \\\\
3781 .filter(
3782 type='toy',
3783 status='bought'
3784 ) \\\\
3785 .aggregate(
3786 Sum('amount')
3787 ) \\\\
3788 .values_list()
3789 "
3790 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
3791 (should (python-info-line-ends-backslash-p 3))
3792 (should (python-info-line-ends-backslash-p 4))
3793 (should (python-info-line-ends-backslash-p 5))
3794 (should (python-info-line-ends-backslash-p 6)) ; ) \...
3795 (should (python-info-line-ends-backslash-p 7))
3796 (should (python-info-line-ends-backslash-p 8))
3797 (should (python-info-line-ends-backslash-p 9))
3798 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
3799
3800 (ert-deftest python-info-beginning-of-backslash-1 ()
3801 (python-tests-with-temp-buffer
3802 "
3803 objects = Thing.objects.all() \\\\
3804 .filter(
3805 type='toy',
3806 status='bought'
3807 ) \\\\
3808 .aggregate(
3809 Sum('amount')
3810 ) \\\\
3811 .values_list()
3812 "
3813 (let ((first 2)
3814 (second (python-tests-look-at ".filter("))
3815 (third (python-tests-look-at ".aggregate(")))
3816 (should (= first (python-info-beginning-of-backslash 2)))
3817 (should (= second (python-info-beginning-of-backslash 3)))
3818 (should (= second (python-info-beginning-of-backslash 4)))
3819 (should (= second (python-info-beginning-of-backslash 5)))
3820 (should (= second (python-info-beginning-of-backslash 6)))
3821 (should (= third (python-info-beginning-of-backslash 7)))
3822 (should (= third (python-info-beginning-of-backslash 8)))
3823 (should (= third (python-info-beginning-of-backslash 9)))
3824 (should (not (python-info-beginning-of-backslash 10))))))
3825
3826 (ert-deftest python-info-continuation-line-p-1 ()
3827 (python-tests-with-temp-buffer
3828 "
3829 if width == 0 and height == 0 and \\\\
3830 color == 'red' and emphasis == 'strong' or \\\\
3831 highlight > 100:
3832 raise ValueError(
3833 'sorry, you lose'
3834
3835 )
3836 "
3837 (python-tests-look-at "if width == 0 and height == 0 and")
3838 (should (not (python-info-continuation-line-p)))
3839 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3840 (should (python-info-continuation-line-p))
3841 (python-tests-look-at "highlight > 100:")
3842 (should (python-info-continuation-line-p))
3843 (python-tests-look-at "raise ValueError(")
3844 (should (not (python-info-continuation-line-p)))
3845 (python-tests-look-at "'sorry, you lose'")
3846 (should (python-info-continuation-line-p))
3847 (forward-line 1)
3848 (should (python-info-continuation-line-p))
3849 (python-tests-look-at ")")
3850 (should (python-info-continuation-line-p))
3851 (forward-line 1)
3852 (should (not (python-info-continuation-line-p)))))
3853
3854 (ert-deftest python-info-block-continuation-line-p-1 ()
3855 (python-tests-with-temp-buffer
3856 "
3857 if width == 0 and height == 0 and \\\\
3858 color == 'red' and emphasis == 'strong' or \\\\
3859 highlight > 100:
3860 raise ValueError(
3861 'sorry, you lose'
3862
3863 )
3864 "
3865 (python-tests-look-at "if width == 0 and")
3866 (should (not (python-info-block-continuation-line-p)))
3867 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3868 (should (= (python-info-block-continuation-line-p)
3869 (python-tests-look-at "if width == 0 and" -1 t)))
3870 (python-tests-look-at "highlight > 100:")
3871 (should (not (python-info-block-continuation-line-p)))))
3872
3873 (ert-deftest python-info-block-continuation-line-p-2 ()
3874 (python-tests-with-temp-buffer
3875 "
3876 def foo(a,
3877 b,
3878 c):
3879 pass
3880 "
3881 (python-tests-look-at "def foo(a,")
3882 (should (not (python-info-block-continuation-line-p)))
3883 (python-tests-look-at "b,")
3884 (should (= (python-info-block-continuation-line-p)
3885 (python-tests-look-at "def foo(a," -1 t)))
3886 (python-tests-look-at "c):")
3887 (should (not (python-info-block-continuation-line-p)))))
3888
3889 (ert-deftest python-info-assignment-continuation-line-p-1 ()
3890 (python-tests-with-temp-buffer
3891 "
3892 data = foo(), bar() \\\\
3893 baz(), 4 \\\\
3894 5, 6
3895 "
3896 (python-tests-look-at "data = foo(), bar()")
3897 (should (not (python-info-assignment-continuation-line-p)))
3898 (python-tests-look-at "baz(), 4")
3899 (should (= (python-info-assignment-continuation-line-p)
3900 (python-tests-look-at "foo()," -1 t)))
3901 (python-tests-look-at "5, 6")
3902 (should (not (python-info-assignment-continuation-line-p)))))
3903
3904 (ert-deftest python-info-assignment-continuation-line-p-2 ()
3905 (python-tests-with-temp-buffer
3906 "
3907 data = (foo(), bar()
3908 baz(), 4
3909 5, 6)
3910 "
3911 (python-tests-look-at "data = (foo(), bar()")
3912 (should (not (python-info-assignment-continuation-line-p)))
3913 (python-tests-look-at "baz(), 4")
3914 (should (= (python-info-assignment-continuation-line-p)
3915 (python-tests-look-at "(foo()," -1 t)))
3916 (python-tests-look-at "5, 6)")
3917 (should (not (python-info-assignment-continuation-line-p)))))
3918
3919 (ert-deftest python-info-looking-at-beginning-of-defun-1 ()
3920 (python-tests-with-temp-buffer
3921 "
3922 def decorat0r(deff):
3923 '''decorates stuff.
3924
3925 @decorat0r
3926 def foo(arg):
3927 ...
3928 '''
3929 def wrap():
3930 deff()
3931 return wwrap
3932 "
3933 (python-tests-look-at "def decorat0r(deff):")
3934 (should (python-info-looking-at-beginning-of-defun))
3935 (python-tests-look-at "def foo(arg):")
3936 (should (not (python-info-looking-at-beginning-of-defun)))
3937 (python-tests-look-at "def wrap():")
3938 (should (python-info-looking-at-beginning-of-defun))
3939 (python-tests-look-at "deff()")
3940 (should (not (python-info-looking-at-beginning-of-defun)))))
3941
3942 (ert-deftest python-info-current-line-comment-p-1 ()
3943 (python-tests-with-temp-buffer
3944 "
3945 # this is a comment
3946 foo = True # another comment
3947 '#this is a string'
3948 if foo:
3949 # more comments
3950 print ('bar') # print bar
3951 "
3952 (python-tests-look-at "# this is a comment")
3953 (should (python-info-current-line-comment-p))
3954 (python-tests-look-at "foo = True # another comment")
3955 (should (not (python-info-current-line-comment-p)))
3956 (python-tests-look-at "'#this is a string'")
3957 (should (not (python-info-current-line-comment-p)))
3958 (python-tests-look-at "# more comments")
3959 (should (python-info-current-line-comment-p))
3960 (python-tests-look-at "print ('bar') # print bar")
3961 (should (not (python-info-current-line-comment-p)))))
3962
3963 (ert-deftest python-info-current-line-empty-p ()
3964 (python-tests-with-temp-buffer
3965 "
3966 # this is a comment
3967
3968 foo = True # another comment
3969 "
3970 (should (python-info-current-line-empty-p))
3971 (python-tests-look-at "# this is a comment")
3972 (should (not (python-info-current-line-empty-p)))
3973 (forward-line 1)
3974 (should (python-info-current-line-empty-p))))
3975
3976 (ert-deftest python-info-encoding-from-cookie-1 ()
3977 "Should detect it on first line."
3978 (python-tests-with-temp-buffer
3979 "# coding=latin-1
3980
3981 foo = True # another comment
3982 "
3983 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
3984
3985 (ert-deftest python-info-encoding-from-cookie-2 ()
3986 "Should detect it on second line."
3987 (python-tests-with-temp-buffer
3988 "
3989 # coding=latin-1
3990
3991 foo = True # another comment
3992 "
3993 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
3994
3995 (ert-deftest python-info-encoding-from-cookie-3 ()
3996 "Should not be detected on third line (and following ones)."
3997 (python-tests-with-temp-buffer
3998 "
3999
4000 # coding=latin-1
4001 foo = True # another comment
4002 "
4003 (should (not (python-info-encoding-from-cookie)))))
4004
4005 (ert-deftest python-info-encoding-from-cookie-4 ()
4006 "Should detect Emacs style."
4007 (python-tests-with-temp-buffer
4008 "# -*- coding: latin-1 -*-
4009
4010 foo = True # another comment"
4011 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4012
4013 (ert-deftest python-info-encoding-from-cookie-5 ()
4014 "Should detect Vim style."
4015 (python-tests-with-temp-buffer
4016 "# vim: set fileencoding=latin-1 :
4017
4018 foo = True # another comment"
4019 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4020
4021 (ert-deftest python-info-encoding-from-cookie-6 ()
4022 "First cookie wins."
4023 (python-tests-with-temp-buffer
4024 "# -*- coding: iso-8859-1 -*-
4025 # vim: set fileencoding=latin-1 :
4026
4027 foo = True # another comment"
4028 (should (eq (python-info-encoding-from-cookie) 'iso-8859-1))))
4029
4030 (ert-deftest python-info-encoding-from-cookie-7 ()
4031 "First cookie wins."
4032 (python-tests-with-temp-buffer
4033 "# vim: set fileencoding=latin-1 :
4034 # -*- coding: iso-8859-1 -*-
4035
4036 foo = True # another comment"
4037 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4038
4039 (ert-deftest python-info-encoding-1 ()
4040 "Should return the detected encoding from cookie."
4041 (python-tests-with-temp-buffer
4042 "# vim: set fileencoding=latin-1 :
4043
4044 foo = True # another comment"
4045 (should (eq (python-info-encoding) 'latin-1))))
4046
4047 (ert-deftest python-info-encoding-2 ()
4048 "Should default to utf-8."
4049 (python-tests-with-temp-buffer
4050 "# No encoding for you
4051
4052 foo = True # another comment"
4053 (should (eq (python-info-encoding) 'utf-8))))
4054
4055 \f
4056 ;;; Utility functions
4057
4058 (ert-deftest python-util-goto-line-1 ()
4059 (python-tests-with-temp-buffer
4060 (concat
4061 "# a comment
4062 # another comment
4063 def foo(a, b, c):
4064 pass" (make-string 20 ?\n))
4065 (python-util-goto-line 10)
4066 (should (= (line-number-at-pos) 10))
4067 (python-util-goto-line 20)
4068 (should (= (line-number-at-pos) 20))))
4069
4070 (ert-deftest python-util-clone-local-variables-1 ()
4071 (let ((buffer (generate-new-buffer
4072 "python-util-clone-local-variables-1"))
4073 (varcons
4074 '((python-fill-docstring-style . django)
4075 (python-shell-interpreter . "python")
4076 (python-shell-interpreter-args . "manage.py shell")
4077 (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
4078 (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
4079 (python-shell-extra-pythonpaths "/home/user/pylib/")
4080 (python-shell-completion-setup-code
4081 . "from IPython.core.completerlib import module_completion")
4082 (python-shell-completion-string-code
4083 . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
4084 (python-shell-virtualenv-root
4085 . "/home/user/.virtualenvs/project"))))
4086 (with-current-buffer buffer
4087 (kill-all-local-variables)
4088 (dolist (ccons varcons)
4089 (set (make-local-variable (car ccons)) (cdr ccons))))
4090 (python-tests-with-temp-buffer
4091 ""
4092 (python-util-clone-local-variables buffer)
4093 (dolist (ccons varcons)
4094 (should
4095 (equal (symbol-value (car ccons)) (cdr ccons)))))
4096 (kill-buffer buffer)))
4097
4098 (ert-deftest python-util-strip-string-1 ()
4099 (should (string= (python-util-strip-string "\t\r\n str") "str"))
4100 (should (string= (python-util-strip-string "str \n\r") "str"))
4101 (should (string= (python-util-strip-string "\t\r\n str \n\r ") "str"))
4102 (should
4103 (string= (python-util-strip-string "\n str \nin \tg \n\r") "str \nin \tg"))
4104 (should (string= (python-util-strip-string "\n \t \n\r ") ""))
4105 (should (string= (python-util-strip-string "") "")))
4106
4107 (ert-deftest python-util-forward-comment-1 ()
4108 (python-tests-with-temp-buffer
4109 (concat
4110 "# a comment
4111 # another comment
4112 # bad indented comment
4113 # more comments" (make-string 9999 ?\n))
4114 (python-util-forward-comment 1)
4115 (should (= (point) (point-max)))
4116 (python-util-forward-comment -1)
4117 (should (= (point) (point-min)))))
4118
4119 (ert-deftest python-util-valid-regexp-p-1 ()
4120 (should (python-util-valid-regexp-p ""))
4121 (should (python-util-valid-regexp-p python-shell-prompt-regexp))
4122 (should (not (python-util-valid-regexp-p "\\("))))
4123
4124 \f
4125 ;;; Electricity
4126
4127 (ert-deftest python-parens-electric-indent-1 ()
4128 (require 'electric)
4129 (let ((eim electric-indent-mode))
4130 (unwind-protect
4131 (progn
4132 (python-tests-with-temp-buffer
4133 "
4134 from django.conf.urls import patterns, include, url
4135
4136 from django.contrib import admin
4137
4138 from myapp import views
4139
4140
4141 urlpatterns = patterns('',
4142 url(r'^$', views.index
4143 )
4144 "
4145 (electric-indent-mode 1)
4146 (python-tests-look-at "views.index")
4147 (end-of-line)
4148
4149 ;; Inserting commas within the same line should leave
4150 ;; indentation unchanged.
4151 (python-tests-self-insert ",")
4152 (should (= (current-indentation) 4))
4153
4154 ;; As well as any other input happening within the same
4155 ;; set of parens.
4156 (python-tests-self-insert " name='index')")
4157 (should (= (current-indentation) 4))
4158
4159 ;; But a comma outside it, should trigger indentation.
4160 (python-tests-self-insert ",")
4161 (should (= (current-indentation) 23))
4162
4163 ;; Newline indents to the first argument column
4164 (python-tests-self-insert "\n")
4165 (should (= (current-indentation) 23))
4166
4167 ;; All this input must not change indentation
4168 (indent-line-to 4)
4169 (python-tests-self-insert "url(r'^/login$', views.login)")
4170 (should (= (current-indentation) 4))
4171
4172 ;; But this comma does
4173 (python-tests-self-insert ",")
4174 (should (= (current-indentation) 23))))
4175 (or eim (electric-indent-mode -1)))))
4176
4177 (ert-deftest python-triple-quote-pairing ()
4178 (require 'electric)
4179 (let ((epm electric-pair-mode))
4180 (unwind-protect
4181 (progn
4182 (python-tests-with-temp-buffer
4183 "\"\"\n"
4184 (or epm (electric-pair-mode 1))
4185 (goto-char (1- (point-max)))
4186 (python-tests-self-insert ?\")
4187 (should (string= (buffer-string)
4188 "\"\"\"\"\"\"\n"))
4189 (should (= (point) 4)))
4190 (python-tests-with-temp-buffer
4191 "\n"
4192 (python-tests-self-insert (list ?\" ?\" ?\"))
4193 (should (string= (buffer-string)
4194 "\"\"\"\"\"\"\n"))
4195 (should (= (point) 4)))
4196 (python-tests-with-temp-buffer
4197 "\"\n\"\"\n"
4198 (goto-char (1- (point-max)))
4199 (python-tests-self-insert ?\")
4200 (should (= (point) (1- (point-max))))
4201 (should (string= (buffer-string)
4202 "\"\n\"\"\"\n"))))
4203 (or epm (electric-pair-mode -1)))))
4204
4205
4206 (provide 'python-tests)
4207
4208 ;; Local Variables:
4209 ;; coding: utf-8
4210 ;; indent-tabs-mode: nil
4211 ;; End:
4212
4213 ;;; python-tests.el ends here