]> code.delx.au - gnu-emacs/blob - test/automated/python-tests.el
Update copyright year to 2015
[gnu-emacs] / test / automated / python-tests.el
1 ;;; python-tests.el --- Test suite for python.el
2
3 ;; Copyright (C) 2013-2015 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, dedenders 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 on different scenarios."
1812 (python-tests-with-temp-buffer
1813 ""
1814 (should (string= (python-shell-get-process-name nil)
1815 python-shell-buffer-name))
1816 ;; When the `current-buffer' doesn't have `buffer-file-name', even
1817 ;; if dedicated flag is non-nil should not include its name.
1818 (should (string= (python-shell-get-process-name t)
1819 python-shell-buffer-name)))
1820 (python-tests-with-temp-file
1821 ""
1822 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1823 ;; should be respected.
1824 (should (string= (python-shell-get-process-name nil)
1825 python-shell-buffer-name))
1826 (should (string=
1827 (python-shell-get-process-name t)
1828 (format "%s[%s]" python-shell-buffer-name buffer-file-name)))))
1829
1830 (ert-deftest python-shell-internal-get-process-name-1 ()
1831 "Check the internal process name is config-unique."
1832 (let* ((python-shell-interpreter python-tests-shell-interpreter)
1833 (python-shell-interpreter-args "")
1834 (python-shell-prompt-regexp ">>> ")
1835 (python-shell-prompt-block-regexp "[.][.][.] ")
1836 (python-shell-setup-codes "")
1837 (python-shell-process-environment "")
1838 (python-shell-extra-pythonpaths "")
1839 (python-shell-exec-path "")
1840 (python-shell-virtualenv-path "")
1841 (expected (python-tests-with-temp-buffer
1842 "" (python-shell-internal-get-process-name))))
1843 ;; Same configurations should match.
1844 (should
1845 (string= expected
1846 (python-tests-with-temp-buffer
1847 "" (python-shell-internal-get-process-name))))
1848 (let ((python-shell-interpreter-args "-B"))
1849 ;; A minimal change should generate different names.
1850 (should
1851 (not (string=
1852 expected
1853 (python-tests-with-temp-buffer
1854 "" (python-shell-internal-get-process-name))))))))
1855
1856 (ert-deftest python-shell-parse-command-1 ()
1857 "Check the command to execute is calculated correctly.
1858 Using `python-shell-interpreter' and
1859 `python-shell-interpreter-args'."
1860 (skip-unless (executable-find python-tests-shell-interpreter))
1861 (let ((python-shell-interpreter (executable-find
1862 python-tests-shell-interpreter))
1863 (python-shell-interpreter-args "-B"))
1864 (should (string=
1865 (format "%s %s"
1866 python-shell-interpreter
1867 python-shell-interpreter-args)
1868 (python-shell-parse-command)))))
1869
1870 (ert-deftest python-shell-calculate-process-environment-1 ()
1871 "Test `python-shell-process-environment' modification."
1872 (let* ((original-process-environment process-environment)
1873 (python-shell-process-environment
1874 '("TESTVAR1=value1" "TESTVAR2=value2"))
1875 (process-environment
1876 (python-shell-calculate-process-environment)))
1877 (should (equal (getenv "TESTVAR1") "value1"))
1878 (should (equal (getenv "TESTVAR2") "value2"))))
1879
1880 (ert-deftest python-shell-calculate-process-environment-2 ()
1881 "Test `python-shell-extra-pythonpaths' modification."
1882 (let* ((original-process-environment process-environment)
1883 (original-pythonpath (getenv "PYTHONPATH"))
1884 (paths '("path1" "path2"))
1885 (python-shell-extra-pythonpaths paths)
1886 (process-environment
1887 (python-shell-calculate-process-environment)))
1888 (should (equal (getenv "PYTHONPATH")
1889 (concat
1890 (mapconcat 'identity paths path-separator)
1891 path-separator original-pythonpath)))))
1892
1893 (ert-deftest python-shell-calculate-process-environment-3 ()
1894 "Test `python-shell-virtualenv-path' modification."
1895 (let* ((original-process-environment process-environment)
1896 (original-path (or (getenv "PATH") ""))
1897 (python-shell-virtualenv-path
1898 (directory-file-name user-emacs-directory))
1899 (process-environment
1900 (python-shell-calculate-process-environment)))
1901 (should (not (getenv "PYTHONHOME")))
1902 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-path))
1903 (should (equal (getenv "PATH")
1904 (format "%s/bin%s%s"
1905 python-shell-virtualenv-path
1906 path-separator original-path)))))
1907
1908 (ert-deftest python-shell-calculate-process-environment-4 ()
1909 "Test `python-shell-unbuffered' modification."
1910 (setenv "PYTHONUNBUFFERED")
1911 (let* ((process-environment
1912 (python-shell-calculate-process-environment)))
1913 ;; Defaults to t
1914 (should python-shell-unbuffered)
1915 (should (string= (getenv "PYTHONUNBUFFERED") "1"))))
1916
1917 (ert-deftest python-shell-calculate-process-environment-5 ()
1918 (setenv "PYTHONUNBUFFERED")
1919 "Test `python-shell-unbuffered' modification."
1920 (let* ((python-shell-unbuffered nil)
1921 (process-environment
1922 (python-shell-calculate-process-environment)))
1923 (should (not (getenv "PYTHONUNBUFFERED")))))
1924
1925 (ert-deftest python-shell-calculate-exec-path-1 ()
1926 "Test `python-shell-exec-path' modification."
1927 (let* ((original-exec-path exec-path)
1928 (python-shell-exec-path '("path1" "path2"))
1929 (exec-path (python-shell-calculate-exec-path)))
1930 (should (equal
1931 exec-path
1932 (append python-shell-exec-path
1933 original-exec-path)))))
1934
1935 (ert-deftest python-shell-calculate-exec-path-2 ()
1936 "Test `python-shell-exec-path' modification."
1937 (let* ((original-exec-path exec-path)
1938 (python-shell-virtualenv-path
1939 (directory-file-name (expand-file-name user-emacs-directory)))
1940 (exec-path (python-shell-calculate-exec-path)))
1941 (should (equal
1942 exec-path
1943 (append (cons
1944 (format "%s/bin" python-shell-virtualenv-path)
1945 original-exec-path))))))
1946
1947 (ert-deftest python-shell-make-comint-1 ()
1948 "Check comint creation for global shell buffer."
1949 (skip-unless (executable-find python-tests-shell-interpreter))
1950 ;; The interpreter can get killed too quickly to allow it to clean
1951 ;; up the tempfiles that the default python-shell-setup-codes create,
1952 ;; so it leaves tempfiles behind, which is a minor irritation.
1953 (let* ((python-shell-setup-codes nil)
1954 (python-shell-interpreter
1955 (executable-find python-tests-shell-interpreter))
1956 (proc-name (python-shell-get-process-name nil))
1957 (shell-buffer
1958 (python-tests-with-temp-buffer
1959 "" (python-shell-make-comint
1960 (python-shell-parse-command) proc-name)))
1961 (process (get-buffer-process shell-buffer)))
1962 (unwind-protect
1963 (progn
1964 (set-process-query-on-exit-flag process nil)
1965 (should (process-live-p process))
1966 (with-current-buffer shell-buffer
1967 (should (eq major-mode 'inferior-python-mode))
1968 (should (string= (buffer-name) (format "*%s*" proc-name)))))
1969 (kill-buffer shell-buffer))))
1970
1971 (ert-deftest python-shell-make-comint-2 ()
1972 "Check comint creation for internal shell buffer."
1973 (skip-unless (executable-find python-tests-shell-interpreter))
1974 (let* ((python-shell-setup-codes nil)
1975 (python-shell-interpreter
1976 (executable-find python-tests-shell-interpreter))
1977 (proc-name (python-shell-internal-get-process-name))
1978 (shell-buffer
1979 (python-tests-with-temp-buffer
1980 "" (python-shell-make-comint
1981 (python-shell-parse-command) proc-name nil t)))
1982 (process (get-buffer-process shell-buffer)))
1983 (unwind-protect
1984 (progn
1985 (set-process-query-on-exit-flag process nil)
1986 (should (process-live-p process))
1987 (with-current-buffer shell-buffer
1988 (should (eq major-mode 'inferior-python-mode))
1989 (should (string= (buffer-name) (format " *%s*" proc-name)))))
1990 (kill-buffer shell-buffer))))
1991
1992 (ert-deftest python-shell-make-comint-3 ()
1993 "Check comint creation with overridden python interpreter and args.
1994 The command passed to `python-shell-make-comint' as argument must
1995 locally override global values set in `python-shell-interpreter'
1996 and `python-shell-interpreter-args' in the new shell buffer."
1997 (skip-unless (executable-find python-tests-shell-interpreter))
1998 (let* ((python-shell-setup-codes nil)
1999 (python-shell-interpreter "interpreter")
2000 (python-shell-interpreter-args "--some-args")
2001 (proc-name (python-shell-get-process-name nil))
2002 (interpreter-override
2003 (concat (executable-find python-tests-shell-interpreter) " " "-i"))
2004 (shell-buffer
2005 (python-tests-with-temp-buffer
2006 "" (python-shell-make-comint interpreter-override proc-name nil)))
2007 (process (get-buffer-process shell-buffer)))
2008 (unwind-protect
2009 (progn
2010 (set-process-query-on-exit-flag process nil)
2011 (should (process-live-p process))
2012 (with-current-buffer shell-buffer
2013 (should (eq major-mode 'inferior-python-mode))
2014 (should (file-equal-p
2015 python-shell-interpreter
2016 (executable-find python-tests-shell-interpreter)))
2017 (should (string= python-shell-interpreter-args "-i"))))
2018 (kill-buffer shell-buffer))))
2019
2020 (ert-deftest python-shell-make-comint-4 ()
2021 "Check shell calculated prompts regexps are set."
2022 (skip-unless (executable-find python-tests-shell-interpreter))
2023 (let* ((process-environment process-environment)
2024 (python-shell-setup-codes nil)
2025 (python-shell-interpreter
2026 (executable-find python-tests-shell-interpreter))
2027 (python-shell-interpreter-args "-i")
2028 (python-shell--prompt-calculated-input-regexp nil)
2029 (python-shell--prompt-calculated-output-regexp nil)
2030 (python-shell-prompt-detect-enabled t)
2031 (python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2032 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2033 (python-shell-prompt-regexp "in")
2034 (python-shell-prompt-block-regexp "block")
2035 (python-shell-prompt-pdb-regexp "pdf")
2036 (python-shell-prompt-output-regexp "output")
2037 (startup-code (concat "import sys\n"
2038 "sys.ps1 = 'py> '\n"
2039 "sys.ps2 = '..> '\n"
2040 "sys.ps3 = 'out '\n"))
2041 (startup-file (python-shell--save-temp-file startup-code))
2042 (proc-name (python-shell-get-process-name nil))
2043 (shell-buffer
2044 (progn
2045 (setenv "PYTHONSTARTUP" startup-file)
2046 (python-tests-with-temp-buffer
2047 "" (python-shell-make-comint
2048 (python-shell-parse-command) proc-name nil))))
2049 (process (get-buffer-process shell-buffer)))
2050 (unwind-protect
2051 (progn
2052 (set-process-query-on-exit-flag process nil)
2053 (should (process-live-p process))
2054 (with-current-buffer shell-buffer
2055 (should (eq major-mode 'inferior-python-mode))
2056 (should (string=
2057 python-shell--prompt-calculated-input-regexp
2058 (concat "^\\(extralargeinputprompt\\|\\.\\.> \\|"
2059 "block\\|py> \\|pdf\\|sml\\|in\\)")))
2060 (should (string=
2061 python-shell--prompt-calculated-output-regexp
2062 "^\\(extralargeoutputprompt\\|output\\|out \\|sml\\)"))))
2063 (delete-file startup-file)
2064 (kill-buffer shell-buffer))))
2065
2066 (ert-deftest python-shell-get-process-1 ()
2067 "Check dedicated shell process preference over global."
2068 (skip-unless (executable-find python-tests-shell-interpreter))
2069 (python-tests-with-temp-file
2070 ""
2071 (let* ((python-shell-setup-codes nil)
2072 (python-shell-interpreter
2073 (executable-find python-tests-shell-interpreter))
2074 (global-proc-name (python-shell-get-process-name nil))
2075 (dedicated-proc-name (python-shell-get-process-name t))
2076 (global-shell-buffer
2077 (python-shell-make-comint
2078 (python-shell-parse-command) global-proc-name))
2079 (dedicated-shell-buffer
2080 (python-shell-make-comint
2081 (python-shell-parse-command) dedicated-proc-name))
2082 (global-process (get-buffer-process global-shell-buffer))
2083 (dedicated-process (get-buffer-process dedicated-shell-buffer)))
2084 (unwind-protect
2085 (progn
2086 (set-process-query-on-exit-flag global-process nil)
2087 (set-process-query-on-exit-flag dedicated-process nil)
2088 ;; Prefer dedicated if global also exists.
2089 (should (equal (python-shell-get-process) dedicated-process))
2090 (kill-buffer dedicated-shell-buffer)
2091 ;; If there's only global, use it.
2092 (should (equal (python-shell-get-process) global-process))
2093 (kill-buffer global-shell-buffer)
2094 ;; No buffer available.
2095 (should (not (python-shell-get-process))))
2096 (ignore-errors (kill-buffer global-shell-buffer))
2097 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
2098
2099 (ert-deftest python-shell-get-or-create-process-1 ()
2100 "Check shell dedicated process creation."
2101 (skip-unless (executable-find python-tests-shell-interpreter))
2102 (python-tests-with-temp-file
2103 ""
2104 (let* ((cmd
2105 (concat (executable-find python-tests-shell-interpreter) " -i"))
2106 (use-dialog-box)
2107 (dedicated-process-name (python-shell-get-process-name t))
2108 (dedicated-process (python-shell-get-or-create-process cmd t))
2109 (dedicated-shell-buffer (process-buffer dedicated-process)))
2110 (unwind-protect
2111 (progn
2112 (set-process-query-on-exit-flag dedicated-process nil)
2113 ;; should be dedicated.
2114 (should (equal (process-name dedicated-process)
2115 dedicated-process-name))
2116 (kill-buffer dedicated-shell-buffer)
2117 ;; Check there are no processes for current buffer.
2118 (should (not (python-shell-get-process))))
2119 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
2120
2121 (ert-deftest python-shell-get-or-create-process-2 ()
2122 "Check shell global process creation."
2123 (skip-unless (executable-find python-tests-shell-interpreter))
2124 (python-tests-with-temp-file
2125 ""
2126 (let* ((cmd
2127 (concat (executable-find python-tests-shell-interpreter) " -i"))
2128 (use-dialog-box)
2129 (process-name (python-shell-get-process-name nil))
2130 (process (python-shell-get-or-create-process cmd))
2131 (shell-buffer (process-buffer process)))
2132 (unwind-protect
2133 (progn
2134 (set-process-query-on-exit-flag process nil)
2135 ;; should be global.
2136 (should (equal (process-name process) process-name))
2137 (kill-buffer shell-buffer)
2138 ;; Check there are no processes for current buffer.
2139 (should (not (python-shell-get-process))))
2140 (ignore-errors (kill-buffer shell-buffer))))))
2141
2142 (ert-deftest python-shell-get-or-create-process-3 ()
2143 "Check shell dedicated/global process preference."
2144 (skip-unless (executable-find python-tests-shell-interpreter))
2145 (python-tests-with-temp-file
2146 ""
2147 (let* ((cmd
2148 (concat (executable-find python-tests-shell-interpreter) " -i"))
2149 (python-shell-interpreter python-tests-shell-interpreter)
2150 (use-dialog-box)
2151 (dedicated-process-name (python-shell-get-process-name t))
2152 (global-process)
2153 (dedicated-process))
2154 (progn
2155 ;; Create global process
2156 (run-python cmd nil)
2157 (setq global-process (get-buffer-process "*Python*"))
2158 (should global-process)
2159 (set-process-query-on-exit-flag global-process nil)
2160 ;; Create dedicated process
2161 (run-python cmd t)
2162 (setq dedicated-process (get-process dedicated-process-name))
2163 (should dedicated-process)
2164 (set-process-query-on-exit-flag dedicated-process nil)
2165 ;; Prefer dedicated.
2166 (should (equal (python-shell-get-or-create-process)
2167 dedicated-process))
2168 ;; Kill the dedicated so the global takes over.
2169 (kill-buffer (process-buffer dedicated-process))
2170 ;; Detect global.
2171 (should (equal (python-shell-get-or-create-process) global-process))
2172 ;; Kill the global.
2173 (kill-buffer (process-buffer global-process))
2174 ;; Check there are no processes for current buffer.
2175 (should (not (python-shell-get-process)))))))
2176
2177 (ert-deftest python-shell-internal-get-or-create-process-1 ()
2178 "Check internal shell process creation fallback."
2179 (skip-unless (executable-find python-tests-shell-interpreter))
2180 (python-tests-with-temp-file
2181 ""
2182 (should (not (process-live-p (python-shell-internal-get-process-name))))
2183 (let* ((python-shell-interpreter
2184 (executable-find python-tests-shell-interpreter))
2185 (internal-process-name (python-shell-internal-get-process-name))
2186 (internal-process (python-shell-internal-get-or-create-process))
2187 (internal-shell-buffer (process-buffer internal-process)))
2188 (unwind-protect
2189 (progn
2190 (set-process-query-on-exit-flag internal-process nil)
2191 (should (equal (process-name internal-process)
2192 internal-process-name))
2193 (should (equal internal-process
2194 (python-shell-internal-get-or-create-process)))
2195 ;; Assert the internal process is not a user process
2196 (should (not (python-shell-get-process)))
2197 (kill-buffer internal-shell-buffer))
2198 (ignore-errors (kill-buffer internal-shell-buffer))))))
2199
2200 (ert-deftest python-shell-prompt-detect-1 ()
2201 "Check prompt autodetection."
2202 (skip-unless (executable-find python-tests-shell-interpreter))
2203 (let ((process-environment process-environment))
2204 ;; Ensure no startup file is enabled
2205 (setenv "PYTHONSTARTUP" "")
2206 (should python-shell-prompt-detect-enabled)
2207 (should (equal (python-shell-prompt-detect) '(">>> " "... " "")))))
2208
2209 (ert-deftest python-shell-prompt-detect-2 ()
2210 "Check prompt autodetection with startup file. Bug#17370."
2211 (skip-unless (executable-find python-tests-shell-interpreter))
2212 (let* ((process-environment process-environment)
2213 (startup-code (concat "import sys\n"
2214 "sys.ps1 = 'py> '\n"
2215 "sys.ps2 = '..> '\n"
2216 "sys.ps3 = 'out '\n"))
2217 (startup-file (python-shell--save-temp-file startup-code)))
2218 (unwind-protect
2219 (progn
2220 ;; Ensure startup file is enabled
2221 (setenv "PYTHONSTARTUP" startup-file)
2222 (should python-shell-prompt-detect-enabled)
2223 (should (equal (python-shell-prompt-detect) '("py> " "..> " "out "))))
2224 (ignore-errors (delete-file startup-file)))))
2225
2226 (ert-deftest python-shell-prompt-detect-3 ()
2227 "Check prompts are not autodetected when feature is disabled."
2228 (skip-unless (executable-find python-tests-shell-interpreter))
2229 (let ((process-environment process-environment)
2230 (python-shell-prompt-detect-enabled nil))
2231 ;; Ensure no startup file is enabled
2232 (should (not python-shell-prompt-detect-enabled))
2233 (should (not (python-shell-prompt-detect)))))
2234
2235 (ert-deftest python-shell-prompt-detect-4 ()
2236 "Check warning is shown when detection fails."
2237 (skip-unless (executable-find python-tests-shell-interpreter))
2238 (let* ((process-environment process-environment)
2239 ;; Trigger failure by removing prompts in the startup file
2240 (startup-code (concat "import sys\n"
2241 "sys.ps1 = ''\n"
2242 "sys.ps2 = ''\n"
2243 "sys.ps3 = ''\n"))
2244 (startup-file (python-shell--save-temp-file startup-code)))
2245 (unwind-protect
2246 (progn
2247 (kill-buffer (get-buffer-create "*Warnings*"))
2248 (should (not (get-buffer "*Warnings*")))
2249 (setenv "PYTHONSTARTUP" startup-file)
2250 (should python-shell-prompt-detect-failure-warning)
2251 (should python-shell-prompt-detect-enabled)
2252 (should (not (python-shell-prompt-detect)))
2253 (should (get-buffer "*Warnings*")))
2254 (ignore-errors (delete-file startup-file)))))
2255
2256 (ert-deftest python-shell-prompt-detect-5 ()
2257 "Check disabled warnings are not shown when detection fails."
2258 (skip-unless (executable-find python-tests-shell-interpreter))
2259 (let* ((process-environment process-environment)
2260 (startup-code (concat "import sys\n"
2261 "sys.ps1 = ''\n"
2262 "sys.ps2 = ''\n"
2263 "sys.ps3 = ''\n"))
2264 (startup-file (python-shell--save-temp-file startup-code))
2265 (python-shell-prompt-detect-failure-warning nil))
2266 (unwind-protect
2267 (progn
2268 (kill-buffer (get-buffer-create "*Warnings*"))
2269 (should (not (get-buffer "*Warnings*")))
2270 (setenv "PYTHONSTARTUP" startup-file)
2271 (should (not python-shell-prompt-detect-failure-warning))
2272 (should python-shell-prompt-detect-enabled)
2273 (should (not (python-shell-prompt-detect)))
2274 (should (not (get-buffer "*Warnings*"))))
2275 (ignore-errors (delete-file startup-file)))))
2276
2277 (ert-deftest python-shell-prompt-detect-6 ()
2278 "Warnings are not shown when detection is disabled."
2279 (skip-unless (executable-find python-tests-shell-interpreter))
2280 (let* ((process-environment process-environment)
2281 (startup-code (concat "import sys\n"
2282 "sys.ps1 = ''\n"
2283 "sys.ps2 = ''\n"
2284 "sys.ps3 = ''\n"))
2285 (startup-file (python-shell--save-temp-file startup-code))
2286 (python-shell-prompt-detect-failure-warning t)
2287 (python-shell-prompt-detect-enabled nil))
2288 (unwind-protect
2289 (progn
2290 (kill-buffer (get-buffer-create "*Warnings*"))
2291 (should (not (get-buffer "*Warnings*")))
2292 (setenv "PYTHONSTARTUP" startup-file)
2293 (should python-shell-prompt-detect-failure-warning)
2294 (should (not python-shell-prompt-detect-enabled))
2295 (should (not (python-shell-prompt-detect)))
2296 (should (not (get-buffer "*Warnings*"))))
2297 (ignore-errors (delete-file startup-file)))))
2298
2299 (ert-deftest python-shell-prompt-validate-regexps-1 ()
2300 "Check `python-shell-prompt-input-regexps' are validated."
2301 (let* ((python-shell-prompt-input-regexps '("\\("))
2302 (error-data (should-error (python-shell-prompt-validate-regexps)
2303 :type 'user-error)))
2304 (should
2305 (string= (cadr error-data)
2306 "Invalid regexp \\( in `python-shell-prompt-input-regexps'"))))
2307
2308 (ert-deftest python-shell-prompt-validate-regexps-2 ()
2309 "Check `python-shell-prompt-output-regexps' are validated."
2310 (let* ((python-shell-prompt-output-regexps '("\\("))
2311 (error-data (should-error (python-shell-prompt-validate-regexps)
2312 :type 'user-error)))
2313 (should
2314 (string= (cadr error-data)
2315 "Invalid regexp \\( in `python-shell-prompt-output-regexps'"))))
2316
2317 (ert-deftest python-shell-prompt-validate-regexps-3 ()
2318 "Check `python-shell-prompt-regexp' is validated."
2319 (let* ((python-shell-prompt-regexp "\\(")
2320 (error-data (should-error (python-shell-prompt-validate-regexps)
2321 :type 'user-error)))
2322 (should
2323 (string= (cadr error-data)
2324 "Invalid regexp \\( in `python-shell-prompt-regexp'"))))
2325
2326 (ert-deftest python-shell-prompt-validate-regexps-4 ()
2327 "Check `python-shell-prompt-block-regexp' is validated."
2328 (let* ((python-shell-prompt-block-regexp "\\(")
2329 (error-data (should-error (python-shell-prompt-validate-regexps)
2330 :type 'user-error)))
2331 (should
2332 (string= (cadr error-data)
2333 "Invalid regexp \\( in `python-shell-prompt-block-regexp'"))))
2334
2335 (ert-deftest python-shell-prompt-validate-regexps-5 ()
2336 "Check `python-shell-prompt-pdb-regexp' is validated."
2337 (let* ((python-shell-prompt-pdb-regexp "\\(")
2338 (error-data (should-error (python-shell-prompt-validate-regexps)
2339 :type 'user-error)))
2340 (should
2341 (string= (cadr error-data)
2342 "Invalid regexp \\( in `python-shell-prompt-pdb-regexp'"))))
2343
2344 (ert-deftest python-shell-prompt-validate-regexps-6 ()
2345 "Check `python-shell-prompt-output-regexp' is validated."
2346 (let* ((python-shell-prompt-output-regexp "\\(")
2347 (error-data (should-error (python-shell-prompt-validate-regexps)
2348 :type 'user-error)))
2349 (should
2350 (string= (cadr error-data)
2351 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2352
2353 (ert-deftest python-shell-prompt-validate-regexps-7 ()
2354 "Check default regexps are valid."
2355 ;; should not signal error
2356 (python-shell-prompt-validate-regexps))
2357
2358 (ert-deftest python-shell-prompt-set-calculated-regexps-1 ()
2359 "Check regexps are validated."
2360 (let* ((python-shell-prompt-output-regexp '("\\("))
2361 (python-shell--prompt-calculated-input-regexp nil)
2362 (python-shell--prompt-calculated-output-regexp nil)
2363 (python-shell-prompt-detect-enabled nil)
2364 (error-data (should-error (python-shell-prompt-set-calculated-regexps)
2365 :type 'user-error)))
2366 (should
2367 (string= (cadr error-data)
2368 "Invalid regexp \\( in `python-shell-prompt-output-regexp'"))))
2369
2370 (ert-deftest python-shell-prompt-set-calculated-regexps-2 ()
2371 "Check `python-shell-prompt-input-regexps' are set."
2372 (let* ((python-shell-prompt-input-regexps '("my" "prompt"))
2373 (python-shell-prompt-output-regexps '(""))
2374 (python-shell-prompt-regexp "")
2375 (python-shell-prompt-block-regexp "")
2376 (python-shell-prompt-pdb-regexp "")
2377 (python-shell-prompt-output-regexp "")
2378 (python-shell--prompt-calculated-input-regexp nil)
2379 (python-shell--prompt-calculated-output-regexp nil)
2380 (python-shell-prompt-detect-enabled nil))
2381 (python-shell-prompt-set-calculated-regexps)
2382 (should (string= python-shell--prompt-calculated-input-regexp
2383 "^\\(prompt\\|my\\|\\)"))))
2384
2385 (ert-deftest python-shell-prompt-set-calculated-regexps-3 ()
2386 "Check `python-shell-prompt-output-regexps' are set."
2387 (let* ((python-shell-prompt-input-regexps '(""))
2388 (python-shell-prompt-output-regexps '("my" "prompt"))
2389 (python-shell-prompt-regexp "")
2390 (python-shell-prompt-block-regexp "")
2391 (python-shell-prompt-pdb-regexp "")
2392 (python-shell-prompt-output-regexp "")
2393 (python-shell--prompt-calculated-input-regexp nil)
2394 (python-shell--prompt-calculated-output-regexp nil)
2395 (python-shell-prompt-detect-enabled nil))
2396 (python-shell-prompt-set-calculated-regexps)
2397 (should (string= python-shell--prompt-calculated-output-regexp
2398 "^\\(prompt\\|my\\|\\)"))))
2399
2400 (ert-deftest python-shell-prompt-set-calculated-regexps-4 ()
2401 "Check user defined prompts are set."
2402 (let* ((python-shell-prompt-input-regexps '(""))
2403 (python-shell-prompt-output-regexps '(""))
2404 (python-shell-prompt-regexp "prompt")
2405 (python-shell-prompt-block-regexp "block")
2406 (python-shell-prompt-pdb-regexp "pdb")
2407 (python-shell-prompt-output-regexp "output")
2408 (python-shell--prompt-calculated-input-regexp nil)
2409 (python-shell--prompt-calculated-output-regexp nil)
2410 (python-shell-prompt-detect-enabled nil))
2411 (python-shell-prompt-set-calculated-regexps)
2412 (should (string= python-shell--prompt-calculated-input-regexp
2413 "^\\(prompt\\|block\\|pdb\\|\\)"))
2414 (should (string= python-shell--prompt-calculated-output-regexp
2415 "^\\(output\\|\\)"))))
2416
2417 (ert-deftest python-shell-prompt-set-calculated-regexps-5 ()
2418 "Check order of regexps (larger first)."
2419 (let* ((python-shell-prompt-input-regexps '("extralargeinputprompt" "sml"))
2420 (python-shell-prompt-output-regexps '("extralargeoutputprompt" "sml"))
2421 (python-shell-prompt-regexp "in")
2422 (python-shell-prompt-block-regexp "block")
2423 (python-shell-prompt-pdb-regexp "pdf")
2424 (python-shell-prompt-output-regexp "output")
2425 (python-shell--prompt-calculated-input-regexp nil)
2426 (python-shell--prompt-calculated-output-regexp nil)
2427 (python-shell-prompt-detect-enabled nil))
2428 (python-shell-prompt-set-calculated-regexps)
2429 (should (string= python-shell--prompt-calculated-input-regexp
2430 "^\\(extralargeinputprompt\\|block\\|pdf\\|sml\\|in\\)"))
2431 (should (string= python-shell--prompt-calculated-output-regexp
2432 "^\\(extralargeoutputprompt\\|output\\|sml\\)"))))
2433
2434 (ert-deftest python-shell-prompt-set-calculated-regexps-6 ()
2435 "Check detected prompts are included `regexp-quote'd."
2436 (skip-unless (executable-find python-tests-shell-interpreter))
2437 (let* ((python-shell-prompt-input-regexps '(""))
2438 (python-shell-prompt-output-regexps '(""))
2439 (python-shell-prompt-regexp "")
2440 (python-shell-prompt-block-regexp "")
2441 (python-shell-prompt-pdb-regexp "")
2442 (python-shell-prompt-output-regexp "")
2443 (python-shell--prompt-calculated-input-regexp nil)
2444 (python-shell--prompt-calculated-output-regexp nil)
2445 (python-shell-prompt-detect-enabled t)
2446 (process-environment process-environment)
2447 (startup-code (concat "import sys\n"
2448 "sys.ps1 = 'p.> '\n"
2449 "sys.ps2 = '..> '\n"
2450 "sys.ps3 = 'o.t '\n"))
2451 (startup-file (python-shell--save-temp-file startup-code)))
2452 (unwind-protect
2453 (progn
2454 (setenv "PYTHONSTARTUP" startup-file)
2455 (python-shell-prompt-set-calculated-regexps)
2456 (should (string= python-shell--prompt-calculated-input-regexp
2457 "^\\(\\.\\.> \\|p\\.> \\|\\)"))
2458 (should (string= python-shell--prompt-calculated-output-regexp
2459 "^\\(o\\.t \\|\\)")))
2460 (ignore-errors (delete-file startup-file)))))
2461
2462 (ert-deftest python-shell-buffer-substring-1 ()
2463 "Selecting a substring of the whole buffer must match its contents."
2464 (python-tests-with-temp-buffer
2465 "
2466 class Foo(models.Model):
2467 pass
2468
2469
2470 class Bar(models.Model):
2471 pass
2472 "
2473 (should (string= (buffer-string)
2474 (python-shell-buffer-substring (point-min) (point-max))))))
2475
2476 (ert-deftest python-shell-buffer-substring-2 ()
2477 "Main block should be removed if NOMAIN is non-nil."
2478 (python-tests-with-temp-buffer
2479 "
2480 class Foo(models.Model):
2481 pass
2482
2483 class Bar(models.Model):
2484 pass
2485
2486 if __name__ == \"__main__\":
2487 foo = Foo()
2488 print (foo)
2489 "
2490 (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
2491 "
2492 class Foo(models.Model):
2493 pass
2494
2495 class Bar(models.Model):
2496 pass
2497
2498
2499
2500
2501 "))))
2502
2503 (ert-deftest python-shell-buffer-substring-3 ()
2504 "Main block should be removed if NOMAIN is non-nil."
2505 (python-tests-with-temp-buffer
2506 "
2507 class Foo(models.Model):
2508 pass
2509
2510 if __name__ == \"__main__\":
2511 foo = Foo()
2512 print (foo)
2513
2514 class Bar(models.Model):
2515 pass
2516 "
2517 (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
2518 "
2519 class Foo(models.Model):
2520 pass
2521
2522
2523
2524
2525
2526 class Bar(models.Model):
2527 pass
2528 "))))
2529
2530 (ert-deftest python-shell-buffer-substring-4 ()
2531 "Coding cookie should be added for substrings."
2532 (python-tests-with-temp-buffer
2533 "# coding: latin-1
2534
2535 class Foo(models.Model):
2536 pass
2537
2538 if __name__ == \"__main__\":
2539 foo = Foo()
2540 print (foo)
2541
2542 class Bar(models.Model):
2543 pass
2544 "
2545 (should (string= (python-shell-buffer-substring
2546 (python-tests-look-at "class Foo(models.Model):")
2547 (progn (python-nav-forward-sexp) (point)))
2548 "# -*- coding: latin-1 -*-
2549
2550 class Foo(models.Model):
2551 pass"))))
2552
2553 (ert-deftest python-shell-buffer-substring-5 ()
2554 "The proper amount of blank lines is added for a substring."
2555 (python-tests-with-temp-buffer
2556 "# coding: latin-1
2557
2558 class Foo(models.Model):
2559 pass
2560
2561 if __name__ == \"__main__\":
2562 foo = Foo()
2563 print (foo)
2564
2565 class Bar(models.Model):
2566 pass
2567 "
2568 (should (string= (python-shell-buffer-substring
2569 (python-tests-look-at "class Bar(models.Model):")
2570 (progn (python-nav-forward-sexp) (point)))
2571 "# -*- coding: latin-1 -*-
2572
2573
2574
2575
2576
2577
2578
2579
2580 class Bar(models.Model):
2581 pass"))))
2582
2583 (ert-deftest python-shell-buffer-substring-6 ()
2584 "Handle substring with coding cookie in the second line."
2585 (python-tests-with-temp-buffer
2586 "
2587 # coding: latin-1
2588
2589 class Foo(models.Model):
2590 pass
2591
2592 if __name__ == \"__main__\":
2593 foo = Foo()
2594 print (foo)
2595
2596 class Bar(models.Model):
2597 pass
2598 "
2599 (should (string= (python-shell-buffer-substring
2600 (python-tests-look-at "# coding: latin-1")
2601 (python-tests-look-at "if __name__ == \"__main__\":"))
2602 "# -*- coding: latin-1 -*-
2603
2604
2605 class Foo(models.Model):
2606 pass
2607
2608 "))))
2609
2610 (ert-deftest python-shell-buffer-substring-7 ()
2611 "Ensure first coding cookie gets precedence."
2612 (python-tests-with-temp-buffer
2613 "# coding: utf-8
2614 # coding: latin-1
2615
2616 class Foo(models.Model):
2617 pass
2618
2619 if __name__ == \"__main__\":
2620 foo = Foo()
2621 print (foo)
2622
2623 class Bar(models.Model):
2624 pass
2625 "
2626 (should (string= (python-shell-buffer-substring
2627 (python-tests-look-at "# coding: latin-1")
2628 (python-tests-look-at "if __name__ == \"__main__\":"))
2629 "# -*- coding: utf-8 -*-
2630
2631
2632 class Foo(models.Model):
2633 pass
2634
2635 "))))
2636
2637 (ert-deftest python-shell-buffer-substring-8 ()
2638 "Ensure first coding cookie gets precedence when sending whole buffer."
2639 (python-tests-with-temp-buffer
2640 "# coding: utf-8
2641 # coding: latin-1
2642
2643 class Foo(models.Model):
2644 pass
2645 "
2646 (should (string= (python-shell-buffer-substring (point-min) (point-max))
2647 "# coding: utf-8
2648
2649
2650 class Foo(models.Model):
2651 pass
2652 "))))
2653
2654 (ert-deftest python-shell-buffer-substring-9 ()
2655 "Check substring starting from `point-min'."
2656 (python-tests-with-temp-buffer
2657 "# coding: utf-8
2658
2659 class Foo(models.Model):
2660 pass
2661
2662 class Bar(models.Model):
2663 pass
2664 "
2665 (should (string= (python-shell-buffer-substring
2666 (point-min)
2667 (python-tests-look-at "class Bar(models.Model):"))
2668 "# coding: utf-8
2669
2670 class Foo(models.Model):
2671 pass
2672
2673 "))))
2674
2675 \f
2676 ;;; Shell completion
2677
2678 \f
2679 ;;; PDB Track integration
2680
2681 \f
2682 ;;; Symbol completion
2683
2684 \f
2685 ;;; Fill paragraph
2686
2687 \f
2688 ;;; Skeletons
2689
2690 \f
2691 ;;; FFAP
2692
2693 \f
2694 ;;; Code check
2695
2696 \f
2697 ;;; Eldoc
2698
2699 \f
2700 ;;; Imenu
2701
2702 (ert-deftest python-imenu-create-index-1 ()
2703 (python-tests-with-temp-buffer
2704 "
2705 class Foo(models.Model):
2706 pass
2707
2708
2709 class Bar(models.Model):
2710 pass
2711
2712
2713 def decorator(arg1, arg2, arg3):
2714 '''print decorated function call data to stdout.
2715
2716 Usage:
2717
2718 @decorator('arg1', 'arg2')
2719 def func(a, b, c=True):
2720 pass
2721 '''
2722
2723 def wrap(f):
2724 print ('wrap')
2725 def wrapped_f(*args):
2726 print ('wrapped_f')
2727 print ('Decorator arguments:', arg1, arg2, arg3)
2728 f(*args)
2729 print ('called f(*args)')
2730 return wrapped_f
2731 return wrap
2732
2733
2734 class Baz(object):
2735
2736 def a(self):
2737 pass
2738
2739 def b(self):
2740 pass
2741
2742 class Frob(object):
2743
2744 def c(self):
2745 pass
2746 "
2747 (goto-char (point-max))
2748 (should (equal
2749 (list
2750 (cons "Foo (class)" (copy-marker 2))
2751 (cons "Bar (class)" (copy-marker 38))
2752 (list
2753 "decorator (def)"
2754 (cons "*function definition*" (copy-marker 74))
2755 (list
2756 "wrap (def)"
2757 (cons "*function definition*" (copy-marker 254))
2758 (cons "wrapped_f (def)" (copy-marker 294))))
2759 (list
2760 "Baz (class)"
2761 (cons "*class definition*" (copy-marker 519))
2762 (cons "a (def)" (copy-marker 539))
2763 (cons "b (def)" (copy-marker 570))
2764 (list
2765 "Frob (class)"
2766 (cons "*class definition*" (copy-marker 601))
2767 (cons "c (def)" (copy-marker 626)))))
2768 (python-imenu-create-index)))))
2769
2770 (ert-deftest python-imenu-create-index-2 ()
2771 (python-tests-with-temp-buffer
2772 "
2773 class Foo(object):
2774 def foo(self):
2775 def foo1():
2776 pass
2777
2778 def foobar(self):
2779 pass
2780 "
2781 (goto-char (point-max))
2782 (should (equal
2783 (list
2784 (list
2785 "Foo (class)"
2786 (cons "*class definition*" (copy-marker 2))
2787 (list
2788 "foo (def)"
2789 (cons "*function definition*" (copy-marker 21))
2790 (cons "foo1 (def)" (copy-marker 40)))
2791 (cons "foobar (def)" (copy-marker 78))))
2792 (python-imenu-create-index)))))
2793
2794 (ert-deftest python-imenu-create-index-3 ()
2795 (python-tests-with-temp-buffer
2796 "
2797 class Foo(object):
2798 def foo(self):
2799 def foo1():
2800 pass
2801 def foo2():
2802 pass
2803 "
2804 (goto-char (point-max))
2805 (should (equal
2806 (list
2807 (list
2808 "Foo (class)"
2809 (cons "*class definition*" (copy-marker 2))
2810 (list
2811 "foo (def)"
2812 (cons "*function definition*" (copy-marker 21))
2813 (cons "foo1 (def)" (copy-marker 40))
2814 (cons "foo2 (def)" (copy-marker 77)))))
2815 (python-imenu-create-index)))))
2816
2817 (ert-deftest python-imenu-create-index-4 ()
2818 (python-tests-with-temp-buffer
2819 "
2820 class Foo(object):
2821 class Bar(object):
2822 def __init__(self):
2823 pass
2824
2825 def __str__(self):
2826 pass
2827
2828 def __init__(self):
2829 pass
2830 "
2831 (goto-char (point-max))
2832 (should (equal
2833 (list
2834 (list
2835 "Foo (class)"
2836 (cons "*class definition*" (copy-marker 2))
2837 (list
2838 "Bar (class)"
2839 (cons "*class definition*" (copy-marker 21))
2840 (cons "__init__ (def)" (copy-marker 44))
2841 (cons "__str__ (def)" (copy-marker 90)))
2842 (cons "__init__ (def)" (copy-marker 135))))
2843 (python-imenu-create-index)))))
2844
2845 (ert-deftest python-imenu-create-flat-index-1 ()
2846 (python-tests-with-temp-buffer
2847 "
2848 class Foo(models.Model):
2849 pass
2850
2851
2852 class Bar(models.Model):
2853 pass
2854
2855
2856 def decorator(arg1, arg2, arg3):
2857 '''print decorated function call data to stdout.
2858
2859 Usage:
2860
2861 @decorator('arg1', 'arg2')
2862 def func(a, b, c=True):
2863 pass
2864 '''
2865
2866 def wrap(f):
2867 print ('wrap')
2868 def wrapped_f(*args):
2869 print ('wrapped_f')
2870 print ('Decorator arguments:', arg1, arg2, arg3)
2871 f(*args)
2872 print ('called f(*args)')
2873 return wrapped_f
2874 return wrap
2875
2876
2877 class Baz(object):
2878
2879 def a(self):
2880 pass
2881
2882 def b(self):
2883 pass
2884
2885 class Frob(object):
2886
2887 def c(self):
2888 pass
2889 "
2890 (goto-char (point-max))
2891 (should (equal
2892 (list (cons "Foo" (copy-marker 2))
2893 (cons "Bar" (copy-marker 38))
2894 (cons "decorator" (copy-marker 74))
2895 (cons "decorator.wrap" (copy-marker 254))
2896 (cons "decorator.wrap.wrapped_f" (copy-marker 294))
2897 (cons "Baz" (copy-marker 519))
2898 (cons "Baz.a" (copy-marker 539))
2899 (cons "Baz.b" (copy-marker 570))
2900 (cons "Baz.Frob" (copy-marker 601))
2901 (cons "Baz.Frob.c" (copy-marker 626)))
2902 (python-imenu-create-flat-index)))))
2903
2904 (ert-deftest python-imenu-create-flat-index-2 ()
2905 (python-tests-with-temp-buffer
2906 "
2907 class Foo(object):
2908 class Bar(object):
2909 def __init__(self):
2910 pass
2911
2912 def __str__(self):
2913 pass
2914
2915 def __init__(self):
2916 pass
2917 "
2918 (goto-char (point-max))
2919 (should (equal
2920 (list
2921 (cons "Foo" (copy-marker 2))
2922 (cons "Foo.Bar" (copy-marker 21))
2923 (cons "Foo.Bar.__init__" (copy-marker 44))
2924 (cons "Foo.Bar.__str__" (copy-marker 90))
2925 (cons "Foo.__init__" (copy-marker 135)))
2926 (python-imenu-create-flat-index)))))
2927
2928 \f
2929 ;;; Misc helpers
2930
2931 (ert-deftest python-info-current-defun-1 ()
2932 (python-tests-with-temp-buffer
2933 "
2934 def foo(a, b):
2935 "
2936 (forward-line 1)
2937 (should (string= "foo" (python-info-current-defun)))
2938 (should (string= "def foo" (python-info-current-defun t)))
2939 (forward-line 1)
2940 (should (not (python-info-current-defun)))
2941 (indent-for-tab-command)
2942 (should (string= "foo" (python-info-current-defun)))
2943 (should (string= "def foo" (python-info-current-defun t)))))
2944
2945 (ert-deftest python-info-current-defun-2 ()
2946 (python-tests-with-temp-buffer
2947 "
2948 class C(object):
2949
2950 def m(self):
2951 if True:
2952 return [i for i in range(3)]
2953 else:
2954 return []
2955
2956 def b():
2957 do_b()
2958
2959 def a():
2960 do_a()
2961
2962 def c(self):
2963 do_c()
2964 "
2965 (forward-line 1)
2966 (should (string= "C" (python-info-current-defun)))
2967 (should (string= "class C" (python-info-current-defun t)))
2968 (python-tests-look-at "return [i for ")
2969 (should (string= "C.m" (python-info-current-defun)))
2970 (should (string= "def C.m" (python-info-current-defun t)))
2971 (python-tests-look-at "def b():")
2972 (should (string= "C.m.b" (python-info-current-defun)))
2973 (should (string= "def C.m.b" (python-info-current-defun t)))
2974 (forward-line 2)
2975 (indent-for-tab-command)
2976 (python-indent-dedent-line-backspace 1)
2977 (should (string= "C.m" (python-info-current-defun)))
2978 (should (string= "def C.m" (python-info-current-defun t)))
2979 (python-tests-look-at "def c(self):")
2980 (forward-line -1)
2981 (indent-for-tab-command)
2982 (should (string= "C.m.a" (python-info-current-defun)))
2983 (should (string= "def C.m.a" (python-info-current-defun t)))
2984 (python-indent-dedent-line-backspace 1)
2985 (should (string= "C.m" (python-info-current-defun)))
2986 (should (string= "def C.m" (python-info-current-defun t)))
2987 (python-indent-dedent-line-backspace 1)
2988 (should (string= "C" (python-info-current-defun)))
2989 (should (string= "class C" (python-info-current-defun t)))
2990 (python-tests-look-at "def c(self):")
2991 (should (string= "C.c" (python-info-current-defun)))
2992 (should (string= "def C.c" (python-info-current-defun t)))
2993 (python-tests-look-at "do_c()")
2994 (should (string= "C.c" (python-info-current-defun)))
2995 (should (string= "def C.c" (python-info-current-defun t)))))
2996
2997 (ert-deftest python-info-current-defun-3 ()
2998 (python-tests-with-temp-buffer
2999 "
3000 def decoratorFunctionWithArguments(arg1, arg2, arg3):
3001 '''print decorated function call data to stdout.
3002
3003 Usage:
3004
3005 @decoratorFunctionWithArguments('arg1', 'arg2')
3006 def func(a, b, c=True):
3007 pass
3008 '''
3009
3010 def wwrap(f):
3011 print 'Inside wwrap()'
3012 def wrapped_f(*args):
3013 print 'Inside wrapped_f()'
3014 print 'Decorator arguments:', arg1, arg2, arg3
3015 f(*args)
3016 print 'After f(*args)'
3017 return wrapped_f
3018 return wwrap
3019 "
3020 (python-tests-look-at "def wwrap(f):")
3021 (forward-line -1)
3022 (should (not (python-info-current-defun)))
3023 (indent-for-tab-command 1)
3024 (should (string= (python-info-current-defun)
3025 "decoratorFunctionWithArguments"))
3026 (should (string= (python-info-current-defun t)
3027 "def decoratorFunctionWithArguments"))
3028 (python-tests-look-at "def wrapped_f(*args):")
3029 (should (string= (python-info-current-defun)
3030 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
3031 (should (string= (python-info-current-defun t)
3032 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
3033 (python-tests-look-at "return wrapped_f")
3034 (should (string= (python-info-current-defun)
3035 "decoratorFunctionWithArguments.wwrap"))
3036 (should (string= (python-info-current-defun t)
3037 "def decoratorFunctionWithArguments.wwrap"))
3038 (end-of-line 1)
3039 (python-tests-look-at "return wwrap")
3040 (should (string= (python-info-current-defun)
3041 "decoratorFunctionWithArguments"))
3042 (should (string= (python-info-current-defun t)
3043 "def decoratorFunctionWithArguments"))))
3044
3045 (ert-deftest python-info-current-symbol-1 ()
3046 (python-tests-with-temp-buffer
3047 "
3048 class C(object):
3049
3050 def m(self):
3051 self.c()
3052
3053 def c(self):
3054 print ('a')
3055 "
3056 (python-tests-look-at "self.c()")
3057 (should (string= "self.c" (python-info-current-symbol)))
3058 (should (string= "C.c" (python-info-current-symbol t)))))
3059
3060 (ert-deftest python-info-current-symbol-2 ()
3061 (python-tests-with-temp-buffer
3062 "
3063 class C(object):
3064
3065 class M(object):
3066
3067 def a(self):
3068 self.c()
3069
3070 def c(self):
3071 pass
3072 "
3073 (python-tests-look-at "self.c()")
3074 (should (string= "self.c" (python-info-current-symbol)))
3075 (should (string= "C.M.c" (python-info-current-symbol t)))))
3076
3077 (ert-deftest python-info-current-symbol-3 ()
3078 "Keywords should not be considered symbols."
3079 :expected-result :failed
3080 (python-tests-with-temp-buffer
3081 "
3082 class C(object):
3083 pass
3084 "
3085 ;; FIXME: keywords are not symbols.
3086 (python-tests-look-at "class C")
3087 (should (not (python-info-current-symbol)))
3088 (should (not (python-info-current-symbol t)))
3089 (python-tests-look-at "C(object)")
3090 (should (string= "C" (python-info-current-symbol)))
3091 (should (string= "class C" (python-info-current-symbol t)))))
3092
3093 (ert-deftest python-info-statement-starts-block-p-1 ()
3094 (python-tests-with-temp-buffer
3095 "
3096 def long_function_name(
3097 var_one, var_two, var_three,
3098 var_four):
3099 print (var_one)
3100 "
3101 (python-tests-look-at "def long_function_name")
3102 (should (python-info-statement-starts-block-p))
3103 (python-tests-look-at "print (var_one)")
3104 (python-util-forward-comment -1)
3105 (should (python-info-statement-starts-block-p))))
3106
3107 (ert-deftest python-info-statement-starts-block-p-2 ()
3108 (python-tests-with-temp-buffer
3109 "
3110 if width == 0 and height == 0 and \\\\
3111 color == 'red' and emphasis == 'strong' or \\\\
3112 highlight > 100:
3113 raise ValueError('sorry, you lose')
3114 "
3115 (python-tests-look-at "if width == 0 and")
3116 (should (python-info-statement-starts-block-p))
3117 (python-tests-look-at "raise ValueError(")
3118 (python-util-forward-comment -1)
3119 (should (python-info-statement-starts-block-p))))
3120
3121 (ert-deftest python-info-statement-ends-block-p-1 ()
3122 (python-tests-with-temp-buffer
3123 "
3124 def long_function_name(
3125 var_one, var_two, var_three,
3126 var_four):
3127 print (var_one)
3128 "
3129 (python-tests-look-at "print (var_one)")
3130 (should (python-info-statement-ends-block-p))))
3131
3132 (ert-deftest python-info-statement-ends-block-p-2 ()
3133 (python-tests-with-temp-buffer
3134 "
3135 if width == 0 and height == 0 and \\\\
3136 color == 'red' and emphasis == 'strong' or \\\\
3137 highlight > 100:
3138 raise ValueError(
3139 'sorry, you lose'
3140
3141 )
3142 "
3143 (python-tests-look-at "raise ValueError(")
3144 (should (python-info-statement-ends-block-p))))
3145
3146 (ert-deftest python-info-beginning-of-statement-p-1 ()
3147 (python-tests-with-temp-buffer
3148 "
3149 def long_function_name(
3150 var_one, var_two, var_three,
3151 var_four):
3152 print (var_one)
3153 "
3154 (python-tests-look-at "def long_function_name")
3155 (should (python-info-beginning-of-statement-p))
3156 (forward-char 10)
3157 (should (not (python-info-beginning-of-statement-p)))
3158 (python-tests-look-at "print (var_one)")
3159 (should (python-info-beginning-of-statement-p))
3160 (goto-char (line-beginning-position))
3161 (should (not (python-info-beginning-of-statement-p)))))
3162
3163 (ert-deftest python-info-beginning-of-statement-p-2 ()
3164 (python-tests-with-temp-buffer
3165 "
3166 if width == 0 and height == 0 and \\\\
3167 color == 'red' and emphasis == 'strong' or \\\\
3168 highlight > 100:
3169 raise ValueError(
3170 'sorry, you lose'
3171
3172 )
3173 "
3174 (python-tests-look-at "if width == 0 and")
3175 (should (python-info-beginning-of-statement-p))
3176 (forward-char 10)
3177 (should (not (python-info-beginning-of-statement-p)))
3178 (python-tests-look-at "raise ValueError(")
3179 (should (python-info-beginning-of-statement-p))
3180 (goto-char (line-beginning-position))
3181 (should (not (python-info-beginning-of-statement-p)))))
3182
3183 (ert-deftest python-info-end-of-statement-p-1 ()
3184 (python-tests-with-temp-buffer
3185 "
3186 def long_function_name(
3187 var_one, var_two, var_three,
3188 var_four):
3189 print (var_one)
3190 "
3191 (python-tests-look-at "def long_function_name")
3192 (should (not (python-info-end-of-statement-p)))
3193 (end-of-line)
3194 (should (not (python-info-end-of-statement-p)))
3195 (python-tests-look-at "print (var_one)")
3196 (python-util-forward-comment -1)
3197 (should (python-info-end-of-statement-p))
3198 (python-tests-look-at "print (var_one)")
3199 (should (not (python-info-end-of-statement-p)))
3200 (end-of-line)
3201 (should (python-info-end-of-statement-p))))
3202
3203 (ert-deftest python-info-end-of-statement-p-2 ()
3204 (python-tests-with-temp-buffer
3205 "
3206 if width == 0 and height == 0 and \\\\
3207 color == 'red' and emphasis == 'strong' or \\\\
3208 highlight > 100:
3209 raise ValueError(
3210 'sorry, you lose'
3211
3212 )
3213 "
3214 (python-tests-look-at "if width == 0 and")
3215 (should (not (python-info-end-of-statement-p)))
3216 (end-of-line)
3217 (should (not (python-info-end-of-statement-p)))
3218 (python-tests-look-at "raise ValueError(")
3219 (python-util-forward-comment -1)
3220 (should (python-info-end-of-statement-p))
3221 (python-tests-look-at "raise ValueError(")
3222 (should (not (python-info-end-of-statement-p)))
3223 (end-of-line)
3224 (should (not (python-info-end-of-statement-p)))
3225 (goto-char (point-max))
3226 (python-util-forward-comment -1)
3227 (should (python-info-end-of-statement-p))))
3228
3229 (ert-deftest python-info-beginning-of-block-p-1 ()
3230 (python-tests-with-temp-buffer
3231 "
3232 def long_function_name(
3233 var_one, var_two, var_three,
3234 var_four):
3235 print (var_one)
3236 "
3237 (python-tests-look-at "def long_function_name")
3238 (should (python-info-beginning-of-block-p))
3239 (python-tests-look-at "var_one, var_two, var_three,")
3240 (should (not (python-info-beginning-of-block-p)))
3241 (python-tests-look-at "print (var_one)")
3242 (should (not (python-info-beginning-of-block-p)))))
3243
3244 (ert-deftest python-info-beginning-of-block-p-2 ()
3245 (python-tests-with-temp-buffer
3246 "
3247 if width == 0 and height == 0 and \\\\
3248 color == 'red' and emphasis == 'strong' or \\\\
3249 highlight > 100:
3250 raise ValueError(
3251 'sorry, you lose'
3252
3253 )
3254 "
3255 (python-tests-look-at "if width == 0 and")
3256 (should (python-info-beginning-of-block-p))
3257 (python-tests-look-at "color == 'red' and emphasis")
3258 (should (not (python-info-beginning-of-block-p)))
3259 (python-tests-look-at "raise ValueError(")
3260 (should (not (python-info-beginning-of-block-p)))))
3261
3262 (ert-deftest python-info-end-of-block-p-1 ()
3263 (python-tests-with-temp-buffer
3264 "
3265 def long_function_name(
3266 var_one, var_two, var_three,
3267 var_four):
3268 print (var_one)
3269 "
3270 (python-tests-look-at "def long_function_name")
3271 (should (not (python-info-end-of-block-p)))
3272 (python-tests-look-at "var_one, var_two, var_three,")
3273 (should (not (python-info-end-of-block-p)))
3274 (python-tests-look-at "var_four):")
3275 (end-of-line)
3276 (should (not (python-info-end-of-block-p)))
3277 (python-tests-look-at "print (var_one)")
3278 (should (not (python-info-end-of-block-p)))
3279 (end-of-line 1)
3280 (should (python-info-end-of-block-p))))
3281
3282 (ert-deftest python-info-end-of-block-p-2 ()
3283 (python-tests-with-temp-buffer
3284 "
3285 if width == 0 and height == 0 and \\\\
3286 color == 'red' and emphasis == 'strong' or \\\\
3287 highlight > 100:
3288 raise ValueError(
3289 'sorry, you lose'
3290
3291 )
3292 "
3293 (python-tests-look-at "if width == 0 and")
3294 (should (not (python-info-end-of-block-p)))
3295 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3296 (should (not (python-info-end-of-block-p)))
3297 (python-tests-look-at "highlight > 100:")
3298 (end-of-line)
3299 (should (not (python-info-end-of-block-p)))
3300 (python-tests-look-at "raise ValueError(")
3301 (should (not (python-info-end-of-block-p)))
3302 (end-of-line 1)
3303 (should (not (python-info-end-of-block-p)))
3304 (goto-char (point-max))
3305 (python-util-forward-comment -1)
3306 (should (python-info-end-of-block-p))))
3307
3308 (ert-deftest python-info-dedenter-opening-block-position-1 ()
3309 (python-tests-with-temp-buffer
3310 "
3311 if request.user.is_authenticated():
3312 try:
3313 profile = request.user.get_profile()
3314 except Profile.DoesNotExist:
3315 profile = Profile.objects.create(user=request.user)
3316 else:
3317 if profile.stats:
3318 profile.recalculate_stats()
3319 else:
3320 profile.clear_stats()
3321 finally:
3322 profile.views += 1
3323 profile.save()
3324 "
3325 (python-tests-look-at "try:")
3326 (should (not (python-info-dedenter-opening-block-position)))
3327 (python-tests-look-at "except Profile.DoesNotExist:")
3328 (should (= (python-tests-look-at "try:" -1 t)
3329 (python-info-dedenter-opening-block-position)))
3330 (python-tests-look-at "else:")
3331 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -1 t)
3332 (python-info-dedenter-opening-block-position)))
3333 (python-tests-look-at "if profile.stats:")
3334 (should (not (python-info-dedenter-opening-block-position)))
3335 (python-tests-look-at "else:")
3336 (should (= (python-tests-look-at "if profile.stats:" -1 t)
3337 (python-info-dedenter-opening-block-position)))
3338 (python-tests-look-at "finally:")
3339 (should (= (python-tests-look-at "else:" -2 t)
3340 (python-info-dedenter-opening-block-position)))))
3341
3342 (ert-deftest python-info-dedenter-opening-block-position-2 ()
3343 (python-tests-with-temp-buffer
3344 "
3345 if request.user.is_authenticated():
3346 profile = Profile.objects.get_or_create(user=request.user)
3347 if profile.stats:
3348 profile.recalculate_stats()
3349
3350 data = {
3351 'else': 'do it'
3352 }
3353 'else'
3354 "
3355 (python-tests-look-at "'else': 'do it'")
3356 (should (not (python-info-dedenter-opening-block-position)))
3357 (python-tests-look-at "'else'")
3358 (should (not (python-info-dedenter-opening-block-position)))))
3359
3360 (ert-deftest python-info-dedenter-opening-block-position-3 ()
3361 (python-tests-with-temp-buffer
3362 "
3363 if save:
3364 try:
3365 write_to_disk(data)
3366 except IOError:
3367 msg = 'Error saving to disk'
3368 message(msg)
3369 logger.exception(msg)
3370 except Exception:
3371 if hide_details:
3372 logger.exception('Unhandled exception')
3373 else
3374 finally:
3375 data.free()
3376 "
3377 (python-tests-look-at "try:")
3378 (should (not (python-info-dedenter-opening-block-position)))
3379
3380 (python-tests-look-at "except IOError:")
3381 (should (= (python-tests-look-at "try:" -1 t)
3382 (python-info-dedenter-opening-block-position)))
3383
3384 (python-tests-look-at "except Exception:")
3385 (should (= (python-tests-look-at "except IOError:" -1 t)
3386 (python-info-dedenter-opening-block-position)))
3387
3388 (python-tests-look-at "if hide_details:")
3389 (should (not (python-info-dedenter-opening-block-position)))
3390
3391 ;; check indentation modifies the detected opening block
3392 (python-tests-look-at "else")
3393 (should (= (python-tests-look-at "if hide_details:" -1 t)
3394 (python-info-dedenter-opening-block-position)))
3395
3396 (indent-line-to 8)
3397 (should (= (python-tests-look-at "if hide_details:" -1 t)
3398 (python-info-dedenter-opening-block-position)))
3399
3400 (indent-line-to 4)
3401 (should (= (python-tests-look-at "except Exception:" -1 t)
3402 (python-info-dedenter-opening-block-position)))
3403
3404 (indent-line-to 0)
3405 (should (= (python-tests-look-at "if save:" -1 t)
3406 (python-info-dedenter-opening-block-position)))))
3407
3408 (ert-deftest python-info-dedenter-opening-block-positions-1 ()
3409 (python-tests-with-temp-buffer
3410 "
3411 if save:
3412 try:
3413 write_to_disk(data)
3414 except IOError:
3415 msg = 'Error saving to disk'
3416 message(msg)
3417 logger.exception(msg)
3418 except Exception:
3419 if hide_details:
3420 logger.exception('Unhandled exception')
3421 else
3422 finally:
3423 data.free()
3424 "
3425 (python-tests-look-at "try:")
3426 (should (not (python-info-dedenter-opening-block-positions)))
3427
3428 (python-tests-look-at "except IOError:")
3429 (should
3430 (equal (list
3431 (python-tests-look-at "try:" -1 t))
3432 (python-info-dedenter-opening-block-positions)))
3433
3434 (python-tests-look-at "except Exception:")
3435 (should
3436 (equal (list
3437 (python-tests-look-at "except IOError:" -1 t))
3438 (python-info-dedenter-opening-block-positions)))
3439
3440 (python-tests-look-at "if hide_details:")
3441 (should (not (python-info-dedenter-opening-block-positions)))
3442
3443 ;; check indentation does not modify the detected opening blocks
3444 (python-tests-look-at "else")
3445 (should
3446 (equal (list
3447 (python-tests-look-at "if hide_details:" -1 t)
3448 (python-tests-look-at "except Exception:" -1 t)
3449 (python-tests-look-at "if save:" -1 t))
3450 (python-info-dedenter-opening-block-positions)))
3451
3452 (indent-line-to 8)
3453 (should
3454 (equal (list
3455 (python-tests-look-at "if hide_details:" -1 t)
3456 (python-tests-look-at "except Exception:" -1 t)
3457 (python-tests-look-at "if save:" -1 t))
3458 (python-info-dedenter-opening-block-positions)))
3459
3460 (indent-line-to 4)
3461 (should
3462 (equal (list
3463 (python-tests-look-at "if hide_details:" -1 t)
3464 (python-tests-look-at "except Exception:" -1 t)
3465 (python-tests-look-at "if save:" -1 t))
3466 (python-info-dedenter-opening-block-positions)))
3467
3468 (indent-line-to 0)
3469 (should
3470 (equal (list
3471 (python-tests-look-at "if hide_details:" -1 t)
3472 (python-tests-look-at "except Exception:" -1 t)
3473 (python-tests-look-at "if save:" -1 t))
3474 (python-info-dedenter-opening-block-positions)))))
3475
3476 (ert-deftest python-info-dedenter-opening-block-positions-2 ()
3477 "Test detection of opening blocks for elif."
3478 (python-tests-with-temp-buffer
3479 "
3480 if var:
3481 if var2:
3482 something()
3483 elif var3:
3484 something_else()
3485 elif
3486 "
3487 (python-tests-look-at "elif var3:")
3488 (should
3489 (equal (list
3490 (python-tests-look-at "if var2:" -1 t)
3491 (python-tests-look-at "if var:" -1 t))
3492 (python-info-dedenter-opening-block-positions)))
3493
3494 (python-tests-look-at "elif\n")
3495 (should
3496 (equal (list
3497 (python-tests-look-at "elif var3:" -1 t)
3498 (python-tests-look-at "if var:" -1 t))
3499 (python-info-dedenter-opening-block-positions)))))
3500
3501 (ert-deftest python-info-dedenter-opening-block-positions-3 ()
3502 "Test detection of opening blocks for else."
3503 (python-tests-with-temp-buffer
3504 "
3505 try:
3506 something()
3507 except:
3508 if var:
3509 if var2:
3510 something()
3511 elif var3:
3512 something_else()
3513 else
3514
3515 if var4:
3516 while var5:
3517 var4.pop()
3518 else
3519
3520 for value in var6:
3521 if value > 0:
3522 print value
3523 else
3524 "
3525 (python-tests-look-at "else\n")
3526 (should
3527 (equal (list
3528 (python-tests-look-at "elif var3:" -1 t)
3529 (python-tests-look-at "if var:" -1 t)
3530 (python-tests-look-at "except:" -1 t))
3531 (python-info-dedenter-opening-block-positions)))
3532
3533 (python-tests-look-at "else\n")
3534 (should
3535 (equal (list
3536 (python-tests-look-at "while var5:" -1 t)
3537 (python-tests-look-at "if var4:" -1 t))
3538 (python-info-dedenter-opening-block-positions)))
3539
3540 (python-tests-look-at "else\n")
3541 (should
3542 (equal (list
3543 (python-tests-look-at "if value > 0:" -1 t)
3544 (python-tests-look-at "for value in var6:" -1 t)
3545 (python-tests-look-at "if var4:" -1 t))
3546 (python-info-dedenter-opening-block-positions)))))
3547
3548 (ert-deftest python-info-dedenter-opening-block-positions-4 ()
3549 "Test detection of opening blocks for except."
3550 (python-tests-with-temp-buffer
3551 "
3552 try:
3553 something()
3554 except ValueError:
3555 something_else()
3556 except
3557 "
3558 (python-tests-look-at "except ValueError:")
3559 (should
3560 (equal (list (python-tests-look-at "try:" -1 t))
3561 (python-info-dedenter-opening-block-positions)))
3562
3563 (python-tests-look-at "except\n")
3564 (should
3565 (equal (list (python-tests-look-at "except ValueError:" -1 t))
3566 (python-info-dedenter-opening-block-positions)))))
3567
3568 (ert-deftest python-info-dedenter-opening-block-positions-5 ()
3569 "Test detection of opening blocks for finally."
3570 (python-tests-with-temp-buffer
3571 "
3572 try:
3573 something()
3574 finally
3575
3576 try:
3577 something_else()
3578 except:
3579 logger.exception('something went wrong')
3580 finally
3581
3582 try:
3583 something_else_else()
3584 except Exception:
3585 logger.exception('something else went wrong')
3586 else:
3587 print ('all good')
3588 finally
3589 "
3590 (python-tests-look-at "finally\n")
3591 (should
3592 (equal (list (python-tests-look-at "try:" -1 t))
3593 (python-info-dedenter-opening-block-positions)))
3594
3595 (python-tests-look-at "finally\n")
3596 (should
3597 (equal (list (python-tests-look-at "except:" -1 t))
3598 (python-info-dedenter-opening-block-positions)))
3599
3600 (python-tests-look-at "finally\n")
3601 (should
3602 (equal (list (python-tests-look-at "else:" -1 t))
3603 (python-info-dedenter-opening-block-positions)))))
3604
3605 (ert-deftest python-info-dedenter-opening-block-message-1 ()
3606 "Test dedenters inside strings are ignored."
3607 (python-tests-with-temp-buffer
3608 "'''
3609 try:
3610 something()
3611 except:
3612 logger.exception('something went wrong')
3613 '''
3614 "
3615 (python-tests-look-at "except\n")
3616 (should (not (python-info-dedenter-opening-block-message)))))
3617
3618 (ert-deftest python-info-dedenter-opening-block-message-2 ()
3619 "Test except keyword."
3620 (python-tests-with-temp-buffer
3621 "
3622 try:
3623 something()
3624 except:
3625 logger.exception('something went wrong')
3626 "
3627 (python-tests-look-at "except:")
3628 (should (string=
3629 "Closes try:"
3630 (substring-no-properties
3631 (python-info-dedenter-opening-block-message))))
3632 (end-of-line)
3633 (should (string=
3634 "Closes try:"
3635 (substring-no-properties
3636 (python-info-dedenter-opening-block-message))))))
3637
3638 (ert-deftest python-info-dedenter-opening-block-message-3 ()
3639 "Test else keyword."
3640 (python-tests-with-temp-buffer
3641 "
3642 try:
3643 something()
3644 except:
3645 logger.exception('something went wrong')
3646 else:
3647 logger.debug('all good')
3648 "
3649 (python-tests-look-at "else:")
3650 (should (string=
3651 "Closes except:"
3652 (substring-no-properties
3653 (python-info-dedenter-opening-block-message))))
3654 (end-of-line)
3655 (should (string=
3656 "Closes except:"
3657 (substring-no-properties
3658 (python-info-dedenter-opening-block-message))))))
3659
3660 (ert-deftest python-info-dedenter-opening-block-message-4 ()
3661 "Test finally keyword."
3662 (python-tests-with-temp-buffer
3663 "
3664 try:
3665 something()
3666 except:
3667 logger.exception('something went wrong')
3668 else:
3669 logger.debug('all good')
3670 finally:
3671 clean()
3672 "
3673 (python-tests-look-at "finally:")
3674 (should (string=
3675 "Closes else:"
3676 (substring-no-properties
3677 (python-info-dedenter-opening-block-message))))
3678 (end-of-line)
3679 (should (string=
3680 "Closes else:"
3681 (substring-no-properties
3682 (python-info-dedenter-opening-block-message))))))
3683
3684 (ert-deftest python-info-dedenter-opening-block-message-5 ()
3685 "Test elif keyword."
3686 (python-tests-with-temp-buffer
3687 "
3688 if a:
3689 something()
3690 elif b:
3691 "
3692 (python-tests-look-at "elif b:")
3693 (should (string=
3694 "Closes if a:"
3695 (substring-no-properties
3696 (python-info-dedenter-opening-block-message))))
3697 (end-of-line)
3698 (should (string=
3699 "Closes if a:"
3700 (substring-no-properties
3701 (python-info-dedenter-opening-block-message))))))
3702
3703
3704 (ert-deftest python-info-dedenter-statement-p-1 ()
3705 "Test dedenters inside strings are ignored."
3706 (python-tests-with-temp-buffer
3707 "'''
3708 try:
3709 something()
3710 except:
3711 logger.exception('something went wrong')
3712 '''
3713 "
3714 (python-tests-look-at "except\n")
3715 (should (not (python-info-dedenter-statement-p)))))
3716
3717 (ert-deftest python-info-dedenter-statement-p-2 ()
3718 "Test except keyword."
3719 (python-tests-with-temp-buffer
3720 "
3721 try:
3722 something()
3723 except:
3724 logger.exception('something went wrong')
3725 "
3726 (python-tests-look-at "except:")
3727 (should (= (point) (python-info-dedenter-statement-p)))
3728 (end-of-line)
3729 (should (= (save-excursion
3730 (back-to-indentation)
3731 (point))
3732 (python-info-dedenter-statement-p)))))
3733
3734 (ert-deftest python-info-dedenter-statement-p-3 ()
3735 "Test else keyword."
3736 (python-tests-with-temp-buffer
3737 "
3738 try:
3739 something()
3740 except:
3741 logger.exception('something went wrong')
3742 else:
3743 logger.debug('all good')
3744 "
3745 (python-tests-look-at "else:")
3746 (should (= (point) (python-info-dedenter-statement-p)))
3747 (end-of-line)
3748 (should (= (save-excursion
3749 (back-to-indentation)
3750 (point))
3751 (python-info-dedenter-statement-p)))))
3752
3753 (ert-deftest python-info-dedenter-statement-p-4 ()
3754 "Test finally keyword."
3755 (python-tests-with-temp-buffer
3756 "
3757 try:
3758 something()
3759 except:
3760 logger.exception('something went wrong')
3761 else:
3762 logger.debug('all good')
3763 finally:
3764 clean()
3765 "
3766 (python-tests-look-at "finally:")
3767 (should (= (point) (python-info-dedenter-statement-p)))
3768 (end-of-line)
3769 (should (= (save-excursion
3770 (back-to-indentation)
3771 (point))
3772 (python-info-dedenter-statement-p)))))
3773
3774 (ert-deftest python-info-dedenter-statement-p-5 ()
3775 "Test elif keyword."
3776 (python-tests-with-temp-buffer
3777 "
3778 if a:
3779 something()
3780 elif b:
3781 "
3782 (python-tests-look-at "elif b:")
3783 (should (= (point) (python-info-dedenter-statement-p)))
3784 (end-of-line)
3785 (should (= (save-excursion
3786 (back-to-indentation)
3787 (point))
3788 (python-info-dedenter-statement-p)))))
3789
3790 (ert-deftest python-info-line-ends-backslash-p-1 ()
3791 (python-tests-with-temp-buffer
3792 "
3793 objects = Thing.objects.all() \\\\
3794 .filter(
3795 type='toy',
3796 status='bought'
3797 ) \\\\
3798 .aggregate(
3799 Sum('amount')
3800 ) \\\\
3801 .values_list()
3802 "
3803 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
3804 (should (python-info-line-ends-backslash-p 3))
3805 (should (python-info-line-ends-backslash-p 4))
3806 (should (python-info-line-ends-backslash-p 5))
3807 (should (python-info-line-ends-backslash-p 6)) ; ) \...
3808 (should (python-info-line-ends-backslash-p 7))
3809 (should (python-info-line-ends-backslash-p 8))
3810 (should (python-info-line-ends-backslash-p 9))
3811 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
3812
3813 (ert-deftest python-info-beginning-of-backslash-1 ()
3814 (python-tests-with-temp-buffer
3815 "
3816 objects = Thing.objects.all() \\\\
3817 .filter(
3818 type='toy',
3819 status='bought'
3820 ) \\\\
3821 .aggregate(
3822 Sum('amount')
3823 ) \\\\
3824 .values_list()
3825 "
3826 (let ((first 2)
3827 (second (python-tests-look-at ".filter("))
3828 (third (python-tests-look-at ".aggregate(")))
3829 (should (= first (python-info-beginning-of-backslash 2)))
3830 (should (= second (python-info-beginning-of-backslash 3)))
3831 (should (= second (python-info-beginning-of-backslash 4)))
3832 (should (= second (python-info-beginning-of-backslash 5)))
3833 (should (= second (python-info-beginning-of-backslash 6)))
3834 (should (= third (python-info-beginning-of-backslash 7)))
3835 (should (= third (python-info-beginning-of-backslash 8)))
3836 (should (= third (python-info-beginning-of-backslash 9)))
3837 (should (not (python-info-beginning-of-backslash 10))))))
3838
3839 (ert-deftest python-info-continuation-line-p-1 ()
3840 (python-tests-with-temp-buffer
3841 "
3842 if width == 0 and height == 0 and \\\\
3843 color == 'red' and emphasis == 'strong' or \\\\
3844 highlight > 100:
3845 raise ValueError(
3846 'sorry, you lose'
3847
3848 )
3849 "
3850 (python-tests-look-at "if width == 0 and height == 0 and")
3851 (should (not (python-info-continuation-line-p)))
3852 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3853 (should (python-info-continuation-line-p))
3854 (python-tests-look-at "highlight > 100:")
3855 (should (python-info-continuation-line-p))
3856 (python-tests-look-at "raise ValueError(")
3857 (should (not (python-info-continuation-line-p)))
3858 (python-tests-look-at "'sorry, you lose'")
3859 (should (python-info-continuation-line-p))
3860 (forward-line 1)
3861 (should (python-info-continuation-line-p))
3862 (python-tests-look-at ")")
3863 (should (python-info-continuation-line-p))
3864 (forward-line 1)
3865 (should (not (python-info-continuation-line-p)))))
3866
3867 (ert-deftest python-info-block-continuation-line-p-1 ()
3868 (python-tests-with-temp-buffer
3869 "
3870 if width == 0 and height == 0 and \\\\
3871 color == 'red' and emphasis == 'strong' or \\\\
3872 highlight > 100:
3873 raise ValueError(
3874 'sorry, you lose'
3875
3876 )
3877 "
3878 (python-tests-look-at "if width == 0 and")
3879 (should (not (python-info-block-continuation-line-p)))
3880 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
3881 (should (= (python-info-block-continuation-line-p)
3882 (python-tests-look-at "if width == 0 and" -1 t)))
3883 (python-tests-look-at "highlight > 100:")
3884 (should (not (python-info-block-continuation-line-p)))))
3885
3886 (ert-deftest python-info-block-continuation-line-p-2 ()
3887 (python-tests-with-temp-buffer
3888 "
3889 def foo(a,
3890 b,
3891 c):
3892 pass
3893 "
3894 (python-tests-look-at "def foo(a,")
3895 (should (not (python-info-block-continuation-line-p)))
3896 (python-tests-look-at "b,")
3897 (should (= (python-info-block-continuation-line-p)
3898 (python-tests-look-at "def foo(a," -1 t)))
3899 (python-tests-look-at "c):")
3900 (should (not (python-info-block-continuation-line-p)))))
3901
3902 (ert-deftest python-info-assignment-continuation-line-p-1 ()
3903 (python-tests-with-temp-buffer
3904 "
3905 data = foo(), bar() \\\\
3906 baz(), 4 \\\\
3907 5, 6
3908 "
3909 (python-tests-look-at "data = foo(), bar()")
3910 (should (not (python-info-assignment-continuation-line-p)))
3911 (python-tests-look-at "baz(), 4")
3912 (should (= (python-info-assignment-continuation-line-p)
3913 (python-tests-look-at "foo()," -1 t)))
3914 (python-tests-look-at "5, 6")
3915 (should (not (python-info-assignment-continuation-line-p)))))
3916
3917 (ert-deftest python-info-assignment-continuation-line-p-2 ()
3918 (python-tests-with-temp-buffer
3919 "
3920 data = (foo(), bar()
3921 baz(), 4
3922 5, 6)
3923 "
3924 (python-tests-look-at "data = (foo(), bar()")
3925 (should (not (python-info-assignment-continuation-line-p)))
3926 (python-tests-look-at "baz(), 4")
3927 (should (= (python-info-assignment-continuation-line-p)
3928 (python-tests-look-at "(foo()," -1 t)))
3929 (python-tests-look-at "5, 6)")
3930 (should (not (python-info-assignment-continuation-line-p)))))
3931
3932 (ert-deftest python-info-looking-at-beginning-of-defun-1 ()
3933 (python-tests-with-temp-buffer
3934 "
3935 def decorat0r(deff):
3936 '''decorates stuff.
3937
3938 @decorat0r
3939 def foo(arg):
3940 ...
3941 '''
3942 def wrap():
3943 deff()
3944 return wwrap
3945 "
3946 (python-tests-look-at "def decorat0r(deff):")
3947 (should (python-info-looking-at-beginning-of-defun))
3948 (python-tests-look-at "def foo(arg):")
3949 (should (not (python-info-looking-at-beginning-of-defun)))
3950 (python-tests-look-at "def wrap():")
3951 (should (python-info-looking-at-beginning-of-defun))
3952 (python-tests-look-at "deff()")
3953 (should (not (python-info-looking-at-beginning-of-defun)))))
3954
3955 (ert-deftest python-info-current-line-comment-p-1 ()
3956 (python-tests-with-temp-buffer
3957 "
3958 # this is a comment
3959 foo = True # another comment
3960 '#this is a string'
3961 if foo:
3962 # more comments
3963 print ('bar') # print bar
3964 "
3965 (python-tests-look-at "# this is a comment")
3966 (should (python-info-current-line-comment-p))
3967 (python-tests-look-at "foo = True # another comment")
3968 (should (not (python-info-current-line-comment-p)))
3969 (python-tests-look-at "'#this is a string'")
3970 (should (not (python-info-current-line-comment-p)))
3971 (python-tests-look-at "# more comments")
3972 (should (python-info-current-line-comment-p))
3973 (python-tests-look-at "print ('bar') # print bar")
3974 (should (not (python-info-current-line-comment-p)))))
3975
3976 (ert-deftest python-info-current-line-empty-p ()
3977 (python-tests-with-temp-buffer
3978 "
3979 # this is a comment
3980
3981 foo = True # another comment
3982 "
3983 (should (python-info-current-line-empty-p))
3984 (python-tests-look-at "# this is a comment")
3985 (should (not (python-info-current-line-empty-p)))
3986 (forward-line 1)
3987 (should (python-info-current-line-empty-p))))
3988
3989 (ert-deftest python-info-encoding-from-cookie-1 ()
3990 "Should detect it on first line."
3991 (python-tests-with-temp-buffer
3992 "# coding=latin-1
3993
3994 foo = True # another comment
3995 "
3996 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
3997
3998 (ert-deftest python-info-encoding-from-cookie-2 ()
3999 "Should detect it on second line."
4000 (python-tests-with-temp-buffer
4001 "
4002 # coding=latin-1
4003
4004 foo = True # another comment
4005 "
4006 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4007
4008 (ert-deftest python-info-encoding-from-cookie-3 ()
4009 "Should not be detected on third line (and following ones)."
4010 (python-tests-with-temp-buffer
4011 "
4012
4013 # coding=latin-1
4014 foo = True # another comment
4015 "
4016 (should (not (python-info-encoding-from-cookie)))))
4017
4018 (ert-deftest python-info-encoding-from-cookie-4 ()
4019 "Should detect Emacs style."
4020 (python-tests-with-temp-buffer
4021 "# -*- coding: latin-1 -*-
4022
4023 foo = True # another comment"
4024 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4025
4026 (ert-deftest python-info-encoding-from-cookie-5 ()
4027 "Should detect Vim style."
4028 (python-tests-with-temp-buffer
4029 "# vim: set fileencoding=latin-1 :
4030
4031 foo = True # another comment"
4032 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4033
4034 (ert-deftest python-info-encoding-from-cookie-6 ()
4035 "First cookie wins."
4036 (python-tests-with-temp-buffer
4037 "# -*- coding: iso-8859-1 -*-
4038 # vim: set fileencoding=latin-1 :
4039
4040 foo = True # another comment"
4041 (should (eq (python-info-encoding-from-cookie) 'iso-8859-1))))
4042
4043 (ert-deftest python-info-encoding-from-cookie-7 ()
4044 "First cookie wins."
4045 (python-tests-with-temp-buffer
4046 "# vim: set fileencoding=latin-1 :
4047 # -*- coding: iso-8859-1 -*-
4048
4049 foo = True # another comment"
4050 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4051
4052 (ert-deftest python-info-encoding-1 ()
4053 "Should return the detected encoding from cookie."
4054 (python-tests-with-temp-buffer
4055 "# vim: set fileencoding=latin-1 :
4056
4057 foo = True # another comment"
4058 (should (eq (python-info-encoding) 'latin-1))))
4059
4060 (ert-deftest python-info-encoding-2 ()
4061 "Should default to utf-8."
4062 (python-tests-with-temp-buffer
4063 "# No encoding for you
4064
4065 foo = True # another comment"
4066 (should (eq (python-info-encoding) 'utf-8))))
4067
4068 \f
4069 ;;; Utility functions
4070
4071 (ert-deftest python-util-goto-line-1 ()
4072 (python-tests-with-temp-buffer
4073 (concat
4074 "# a comment
4075 # another comment
4076 def foo(a, b, c):
4077 pass" (make-string 20 ?\n))
4078 (python-util-goto-line 10)
4079 (should (= (line-number-at-pos) 10))
4080 (python-util-goto-line 20)
4081 (should (= (line-number-at-pos) 20))))
4082
4083 (ert-deftest python-util-clone-local-variables-1 ()
4084 (let ((buffer (generate-new-buffer
4085 "python-util-clone-local-variables-1"))
4086 (varcons
4087 '((python-fill-docstring-style . django)
4088 (python-shell-interpreter . "python")
4089 (python-shell-interpreter-args . "manage.py shell")
4090 (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
4091 (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
4092 (python-shell-extra-pythonpaths "/home/user/pylib/")
4093 (python-shell-completion-setup-code
4094 . "from IPython.core.completerlib import module_completion")
4095 (python-shell-completion-string-code
4096 . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
4097 (python-shell-virtualenv-path
4098 . "/home/user/.virtualenvs/project"))))
4099 (with-current-buffer buffer
4100 (kill-all-local-variables)
4101 (dolist (ccons varcons)
4102 (set (make-local-variable (car ccons)) (cdr ccons))))
4103 (python-tests-with-temp-buffer
4104 ""
4105 (python-util-clone-local-variables buffer)
4106 (dolist (ccons varcons)
4107 (should
4108 (equal (symbol-value (car ccons)) (cdr ccons)))))
4109 (kill-buffer buffer)))
4110
4111 (ert-deftest python-util-strip-string-1 ()
4112 (should (string= (python-util-strip-string "\t\r\n str") "str"))
4113 (should (string= (python-util-strip-string "str \n\r") "str"))
4114 (should (string= (python-util-strip-string "\t\r\n str \n\r ") "str"))
4115 (should
4116 (string= (python-util-strip-string "\n str \nin \tg \n\r") "str \nin \tg"))
4117 (should (string= (python-util-strip-string "\n \t \n\r ") ""))
4118 (should (string= (python-util-strip-string "") "")))
4119
4120 (ert-deftest python-util-forward-comment-1 ()
4121 (python-tests-with-temp-buffer
4122 (concat
4123 "# a comment
4124 # another comment
4125 # bad indented comment
4126 # more comments" (make-string 9999 ?\n))
4127 (python-util-forward-comment 1)
4128 (should (= (point) (point-max)))
4129 (python-util-forward-comment -1)
4130 (should (= (point) (point-min)))))
4131
4132 (ert-deftest python-util-valid-regexp-p-1 ()
4133 (should (python-util-valid-regexp-p ""))
4134 (should (python-util-valid-regexp-p python-shell-prompt-regexp))
4135 (should (not (python-util-valid-regexp-p "\\("))))
4136
4137 \f
4138 ;;; Electricity
4139
4140 (ert-deftest python-parens-electric-indent-1 ()
4141 (require 'electric)
4142 (let ((eim electric-indent-mode))
4143 (unwind-protect
4144 (progn
4145 (python-tests-with-temp-buffer
4146 "
4147 from django.conf.urls import patterns, include, url
4148
4149 from django.contrib import admin
4150
4151 from myapp import views
4152
4153
4154 urlpatterns = patterns('',
4155 url(r'^$', views.index
4156 )
4157 "
4158 (electric-indent-mode 1)
4159 (python-tests-look-at "views.index")
4160 (end-of-line)
4161
4162 ;; Inserting commas within the same line should leave
4163 ;; indentation unchanged.
4164 (python-tests-self-insert ",")
4165 (should (= (current-indentation) 4))
4166
4167 ;; As well as any other input happening within the same
4168 ;; set of parens.
4169 (python-tests-self-insert " name='index')")
4170 (should (= (current-indentation) 4))
4171
4172 ;; But a comma outside it, should trigger indentation.
4173 (python-tests-self-insert ",")
4174 (should (= (current-indentation) 23))
4175
4176 ;; Newline indents to the first argument column
4177 (python-tests-self-insert "\n")
4178 (should (= (current-indentation) 23))
4179
4180 ;; All this input must not change indentation
4181 (indent-line-to 4)
4182 (python-tests-self-insert "url(r'^/login$', views.login)")
4183 (should (= (current-indentation) 4))
4184
4185 ;; But this comma does
4186 (python-tests-self-insert ",")
4187 (should (= (current-indentation) 23))))
4188 (or eim (electric-indent-mode -1)))))
4189
4190 (ert-deftest python-triple-quote-pairing ()
4191 (require 'electric)
4192 (let ((epm electric-pair-mode))
4193 (unwind-protect
4194 (progn
4195 (python-tests-with-temp-buffer
4196 "\"\"\n"
4197 (or epm (electric-pair-mode 1))
4198 (goto-char (1- (point-max)))
4199 (python-tests-self-insert ?\")
4200 (should (string= (buffer-string)
4201 "\"\"\"\"\"\"\n"))
4202 (should (= (point) 4)))
4203 (python-tests-with-temp-buffer
4204 "\n"
4205 (python-tests-self-insert (list ?\" ?\" ?\"))
4206 (should (string= (buffer-string)
4207 "\"\"\"\"\"\"\n"))
4208 (should (= (point) 4)))
4209 (python-tests-with-temp-buffer
4210 "\"\n\"\"\n"
4211 (goto-char (1- (point-max)))
4212 (python-tests-self-insert ?\")
4213 (should (= (point) (1- (point-max))))
4214 (should (string= (buffer-string)
4215 "\"\n\"\"\"\n"))))
4216 (or epm (electric-pair-mode -1)))))
4217
4218
4219 (provide 'python-tests)
4220
4221 ;; Local Variables:
4222 ;; coding: utf-8
4223 ;; indent-tabs-mode: nil
4224 ;; End:
4225
4226 ;;; python-tests.el ends here