1 ;;; python-tests.el --- Test suite for python.el
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 (defmacro python-tests-with-temp-buffer
(contents &rest body
)
27 "Create a `python-mode' enabled temp buffer with CONTENTS.
28 BODY is code to be executed within the temp buffer. Point is
29 always located at the beginning of buffer."
30 (declare (indent 1) (debug t
))
34 (goto-char (point-min))
37 (defmacro python-tests-with-temp-file
(contents &rest body
)
38 "Create a `python-mode' enabled file with CONTENTS.
39 BODY is code to be executed within the temp buffer. Point is
40 always located at the beginning of buffer."
41 (declare (indent 1) (debug t
))
42 `(let* ((temp-file (concat (make-temp-file "python-tests") ".py"))
43 (buffer (find-file-noselect temp-file
)))
45 (with-current-buffer buffer
48 (goto-char (point-min))
50 (and buffer
(kill-buffer buffer
)))))
52 (defun python-tests-look-at (string &optional num restore-point
)
53 "Move point at beginning of STRING in the current buffer.
54 Optional argument NUM defaults to 1 and is an integer indicating
55 how many occurrences must be found, when positive the search is
56 done forwards, otherwise backwards. When RESTORE-POINT is
57 non-nil the point is not moved but the position found is still
58 returned. When searching forward and point is already looking at
59 STRING, it is skipped so the next STRING occurrence is selected."
60 (let* ((num (or num
1))
61 (starting-point (point))
62 (string (regexp-quote string
))
63 (search-fn (if (> num
0) #'re-search-forward
#'re-search-backward
))
64 (deinc-fn (if (> num
0) #'1-
#'1+))
68 (while (not (= num
0))
71 ;; Moving forward and already looking at STRING, skip it.
72 (forward-char (length (match-string-no-properties 0))))
73 (and (not (funcall search-fn string nil t
))
76 ;; `re-search-forward' leaves point at the end of the
77 ;; occurrence, move back so point is at the beginning
79 (forward-char (- (length (match-string-no-properties 0)))))
81 num
(funcall deinc-fn num
)
82 found-point
(point))))
84 (and restore-point
(goto-char starting-point
)))))
87 ;;; Tests for your tests, so you can test while you test.
89 (ert-deftest python-tests-look-at-1
()
90 "Test forward movement."
91 (python-tests-with-temp-buffer
92 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
93 sed do eiusmod tempor incididunt ut labore et dolore magna
95 (let ((expected (save-excursion
97 (re-search-forward "et" nil t
))
100 (should (= (python-tests-look-at "et" 3 t
) expected
))
101 ;; Even if NUM is bigger than found occurrences the point of last
102 ;; one should be returned.
103 (should (= (python-tests-look-at "et" 6 t
) expected
))
104 ;; If already looking at STRING, it should skip it.
105 (dotimes (i 2) (re-search-forward "et"))
107 (should (= (python-tests-look-at "et") expected
)))))
109 (ert-deftest python-tests-look-at-2
()
110 "Test backward movement."
111 (python-tests-with-temp-buffer
112 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
113 sed do eiusmod tempor incididunt ut labore et dolore magna
117 (re-search-forward "et" nil t
)
121 (re-search-forward "et" nil t
))
122 (should (= (python-tests-look-at "et" -
3 t
) expected
))
123 (should (= (python-tests-look-at "et" -
6 t
) expected
)))))
129 ;;; Python specialized rx
132 ;;; Font-lock and syntax
137 ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
139 (ert-deftest python-indent-pep8-1
()
141 (python-tests-with-temp-buffer
142 "# Aligned with opening delimiter
143 foo = long_function_name(var_one, var_two,
146 (should (eq (car (python-indent-context)) 'no-indent
))
147 (should (= (python-indent-calculate-indentation) 0))
148 (python-tests-look-at "foo = long_function_name(var_one, var_two,")
149 (should (eq (car (python-indent-context)) 'after-line
))
150 (should (= (python-indent-calculate-indentation) 0))
151 (python-tests-look-at "var_three, var_four)")
152 (should (eq (car (python-indent-context)) 'inside-paren
))
153 (should (= (python-indent-calculate-indentation) 25))))
155 (ert-deftest python-indent-pep8-2
()
157 (python-tests-with-temp-buffer
158 "# More indentation included to distinguish this from the rest.
159 def long_function_name(
160 var_one, var_two, var_three,
164 (should (eq (car (python-indent-context)) 'no-indent
))
165 (should (= (python-indent-calculate-indentation) 0))
166 (python-tests-look-at "def long_function_name(")
167 (should (eq (car (python-indent-context)) 'after-line
))
168 (should (= (python-indent-calculate-indentation) 0))
169 (python-tests-look-at "var_one, var_two, var_three,")
170 (should (eq (car (python-indent-context)) 'inside-paren
))
171 (should (= (python-indent-calculate-indentation) 8))
172 (python-tests-look-at "var_four):")
173 (should (eq (car (python-indent-context)) 'inside-paren
))
174 (should (= (python-indent-calculate-indentation) 8))
175 (python-tests-look-at "print (var_one)")
176 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
177 (should (= (python-indent-calculate-indentation) 4))))
179 (ert-deftest python-indent-pep8-3
()
181 (python-tests-with-temp-buffer
182 "# Extra indentation is not necessary.
183 foo = long_function_name(
187 (should (eq (car (python-indent-context)) 'no-indent
))
188 (should (= (python-indent-calculate-indentation) 0))
189 (python-tests-look-at "foo = long_function_name(")
190 (should (eq (car (python-indent-context)) 'after-line
))
191 (should (= (python-indent-calculate-indentation) 0))
192 (python-tests-look-at "var_one, var_two,")
193 (should (eq (car (python-indent-context)) 'inside-paren
))
194 (should (= (python-indent-calculate-indentation) 4))
195 (python-tests-look-at "var_three, var_four)")
196 (should (eq (car (python-indent-context)) 'inside-paren
))
197 (should (= (python-indent-calculate-indentation) 4))))
199 (ert-deftest python-indent-inside-paren-1
()
200 "The most simple inside-paren case that shouldn't fail."
201 (python-tests-with-temp-buffer
219 (python-tests-look-at "data = {")
220 (should (eq (car (python-indent-context)) 'after-line
))
221 (should (= (python-indent-calculate-indentation) 0))
222 (python-tests-look-at "'key':")
223 (should (eq (car (python-indent-context)) 'inside-paren
))
224 (should (= (python-indent-calculate-indentation) 4))
225 (python-tests-look-at "{")
226 (should (eq (car (python-indent-context)) 'inside-paren
))
227 (should (= (python-indent-calculate-indentation) 4))
228 (python-tests-look-at "'objlist': [")
229 (should (eq (car (python-indent-context)) 'inside-paren
))
230 (should (= (python-indent-calculate-indentation) 8))
231 (python-tests-look-at "{")
232 (should (eq (car (python-indent-context)) 'inside-paren
))
233 (should (= (python-indent-calculate-indentation) 12))
234 (python-tests-look-at "'pk': 1,")
235 (should (eq (car (python-indent-context)) 'inside-paren
))
236 (should (= (python-indent-calculate-indentation) 16))
237 (python-tests-look-at "'name': 'first',")
238 (should (eq (car (python-indent-context)) 'inside-paren
))
239 (should (= (python-indent-calculate-indentation) 16))
240 (python-tests-look-at "},")
241 (should (eq (car (python-indent-context)) 'inside-paren
))
242 (should (= (python-indent-calculate-indentation) 12))
243 (python-tests-look-at "{")
244 (should (eq (car (python-indent-context)) 'inside-paren
))
245 (should (= (python-indent-calculate-indentation) 12))
246 (python-tests-look-at "'pk': 2,")
247 (should (eq (car (python-indent-context)) 'inside-paren
))
248 (should (= (python-indent-calculate-indentation) 16))
249 (python-tests-look-at "'name': 'second',")
250 (should (eq (car (python-indent-context)) 'inside-paren
))
251 (should (= (python-indent-calculate-indentation) 16))
252 (python-tests-look-at "}")
253 (should (eq (car (python-indent-context)) 'inside-paren
))
254 (should (= (python-indent-calculate-indentation) 12))
255 (python-tests-look-at "]")
256 (should (eq (car (python-indent-context)) 'inside-paren
))
257 (should (= (python-indent-calculate-indentation) 8))
258 (python-tests-look-at "}")
259 (should (eq (car (python-indent-context)) 'inside-paren
))
260 (should (= (python-indent-calculate-indentation) 4))
261 (python-tests-look-at "}")
262 (should (eq (car (python-indent-context)) 'inside-paren
))
263 (should (= (python-indent-calculate-indentation) 0))))
265 (ert-deftest python-indent-inside-paren-2
()
266 "Another more compact paren group style."
267 (python-tests-with-temp-buffer
278 (python-tests-look-at "data = {")
279 (should (eq (car (python-indent-context)) 'after-line
))
280 (should (= (python-indent-calculate-indentation) 0))
281 (python-tests-look-at "'objlist': [")
282 (should (eq (car (python-indent-context)) 'inside-paren
))
283 (should (= (python-indent-calculate-indentation) 4))
284 (python-tests-look-at "{'pk': 1,")
285 (should (eq (car (python-indent-context)) 'inside-paren
))
286 (should (= (python-indent-calculate-indentation) 8))
287 (python-tests-look-at "'name': 'first'},")
288 (should (eq (car (python-indent-context)) 'inside-paren
))
289 (should (= (python-indent-calculate-indentation) 9))
290 (python-tests-look-at "{'pk': 2,")
291 (should (eq (car (python-indent-context)) 'inside-paren
))
292 (should (= (python-indent-calculate-indentation) 8))
293 (python-tests-look-at "'name': 'second'}")
294 (should (eq (car (python-indent-context)) 'inside-paren
))
295 (should (= (python-indent-calculate-indentation) 9))
296 (python-tests-look-at "]")
297 (should (eq (car (python-indent-context)) 'inside-paren
))
298 (should (= (python-indent-calculate-indentation) 4))
299 (python-tests-look-at "}}")
300 (should (eq (car (python-indent-context)) 'inside-paren
))
301 (should (= (python-indent-calculate-indentation) 0))
302 (python-tests-look-at "}")
303 (should (eq (car (python-indent-context)) 'inside-paren
))
304 (should (= (python-indent-calculate-indentation) 0))))
306 (ert-deftest python-indent-after-block-1
()
307 "The most simple after-block case that shouldn't fail."
308 (python-tests-with-temp-buffer
310 def foo(a, b, c=True):
312 (should (eq (car (python-indent-context)) 'no-indent
))
313 (should (= (python-indent-calculate-indentation) 0))
314 (goto-char (point-max))
315 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
316 (should (= (python-indent-calculate-indentation) 4))))
318 (ert-deftest python-indent-after-block-2
()
319 "A weird (malformed) multiline block statement."
320 (python-tests-with-temp-buffer
326 (goto-char (point-max))
327 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
328 (should (= (python-indent-calculate-indentation) 4))))
330 (ert-deftest python-indent-dedenters-1
()
331 "Check all dedenters."
332 (python-tests-with-temp-buffer
342 except (IndexError, AttributeError):
345 print ('nor a, nor b are true')
347 (python-tests-look-at "if a:")
348 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
349 (should (= (python-indent-calculate-indentation) 4))
350 (python-tests-look-at "print (a)")
351 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
352 (should (= (python-indent-calculate-indentation) 8))
353 (python-tests-look-at "elif b:")
354 (should (eq (car (python-indent-context)) 'after-line
))
355 (should (= (python-indent-calculate-indentation) 4))
356 (python-tests-look-at "print (b)")
357 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
358 (should (= (python-indent-calculate-indentation) 8))
359 (python-tests-look-at "else:")
360 (should (eq (car (python-indent-context)) 'after-line
))
361 (should (= (python-indent-calculate-indentation) 4))
362 (python-tests-look-at "try:")
363 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
364 (should (= (python-indent-calculate-indentation) 8))
365 (python-tests-look-at "print (c.pop())")
366 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
367 (should (= (python-indent-calculate-indentation) 12))
368 (python-tests-look-at "except (IndexError, AttributeError):")
369 (should (eq (car (python-indent-context)) 'after-line
))
370 (should (= (python-indent-calculate-indentation) 8))
371 (python-tests-look-at "print (c)")
372 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
373 (should (= (python-indent-calculate-indentation) 12))
374 (python-tests-look-at "finally:")
375 (should (eq (car (python-indent-context)) 'after-line
))
376 (should (= (python-indent-calculate-indentation) 8))
377 (python-tests-look-at "print ('nor a, nor b are true')")
378 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
379 (should (= (python-indent-calculate-indentation) 12))))
381 (ert-deftest python-indent-after-backslash-1
()
382 "The most common case."
383 (python-tests-with-temp-buffer
385 from foo.bar.baz import something, something_1 \\\\
386 something_2 something_3, \\\\
387 something_4, something_5
389 (python-tests-look-at "from foo.bar.baz import something, something_1")
390 (should (eq (car (python-indent-context)) 'after-line
))
391 (should (= (python-indent-calculate-indentation) 0))
392 (python-tests-look-at "something_2 something_3,")
393 (should (eq (car (python-indent-context)) 'after-backslash
))
394 (should (= (python-indent-calculate-indentation) 4))
395 (python-tests-look-at "something_4, something_5")
396 (should (eq (car (python-indent-context)) 'after-backslash
))
397 (should (= (python-indent-calculate-indentation) 4))
398 (goto-char (point-max))
399 (should (eq (car (python-indent-context)) 'after-line
))
400 (should (= (python-indent-calculate-indentation) 0))))
402 (ert-deftest python-indent-after-backslash-2
()
403 "A pretty extreme complicated case."
404 (python-tests-with-temp-buffer
406 objects = Thing.objects.all() \\\\
416 (python-tests-look-at "objects = Thing.objects.all()")
417 (should (eq (car (python-indent-context)) 'after-line
))
418 (should (= (python-indent-calculate-indentation) 0))
419 (python-tests-look-at ".filter(")
420 (should (eq (car (python-indent-context)) 'after-backslash
))
421 (should (= (python-indent-calculate-indentation) 23))
422 (python-tests-look-at "type='toy',")
423 (should (eq (car (python-indent-context)) 'inside-paren
))
424 (should (= (python-indent-calculate-indentation) 27))
425 (python-tests-look-at "status='bought'")
426 (should (eq (car (python-indent-context)) 'inside-paren
))
427 (should (= (python-indent-calculate-indentation) 27))
428 (python-tests-look-at ") \\\\")
429 (should (eq (car (python-indent-context)) 'inside-paren
))
430 (should (= (python-indent-calculate-indentation) 23))
431 (python-tests-look-at ".aggregate(")
432 (should (eq (car (python-indent-context)) 'after-backslash
))
433 (should (= (python-indent-calculate-indentation) 23))
434 (python-tests-look-at "Sum('amount')")
435 (should (eq (car (python-indent-context)) 'inside-paren
))
436 (should (= (python-indent-calculate-indentation) 27))
437 (python-tests-look-at ") \\\\")
438 (should (eq (car (python-indent-context)) 'inside-paren
))
439 (should (= (python-indent-calculate-indentation) 23))
440 (python-tests-look-at ".values_list()")
441 (should (eq (car (python-indent-context)) 'after-backslash
))
442 (should (= (python-indent-calculate-indentation) 23))
444 (should (eq (car (python-indent-context)) 'after-line
))
445 (should (= (python-indent-calculate-indentation) 0))))
447 (ert-deftest python-indent-block-enders
()
448 "Test `python-indent-block-enders' value honoring."
449 (python-tests-with-temp-buffer
462 (python-tests-look-at "3)")
464 (= (python-indent-calculate-indentation) 12)
465 (python-tests-look-at "pass")
467 (= (python-indent-calculate-indentation) 8)))
472 (ert-deftest python-nav-beginning-of-defun-1
()
473 (python-tests-with-temp-buffer
475 def decoratorFunctionWithArguments(arg1, arg2, arg3):
476 '''print decorated function call data to stdout.
480 @decoratorFunctionWithArguments('arg1', 'arg2')
481 def func(a, b, c=True):
486 print 'Inside wwrap()'
487 def wrapped_f(*args):
488 print 'Inside wrapped_f()'
489 print 'Decorator arguments:', arg1, arg2, arg3
491 print 'After f(*args)'
495 (python-tests-look-at "return wrap")
496 (should (= (save-excursion
497 (python-nav-beginning-of-defun)
500 (python-tests-look-at "def wrapped_f(*args):" -
1)
503 (python-tests-look-at "def wrapped_f(*args):" -
1)
504 (should (= (save-excursion
505 (python-nav-beginning-of-defun)
508 (python-tests-look-at "def wwrap(f):" -
1)
511 (python-tests-look-at "def wwrap(f):" -
1)
512 (should (= (save-excursion
513 (python-nav-beginning-of-defun)
516 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)
520 (ert-deftest python-nav-beginning-of-defun-2
()
521 (python-tests-with-temp-buffer
537 ;; Nested defuns, are handled with care.
538 (python-tests-look-at "def c(self):")
539 (should (= (save-excursion
540 (python-nav-beginning-of-defun)
543 (python-tests-look-at "def m(self):" -
1)
546 ;; Defuns on same levels should be respected.
547 (python-tests-look-at "def a():" -
1)
548 (should (= (save-excursion
549 (python-nav-beginning-of-defun)
552 (python-tests-look-at "def b():" -
1)
555 ;; Jump to a top level defun.
556 (python-tests-look-at "def b():" -
1)
557 (should (= (save-excursion
558 (python-nav-beginning-of-defun)
561 (python-tests-look-at "def m(self):" -
1)
564 ;; Jump to a top level defun again.
565 (python-tests-look-at "def m(self):" -
1)
566 (should (= (save-excursion
567 (python-nav-beginning-of-defun)
570 (python-tests-look-at "class C(object):" -
1)
574 (ert-deftest python-nav-end-of-defun-1
()
575 (python-tests-with-temp-buffer
591 (should (= (save-excursion
592 (python-tests-look-at "class C(object):")
593 (python-nav-end-of-defun)
597 (should (= (save-excursion
598 (python-tests-look-at "def m(self):")
599 (python-nav-end-of-defun)
602 (python-tests-look-at "def c(self):")
605 (should (= (save-excursion
606 (python-tests-look-at "def b():")
607 (python-nav-end-of-defun)
610 (python-tests-look-at "def b():")
613 (should (= (save-excursion
614 (python-tests-look-at "def c(self):")
615 (python-nav-end-of-defun)
620 (ert-deftest python-nav-end-of-defun-2
()
621 (python-tests-with-temp-buffer
623 def decoratorFunctionWithArguments(arg1, arg2, arg3):
624 '''print decorated function call data to stdout.
628 @decoratorFunctionWithArguments('arg1', 'arg2')
629 def func(a, b, c=True):
634 print 'Inside wwrap()'
635 def wrapped_f(*args):
636 print 'Inside wrapped_f()'
637 print 'Decorator arguments:', arg1, arg2, arg3
639 print 'After f(*args)'
643 (should (= (save-excursion
644 (python-tests-look-at "def decoratorFunctionWithArguments")
645 (python-nav-end-of-defun)
649 (should (= (save-excursion
650 (python-tests-look-at "@decoratorFunctionWithArguments")
651 (python-nav-end-of-defun)
655 (should (= (save-excursion
656 (python-tests-look-at "def wwrap(f):")
657 (python-nav-end-of-defun)
660 (python-tests-look-at "return wwrap")
661 (line-beginning-position))))
662 (should (= (save-excursion
663 (python-tests-look-at "def wrapped_f(*args):")
664 (python-nav-end-of-defun)
667 (python-tests-look-at "return wrapped_f")
668 (line-beginning-position))))
669 (should (= (save-excursion
670 (python-tests-look-at "f(*args)")
671 (python-nav-end-of-defun)
674 (python-tests-look-at "return wrapped_f")
675 (line-beginning-position))))))
677 (ert-deftest python-nav-backward-defun-1
()
678 (python-tests-with-temp-buffer
704 (goto-char (point-max))
705 (should (= (save-excursion (python-nav-backward-defun))
706 (python-tests-look-at " def c(self): # c" -
1)))
707 (should (= (save-excursion (python-nav-backward-defun))
708 (python-tests-look-at " def d(self): # d" -
1)))
709 (should (= (save-excursion (python-nav-backward-defun))
710 (python-tests-look-at " class C(object): # C" -
1)))
711 (should (= (save-excursion (python-nav-backward-defun))
712 (python-tests-look-at " class B(object): # B" -
1)))
713 (should (= (save-excursion (python-nav-backward-defun))
714 (python-tests-look-at " def b(self): # b" -
1)))
715 (should (= (save-excursion (python-nav-backward-defun))
716 (python-tests-look-at " def a(self): # a" -
1)))
717 (should (= (save-excursion (python-nav-backward-defun))
718 (python-tests-look-at "class A(object): # A" -
1)))
719 (should (not (python-nav-backward-defun)))))
721 (ert-deftest python-nav-forward-defun-1
()
722 (python-tests-with-temp-buffer
748 (goto-char (point-min))
749 (should (= (save-excursion (python-nav-forward-defun))
750 (python-tests-look-at "(object): # A")))
751 (should (= (save-excursion (python-nav-forward-defun))
752 (python-tests-look-at "(self): # a")))
753 (should (= (save-excursion (python-nav-forward-defun))
754 (python-tests-look-at "(self): # b")))
755 (should (= (save-excursion (python-nav-forward-defun))
756 (python-tests-look-at "(object): # B")))
757 (should (= (save-excursion (python-nav-forward-defun))
758 (python-tests-look-at "(object): # C")))
759 (should (= (save-excursion (python-nav-forward-defun))
760 (python-tests-look-at "(self): # d")))
761 (should (= (save-excursion (python-nav-forward-defun))
762 (python-tests-look-at "(self): # c")))
763 (should (not (python-nav-forward-defun)))))
765 (ert-deftest python-nav-beginning-of-statement-1
()
766 (python-tests-with-temp-buffer
776 v3 = ('this is a string'
781 # this is a comment, yo
782 'continue previous line')
788 (python-tests-look-at "v2 =")
789 (python-util-forward-comment -
1)
790 (should (= (save-excursion
791 (python-nav-beginning-of-statement)
793 (python-tests-look-at "v1 =" -
1 t
)))
794 (python-tests-look-at "v3 =")
795 (python-util-forward-comment -
1)
796 (should (= (save-excursion
797 (python-nav-beginning-of-statement)
799 (python-tests-look-at "v2 =" -
1 t
)))
800 (python-tests-look-at "v4 =")
801 (python-util-forward-comment -
1)
802 (should (= (save-excursion
803 (python-nav-beginning-of-statement)
805 (python-tests-look-at "v3 =" -
1 t
)))
806 (goto-char (point-max))
807 (python-util-forward-comment -
1)
808 (should (= (save-excursion
809 (python-nav-beginning-of-statement)
811 (python-tests-look-at "v4 =" -
1 t
)))))
813 (ert-deftest python-nav-end-of-statement-1
()
814 (python-tests-with-temp-buffer
824 v3 = ('this is a string'
829 # this is a comment, yo
830 'continue previous line')
836 (python-tests-look-at "v1 =")
837 (should (= (save-excursion
838 (python-nav-end-of-statement)
841 (python-tests-look-at "789")
842 (line-end-position))))
843 (python-tests-look-at "v2 =")
844 (should (= (save-excursion
845 (python-nav-end-of-statement)
848 (python-tests-look-at "value4)")
849 (line-end-position))))
850 (python-tests-look-at "v3 =")
851 (should (= (save-excursion
852 (python-nav-end-of-statement)
855 (python-tests-look-at
856 "'continue previous line')")
857 (line-end-position))))
858 (python-tests-look-at "v4 =")
859 (should (= (save-excursion
860 (python-nav-end-of-statement)
863 (goto-char (point-max))
864 (python-util-forward-comment -
1)
867 (ert-deftest python-nav-forward-statement-1
()
868 (python-tests-with-temp-buffer
878 v3 = ('this is a string'
883 # this is a comment, yo
884 'continue previous line')
890 (python-tests-look-at "v1 =")
891 (should (= (save-excursion
892 (python-nav-forward-statement)
894 (python-tests-look-at "v2 =")))
895 (should (= (save-excursion
896 (python-nav-forward-statement)
898 (python-tests-look-at "v3 =")))
899 (should (= (save-excursion
900 (python-nav-forward-statement)
902 (python-tests-look-at "v4 =")))
903 (should (= (save-excursion
904 (python-nav-forward-statement)
908 (ert-deftest python-nav-backward-statement-1
()
909 (python-tests-with-temp-buffer
919 v3 = ('this is a string'
924 # this is a comment, yo
925 'continue previous line')
931 (goto-char (point-max))
932 (should (= (save-excursion
933 (python-nav-backward-statement)
935 (python-tests-look-at "v4 =" -
1)))
936 (should (= (save-excursion
937 (python-nav-backward-statement)
939 (python-tests-look-at "v3 =" -
1)))
940 (should (= (save-excursion
941 (python-nav-backward-statement)
943 (python-tests-look-at "v2 =" -
1)))
944 (should (= (save-excursion
945 (python-nav-backward-statement)
947 (python-tests-look-at "v1 =" -
1)))))
949 (ert-deftest python-nav-backward-statement-2
()
950 :expected-result
:failed
951 (python-tests-with-temp-buffer
962 ;; FIXME: For some reason `python-nav-backward-statement' is moving
963 ;; back two sentences when starting from 'value4)'.
964 (goto-char (point-max))
965 (python-util-forward-comment -
1)
966 (should (= (save-excursion
967 (python-nav-backward-statement)
969 (python-tests-look-at "v2 =" -
1 t
)))))
971 (ert-deftest python-nav-beginning-of-block-1
()
972 (python-tests-with-temp-buffer
974 def decoratorFunctionWithArguments(arg1, arg2, arg3):
975 '''print decorated function call data to stdout.
979 @decoratorFunctionWithArguments('arg1', 'arg2')
980 def func(a, b, c=True):
985 print 'Inside wwrap()'
986 def wrapped_f(*args):
987 print 'Inside wrapped_f()'
988 print 'Decorator arguments:', arg1, arg2, arg3
990 print 'After f(*args)'
994 (python-tests-look-at "return wwrap")
995 (should (= (save-excursion
996 (python-nav-beginning-of-block)
998 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)))
999 (python-tests-look-at "print 'Inside wwrap()'")
1000 (should (= (save-excursion
1001 (python-nav-beginning-of-block)
1003 (python-tests-look-at "def wwrap(f):" -
1)))
1004 (python-tests-look-at "print 'After f(*args)'")
1006 (should (= (save-excursion
1007 (python-nav-beginning-of-block)
1009 (python-tests-look-at "def wrapped_f(*args):" -
1)))
1010 (python-tests-look-at "return wrapped_f")
1011 (should (= (save-excursion
1012 (python-nav-beginning-of-block)
1014 (python-tests-look-at "def wwrap(f):" -
1)))))
1016 (ert-deftest python-nav-end-of-block-1
()
1017 (python-tests-with-temp-buffer
1019 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1020 '''print decorated function call data to stdout.
1024 @decoratorFunctionWithArguments('arg1', 'arg2')
1025 def func(a, b, c=True):
1030 print 'Inside wwrap()'
1031 def wrapped_f(*args):
1032 print 'Inside wrapped_f()'
1033 print 'Decorator arguments:', arg1, arg2, arg3
1035 print 'After f(*args)'
1039 (python-tests-look-at "def decoratorFunctionWithArguments")
1040 (should (= (save-excursion
1041 (python-nav-end-of-block)
1044 (goto-char (point-max))
1045 (python-util-forward-comment -
1)
1047 (python-tests-look-at "def wwrap(f):")
1048 (should (= (save-excursion
1049 (python-nav-end-of-block)
1052 (python-tests-look-at "return wrapped_f")
1053 (line-end-position))))
1055 (should (= (save-excursion
1056 (python-nav-end-of-block)
1059 (python-tests-look-at "return wrapped_f")
1060 (line-end-position))))
1061 (python-tests-look-at "f(*args)")
1062 (should (= (save-excursion
1063 (python-nav-end-of-block)
1066 (python-tests-look-at "print 'After f(*args)'")
1067 (line-end-position))))))
1069 (ert-deftest python-nav-forward-block-1
()
1070 "This also accounts as a test for `python-nav-backward-block'."
1071 (python-tests-with-temp-buffer
1073 if request.user.is_authenticated():
1077 profile = request.user.get_profile()
1078 except Profile.DoesNotExist:
1079 profile = Profile.objects.create(user=request.user)
1082 profile.recalculate_stats()
1084 profile.clear_stats()
1089 (should (= (save-excursion (python-nav-forward-block))
1090 (python-tests-look-at "if request.user.is_authenticated():")))
1091 (should (= (save-excursion (python-nav-forward-block))
1092 (python-tests-look-at "try:")))
1093 (should (= (save-excursion (python-nav-forward-block))
1094 (python-tests-look-at "except Profile.DoesNotExist:")))
1095 (should (= (save-excursion (python-nav-forward-block))
1096 (python-tests-look-at "else:")))
1097 (should (= (save-excursion (python-nav-forward-block))
1098 (python-tests-look-at "if profile.stats:")))
1099 (should (= (save-excursion (python-nav-forward-block))
1100 (python-tests-look-at "else:")))
1101 (should (= (save-excursion (python-nav-forward-block))
1102 (python-tests-look-at "finally:")))
1103 ;; When point is at the last block, leave it there and return nil
1104 (should (not (save-excursion (python-nav-forward-block))))
1105 ;; Move backwards, and even if the number of moves is less than the
1106 ;; provided argument return the point.
1107 (should (= (save-excursion (python-nav-forward-block -
10))
1108 (python-tests-look-at
1109 "if request.user.is_authenticated():" -
1)))))
1111 (ert-deftest python-nav-lisp-forward-sexp-safe-1
()
1112 (python-tests-with-temp-buffer
1114 profile = Profile.objects.create(user=request.user)
1117 (python-tests-look-at "profile =")
1118 (python-nav-lisp-forward-sexp-safe 4)
1119 (should (looking-at "(user=request.user)"))
1120 (python-tests-look-at "user=request.user")
1121 (python-nav-lisp-forward-sexp-safe -
1)
1122 (should (looking-at "(user=request.user)"))
1123 (python-nav-lisp-forward-sexp-safe -
4)
1124 (should (looking-at "profile ="))
1125 (python-tests-look-at "user=request.user")
1126 (python-nav-lisp-forward-sexp-safe 3)
1127 (should (looking-at ")"))
1128 (python-nav-lisp-forward-sexp-safe 1)
1129 (should (looking-at "$"))
1130 (python-nav-lisp-forward-sexp-safe 1)
1131 (should (looking-at ".notify()"))))
1133 (ert-deftest python-nav-forward-sexp-1
()
1134 (python-tests-with-temp-buffer
1140 (python-tests-look-at "a()")
1141 (python-nav-forward-sexp)
1142 (should (looking-at "$"))
1143 (should (save-excursion
1145 (looking-at "a()")))
1146 (python-nav-forward-sexp)
1147 (should (looking-at "$"))
1148 (should (save-excursion
1150 (looking-at "b()")))
1151 (python-nav-forward-sexp)
1152 (should (looking-at "$"))
1153 (should (save-excursion
1155 (looking-at "c()")))
1156 ;; Movement next to a paren should do what lisp does and
1157 ;; unfortunately It can't change, because otherwise
1158 ;; `blink-matching-open' breaks.
1159 (python-nav-forward-sexp -
1)
1160 (should (looking-at "()"))
1161 (should (save-excursion
1163 (looking-at "c()")))
1164 (python-nav-forward-sexp -
1)
1165 (should (looking-at "c()"))
1166 (python-nav-forward-sexp -
1)
1167 (should (looking-at "b()"))
1168 (python-nav-forward-sexp -
1)
1169 (should (looking-at "a()"))))
1171 (ert-deftest python-nav-forward-sexp-2
()
1172 (python-tests-with-temp-buffer
1181 (python-tests-look-at "aa =")
1182 (python-nav-forward-sexp)
1183 (should (looking-at " = bbb"))
1184 (python-nav-forward-sexp)
1185 (should (looking-at "$"))
1186 (should (save-excursion
1187 (back-to-indentation)
1188 (looking-at "aaa = bbb")))
1189 (python-nav-forward-sexp)
1190 (should (looking-at "$"))
1191 (should (save-excursion
1192 (back-to-indentation)
1193 (looking-at "ccc = ddd")))
1194 (python-nav-forward-sexp)
1195 (should (looking-at "$"))
1196 (should (save-excursion
1197 (back-to-indentation)
1198 (looking-at "eee = fff")))
1199 (python-nav-forward-sexp)
1200 (should (looking-at "$"))
1201 (should (save-excursion
1202 (back-to-indentation)
1203 (looking-at "return ggg")))
1204 (python-nav-forward-sexp -
1)
1205 (should (looking-at "def func():"))))
1207 (ert-deftest python-nav-forward-sexp-3
()
1208 (python-tests-with-temp-buffer
1210 from some_module import some_sub_module
1211 from another_module import another_sub_module
1213 def another_statement():
1216 (python-tests-look-at "some_module")
1217 (python-nav-forward-sexp)
1218 (should (looking-at " import"))
1219 (python-nav-forward-sexp)
1220 (should (looking-at " some_sub_module"))
1221 (python-nav-forward-sexp)
1222 (should (looking-at "$"))
1225 (back-to-indentation)
1227 "from some_module import some_sub_module")))
1228 (python-nav-forward-sexp)
1229 (should (looking-at "$"))
1232 (back-to-indentation)
1234 "from another_module import another_sub_module")))
1235 (python-nav-forward-sexp)
1236 (should (looking-at "$"))
1239 (back-to-indentation)
1242 (python-nav-forward-sexp -
1)
1243 (should (looking-at "def another_statement():"))
1244 (python-nav-forward-sexp -
1)
1245 (should (looking-at "from another_module import another_sub_module"))
1246 (python-nav-forward-sexp -
1)
1247 (should (looking-at "from some_module import some_sub_module"))))
1249 (ert-deftest python-nav-up-list-1
()
1250 (python-tests-with-temp-buffer
1254 return [i for i in range(3)]
1256 (python-tests-look-at "3)]")
1257 (python-nav-up-list)
1258 (should (looking-at "]"))
1259 (python-nav-up-list)
1260 (should (looking-at "$"))))
1262 (ert-deftest python-nav-backward-up-list-1
()
1263 :expected-result
:failed
1264 (python-tests-with-temp-buffer
1268 return [i for i in range(3)]
1270 (python-tests-look-at "3)]")
1271 (python-nav-backward-up-list)
1272 (should (looking-at "(3)\\]"))
1273 (python-nav-backward-up-list)
1275 "\\[i for i in range(3)\\]"))
1276 ;; FIXME: Need to move to beginning-of-statement.
1277 (python-nav-backward-up-list)
1279 "return \\[i for i in range(3)\\]"))
1280 (python-nav-backward-up-list)
1281 (should (looking-at "if True:"))
1282 (python-nav-backward-up-list)
1283 (should (looking-at "def f():"))))
1286 ;;; Shell integration
1288 (defvar python-tests-shell-interpreter
"python")
1290 (ert-deftest python-shell-get-process-name-1
()
1291 "Check process name calculation on different scenarios."
1292 (python-tests-with-temp-buffer
1294 (should (string= (python-shell-get-process-name nil
)
1295 python-shell-buffer-name
))
1296 ;; When the `current-buffer' doesn't have `buffer-file-name', even
1297 ;; if dedicated flag is non-nil should not include its name.
1298 (should (string= (python-shell-get-process-name t
)
1299 python-shell-buffer-name
)))
1300 (python-tests-with-temp-file
1302 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1303 ;; should be respected.
1304 (should (string= (python-shell-get-process-name nil
)
1305 python-shell-buffer-name
))
1307 (python-shell-get-process-name t
)
1308 (format "%s[%s]" python-shell-buffer-name buffer-file-name
)))))
1310 (ert-deftest python-shell-internal-get-process-name-1
()
1311 "Check the internal process name is config-unique."
1312 (let* ((python-shell-interpreter python-tests-shell-interpreter
)
1313 (python-shell-interpreter-args "")
1314 (python-shell-prompt-regexp ">>> ")
1315 (python-shell-prompt-block-regexp "[.][.][.] ")
1316 (python-shell-setup-codes "")
1317 (python-shell-process-environment "")
1318 (python-shell-extra-pythonpaths "")
1319 (python-shell-exec-path "")
1320 (python-shell-virtualenv-path "")
1321 (expected (python-tests-with-temp-buffer
1322 "" (python-shell-internal-get-process-name))))
1323 ;; Same configurations should match.
1326 (python-tests-with-temp-buffer
1327 "" (python-shell-internal-get-process-name))))
1328 (let ((python-shell-interpreter-args "-B"))
1329 ;; A minimal change should generate different names.
1333 (python-tests-with-temp-buffer
1334 "" (python-shell-internal-get-process-name))))))))
1336 (ert-deftest python-shell-parse-command-1
()
1337 "Check the command to execute is calculated correctly.
1338 Using `python-shell-interpreter' and
1339 `python-shell-interpreter-args'."
1340 :expected-result
(if (executable-find python-tests-shell-interpreter
)
1343 (let ((python-shell-interpreter (executable-find
1344 python-tests-shell-interpreter
))
1345 (python-shell-interpreter-args "-B"))
1348 python-shell-interpreter
1349 python-shell-interpreter-args
)
1350 (python-shell-parse-command)))))
1352 (ert-deftest python-shell-calculate-process-environment-1
()
1353 "Test `python-shell-process-environment' modification."
1354 (let* ((original-process-environment process-environment
)
1355 (python-shell-process-environment
1356 '("TESTVAR1=value1" "TESTVAR2=value2"))
1357 (process-environment
1358 (python-shell-calculate-process-environment)))
1359 (should (equal (getenv "TESTVAR1") "value1"))
1360 (should (equal (getenv "TESTVAR2") "value2"))))
1362 (ert-deftest python-shell-calculate-process-environment-2
()
1363 "Test `python-shell-extra-pythonpaths' modification."
1364 (let* ((original-process-environment process-environment
)
1365 (original-pythonpath (getenv "PYTHONPATH"))
1366 (paths '("path1" "path2"))
1367 (python-shell-extra-pythonpaths paths
)
1368 (process-environment
1369 (python-shell-calculate-process-environment)))
1370 (should (equal (getenv "PYTHONPATH")
1372 (mapconcat 'identity paths path-separator
)
1373 path-separator original-pythonpath
)))))
1375 (ert-deftest python-shell-calculate-process-environment-3
()
1376 "Test `python-shell-virtualenv-path' modification."
1377 (let* ((original-process-environment process-environment
)
1378 (original-path (or (getenv "PATH") ""))
1379 (python-shell-virtualenv-path
1380 (directory-file-name user-emacs-directory
))
1381 (process-environment
1382 (python-shell-calculate-process-environment)))
1383 (should (not (getenv "PYTHONHOME")))
1384 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-path
))
1385 (should (equal (getenv "PATH")
1386 (format "%s/bin%s%s"
1387 python-shell-virtualenv-path
1388 path-separator original-path
)))))
1390 (ert-deftest python-shell-calculate-exec-path-1
()
1391 "Test `python-shell-exec-path' modification."
1392 (let* ((original-exec-path exec-path
)
1393 (python-shell-exec-path '("path1" "path2"))
1394 (exec-path (python-shell-calculate-exec-path)))
1397 (append python-shell-exec-path
1398 original-exec-path
)))))
1400 (ert-deftest python-shell-calculate-exec-path-2
()
1401 "Test `python-shell-exec-path' modification."
1402 (let* ((original-exec-path exec-path
)
1403 (python-shell-virtualenv-path
1404 (directory-file-name user-emacs-directory
))
1405 (exec-path (python-shell-calculate-exec-path)))
1409 (format "%s/bin" python-shell-virtualenv-path
)
1410 original-exec-path
))))))
1412 (ert-deftest python-shell-make-comint-1
()
1413 "Check comint creation for global shell buffer."
1414 :expected-result
(if (executable-find python-tests-shell-interpreter
)
1417 (let* ((python-shell-interpreter
1418 (executable-find python-tests-shell-interpreter
))
1419 (proc-name (python-shell-get-process-name nil
))
1421 (python-tests-with-temp-buffer
1422 "" (python-shell-make-comint
1423 (python-shell-parse-command) proc-name
)))
1424 (process (get-buffer-process shell-buffer
)))
1427 (set-process-query-on-exit-flag process nil
)
1428 (should (process-live-p process
))
1429 (with-current-buffer shell-buffer
1430 (should (eq major-mode
'inferior-python-mode
))
1431 (should (string= (buffer-name) (format "*%s*" proc-name
)))))
1432 (kill-buffer shell-buffer
))))
1434 (ert-deftest python-shell-make-comint-2
()
1435 "Check comint creation for internal shell buffer."
1436 :expected-result
(if (executable-find python-tests-shell-interpreter
)
1439 (let* ((python-shell-interpreter
1440 (executable-find python-tests-shell-interpreter
))
1441 (proc-name (python-shell-internal-get-process-name))
1443 (python-tests-with-temp-buffer
1444 "" (python-shell-make-comint
1445 (python-shell-parse-command) proc-name nil t
)))
1446 (process (get-buffer-process shell-buffer
)))
1449 (set-process-query-on-exit-flag process nil
)
1450 (should (process-live-p process
))
1451 (with-current-buffer shell-buffer
1452 (should (eq major-mode
'inferior-python-mode
))
1453 (should (string= (buffer-name) (format " *%s*" proc-name
)))))
1454 (kill-buffer shell-buffer
))))
1456 (ert-deftest python-shell-get-process-1
()
1457 "Check dedicated shell process preference over global."
1458 :expected-result
(if (executable-find python-tests-shell-interpreter
)
1461 (python-tests-with-temp-file
1463 (let* ((python-shell-interpreter
1464 (executable-find python-tests-shell-interpreter
))
1465 (global-proc-name (python-shell-get-process-name nil
))
1466 (dedicated-proc-name (python-shell-get-process-name t
))
1467 (global-shell-buffer
1468 (python-shell-make-comint
1469 (python-shell-parse-command) global-proc-name
))
1470 (dedicated-shell-buffer
1471 (python-shell-make-comint
1472 (python-shell-parse-command) dedicated-proc-name
))
1473 (global-process (get-buffer-process global-shell-buffer
))
1474 (dedicated-process (get-buffer-process dedicated-shell-buffer
)))
1477 (set-process-query-on-exit-flag global-process nil
)
1478 (set-process-query-on-exit-flag dedicated-process nil
)
1479 ;; Prefer dedicated if global also exists.
1480 (should (equal (python-shell-get-process) dedicated-process
))
1481 (kill-buffer dedicated-shell-buffer
)
1482 ;; If there's only global, use it.
1483 (should (equal (python-shell-get-process) global-process
))
1484 (kill-buffer global-shell-buffer
)
1485 ;; No buffer available.
1486 (should (not (python-shell-get-process))))
1487 (ignore-errors (kill-buffer global-shell-buffer
))
1488 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
1490 (ert-deftest python-shell-get-or-create-process-1
()
1491 "Check shell process creation fallback."
1492 :expected-result
:failed
1493 (python-tests-with-temp-file
1495 ;; XXX: Break early until we can skip stuff. We need to mimic
1496 ;; user interaction because `python-shell-get-or-create-process'
1497 ;; asks for all arguments interactively when a shell process
1500 (let* ((python-shell-interpreter
1501 (executable-find python-tests-shell-interpreter
))
1503 (dedicated-process-name (python-shell-get-process-name t
))
1504 (dedicated-process (python-shell-get-or-create-process))
1505 (dedicated-shell-buffer (process-buffer dedicated-process
)))
1508 (set-process-query-on-exit-flag dedicated-process nil
)
1509 ;; Prefer dedicated if not buffer exist.
1510 (should (equal (process-name dedicated-process
)
1511 dedicated-process-name
))
1512 (kill-buffer dedicated-shell-buffer
)
1513 ;; No buffer available.
1514 (should (not (python-shell-get-process))))
1515 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
1517 (ert-deftest python-shell-internal-get-or-create-process-1
()
1518 "Check internal shell process creation fallback."
1519 :expected-result
(if (executable-find python-tests-shell-interpreter
)
1522 (python-tests-with-temp-file
1524 (should (not (process-live-p (python-shell-internal-get-process-name))))
1525 (let* ((python-shell-interpreter
1526 (executable-find python-tests-shell-interpreter
))
1527 (internal-process-name (python-shell-internal-get-process-name))
1528 (internal-process (python-shell-internal-get-or-create-process))
1529 (internal-shell-buffer (process-buffer internal-process
)))
1532 (set-process-query-on-exit-flag internal-process nil
)
1533 (should (equal (process-name internal-process
)
1534 internal-process-name
))
1535 (should (equal internal-process
1536 (python-shell-internal-get-or-create-process)))
1537 ;; No user buffer available.
1538 (should (not (python-shell-get-process)))
1539 (kill-buffer internal-shell-buffer
))
1540 (ignore-errors (kill-buffer internal-shell-buffer
))))))
1543 ;;; Shell completion
1546 ;;; PDB Track integration
1549 ;;; Symbol completion
1568 (ert-deftest python-imenu-prev-index-position-1
()
1570 (python-tests-with-temp-buffer
1572 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1573 '''print decorated function call data to stdout.
1577 @decoratorFunctionWithArguments('arg1', 'arg2')
1578 def func(a, b, c=True):
1583 print 'Inside wwrap()'
1584 def wrapped_f(*args):
1585 print 'Inside wrapped_f()'
1586 print 'Decorator arguments:', arg1, arg2, arg3
1588 print 'After f(*args)'
1592 def test(): # Some comment
1593 'This is a test function'
1611 '(("*Rescan*" . -
99)
1612 ("decoratorFunctionWithArguments" .
2)
1613 ("decoratorFunctionWithArguments.wwrap" .
224)
1614 ("decoratorFunctionWithArguments.wwrap.wrapped_f" .
273)
1623 (should (= (cdr (assoc-string (car elt
) expected
))
1624 (if (markerp (cdr elt
))
1625 (marker-position (cdr elt
))
1627 (imenu--make-index-alist)))))
1632 (ert-deftest python-info-current-defun-1
()
1633 (python-tests-with-temp-buffer
1638 (should (string= "foo" (python-info-current-defun)))
1639 (should (string= "def foo" (python-info-current-defun t)))
1641 (should (not (python-info-current-defun)))
1642 (indent-for-tab-command)
1643 (should (string= "foo" (python-info-current-defun)))
1644 (should (string= "def foo" (python-info-current-defun t)))))
1646 (ert-deftest python-info-current-defun-2
()
1647 (python-tests-with-temp-buffer
1653 return [i for i in range(3)]
1667 (should (string= "C" (python-info-current-defun)))
1668 (should (string= "class C" (python-info-current-defun t)))
1669 (python-tests-look-at "return [i for ")
1670 (should (string= "C.m" (python-info-current-defun)))
1671 (should (string= "def C.m" (python-info-current-defun t)))
1672 (python-tests-look-at "def b():")
1673 (should (string= "C.m.b" (python-info-current-defun)))
1674 (should (string= "def C.m.b" (python-info-current-defun t)))
1676 (indent-for-tab-command)
1677 (python-indent-dedent-line-backspace 1)
1678 (should (string= "C.m" (python-info-current-defun)))
1679 (should (string= "def C.m" (python-info-current-defun t)))
1680 (python-tests-look-at "def c(self):")
1682 (indent-for-tab-command)
1683 (should (string= "C.m.a" (python-info-current-defun)))
1684 (should (string= "def C.m.a" (python-info-current-defun t)))
1685 (python-indent-dedent-line-backspace 1)
1686 (should (string= "C.m" (python-info-current-defun)))
1687 (should (string= "def C.m" (python-info-current-defun t)))
1688 (python-indent-dedent-line-backspace 1)
1689 (should (string= "C" (python-info-current-defun)))
1690 (should (string= "class C" (python-info-current-defun t)))
1691 (python-tests-look-at "def c(self):")
1692 (should (string= "C.c" (python-info-current-defun)))
1693 (should (string= "def C.c" (python-info-current-defun t)))
1694 (python-tests-look-at "do_c()")
1695 (should (string= "C.c" (python-info-current-defun)))
1696 (should (string= "def C.c" (python-info-current-defun t)))))
1698 (ert-deftest python-info-current-defun-3
()
1699 (python-tests-with-temp-buffer
1701 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1702 '''print decorated function call data to stdout.
1706 @decoratorFunctionWithArguments('arg1', 'arg2')
1707 def func(a, b, c=True):
1712 print 'Inside wwrap()'
1713 def wrapped_f(*args):
1714 print 'Inside wrapped_f()'
1715 print 'Decorator arguments:', arg1, arg2, arg3
1717 print 'After f(*args)'
1721 (python-tests-look-at "def wwrap(f):")
1723 (should (not (python-info-current-defun)))
1724 (indent-for-tab-command 1)
1725 (should (string= (python-info-current-defun)
1726 "decoratorFunctionWithArguments"))
1727 (should (string= (python-info-current-defun t)
1728 "def decoratorFunctionWithArguments"))
1729 (python-tests-look-at "def wrapped_f(*args):")
1730 (should (string= (python-info-current-defun)
1731 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
1732 (should (string= (python-info-current-defun t)
1733 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
1734 (python-tests-look-at "return wrapped_f")
1735 (should (string= (python-info-current-defun)
1736 "decoratorFunctionWithArguments.wwrap"))
1737 (should (string= (python-info-current-defun t)
1738 "def decoratorFunctionWithArguments.wwrap"))
1740 (python-tests-look-at "return wwrap")
1741 (should (string= (python-info-current-defun)
1742 "decoratorFunctionWithArguments"))
1743 (should (string= (python-info-current-defun t)
1744 "def decoratorFunctionWithArguments"))))
1746 (ert-deftest python-info-current-symbol-1
()
1747 (python-tests-with-temp-buffer
1757 (python-tests-look-at "self.c()")
1758 (should (string= "self.c" (python-info-current-symbol)))
1759 (should (string= "C.c" (python-info-current-symbol t
)))))
1761 (ert-deftest python-info-current-symbol-2
()
1762 (python-tests-with-temp-buffer
1774 (python-tests-look-at "self.c()")
1775 (should (string= "self.c" (python-info-current-symbol)))
1776 (should (string= "C.M.c" (python-info-current-symbol t
)))))
1778 (ert-deftest python-info-current-symbol-3
()
1779 "Keywords should not be considered symbols."
1780 :expected-result
:failed
1781 (python-tests-with-temp-buffer
1786 ;; FIXME: keywords are not symbols.
1787 (python-tests-look-at "class C")
1788 (should (not (python-info-current-symbol)))
1789 (should (not (python-info-current-symbol t
)))
1790 (python-tests-look-at "C(object)")
1791 (should (string= "C" (python-info-current-symbol)))
1792 (should (string= "class C" (python-info-current-symbol t
)))))
1794 (ert-deftest python-info-statement-starts-block-p-1
()
1795 (python-tests-with-temp-buffer
1797 def long_function_name(
1798 var_one, var_two, var_three,
1802 (python-tests-look-at "def long_function_name")
1803 (should (python-info-statement-starts-block-p))
1804 (python-tests-look-at "print (var_one)")
1805 (python-util-forward-comment -
1)
1806 (should (python-info-statement-starts-block-p))))
1808 (ert-deftest python-info-statement-starts-block-p-2
()
1809 (python-tests-with-temp-buffer
1811 if width == 0 and height == 0 and \\\\
1812 color == 'red' and emphasis == 'strong' or \\\\
1814 raise ValueError('sorry, you lose')
1816 (python-tests-look-at "if width == 0 and")
1817 (should (python-info-statement-starts-block-p))
1818 (python-tests-look-at "raise ValueError(")
1819 (python-util-forward-comment -
1)
1820 (should (python-info-statement-starts-block-p))))
1822 (ert-deftest python-info-statement-ends-block-p-1
()
1823 (python-tests-with-temp-buffer
1825 def long_function_name(
1826 var_one, var_two, var_three,
1830 (python-tests-look-at "print (var_one)")
1831 (should (python-info-statement-ends-block-p))))
1833 (ert-deftest python-info-statement-ends-block-p-2
()
1834 (python-tests-with-temp-buffer
1836 if width == 0 and height == 0 and \\\\
1837 color == 'red' and emphasis == 'strong' or \\\\
1844 (python-tests-look-at "raise ValueError(")
1845 (should (python-info-statement-ends-block-p))))
1847 (ert-deftest python-info-beginning-of-statement-p-1
()
1848 (python-tests-with-temp-buffer
1850 def long_function_name(
1851 var_one, var_two, var_three,
1855 (python-tests-look-at "def long_function_name")
1856 (should (python-info-beginning-of-statement-p))
1858 (should (not (python-info-beginning-of-statement-p)))
1859 (python-tests-look-at "print (var_one)")
1860 (should (python-info-beginning-of-statement-p))
1861 (goto-char (line-beginning-position))
1862 (should (not (python-info-beginning-of-statement-p)))))
1864 (ert-deftest python-info-beginning-of-statement-p-2
()
1865 (python-tests-with-temp-buffer
1867 if width == 0 and height == 0 and \\\\
1868 color == 'red' and emphasis == 'strong' or \\\\
1875 (python-tests-look-at "if width == 0 and")
1876 (should (python-info-beginning-of-statement-p))
1878 (should (not (python-info-beginning-of-statement-p)))
1879 (python-tests-look-at "raise ValueError(")
1880 (should (python-info-beginning-of-statement-p))
1881 (goto-char (line-beginning-position))
1882 (should (not (python-info-beginning-of-statement-p)))))
1884 (ert-deftest python-info-end-of-statement-p-1
()
1885 (python-tests-with-temp-buffer
1887 def long_function_name(
1888 var_one, var_two, var_three,
1892 (python-tests-look-at "def long_function_name")
1893 (should (not (python-info-end-of-statement-p)))
1895 (should (not (python-info-end-of-statement-p)))
1896 (python-tests-look-at "print (var_one)")
1897 (python-util-forward-comment -
1)
1898 (should (python-info-end-of-statement-p))
1899 (python-tests-look-at "print (var_one)")
1900 (should (not (python-info-end-of-statement-p)))
1902 (should (python-info-end-of-statement-p))))
1904 (ert-deftest python-info-end-of-statement-p-2
()
1905 (python-tests-with-temp-buffer
1907 if width == 0 and height == 0 and \\\\
1908 color == 'red' and emphasis == 'strong' or \\\\
1915 (python-tests-look-at "if width == 0 and")
1916 (should (not (python-info-end-of-statement-p)))
1918 (should (not (python-info-end-of-statement-p)))
1919 (python-tests-look-at "raise ValueError(")
1920 (python-util-forward-comment -
1)
1921 (should (python-info-end-of-statement-p))
1922 (python-tests-look-at "raise ValueError(")
1923 (should (not (python-info-end-of-statement-p)))
1925 (should (not (python-info-end-of-statement-p)))
1926 (goto-char (point-max))
1927 (python-util-forward-comment -
1)
1928 (should (python-info-end-of-statement-p))))
1930 (ert-deftest python-info-beginning-of-block-p-1
()
1931 (python-tests-with-temp-buffer
1933 def long_function_name(
1934 var_one, var_two, var_three,
1938 (python-tests-look-at "def long_function_name")
1939 (should (python-info-beginning-of-block-p))
1940 (python-tests-look-at "var_one, var_two, var_three,")
1941 (should (not (python-info-beginning-of-block-p)))
1942 (python-tests-look-at "print (var_one)")
1943 (should (not (python-info-beginning-of-block-p)))))
1945 (ert-deftest python-info-beginning-of-block-p-2
()
1946 (python-tests-with-temp-buffer
1948 if width == 0 and height == 0 and \\\\
1949 color == 'red' and emphasis == 'strong' or \\\\
1956 (python-tests-look-at "if width == 0 and")
1957 (should (python-info-beginning-of-block-p))
1958 (python-tests-look-at "color == 'red' and emphasis")
1959 (should (not (python-info-beginning-of-block-p)))
1960 (python-tests-look-at "raise ValueError(")
1961 (should (not (python-info-beginning-of-block-p)))))
1963 (ert-deftest python-info-end-of-block-p-1
()
1964 (python-tests-with-temp-buffer
1966 def long_function_name(
1967 var_one, var_two, var_three,
1971 (python-tests-look-at "def long_function_name")
1972 (should (not (python-info-end-of-block-p)))
1973 (python-tests-look-at "var_one, var_two, var_three,")
1974 (should (not (python-info-end-of-block-p)))
1975 (python-tests-look-at "var_four):")
1977 (should (not (python-info-end-of-block-p)))
1978 (python-tests-look-at "print (var_one)")
1979 (should (not (python-info-end-of-block-p)))
1981 (should (python-info-end-of-block-p))))
1983 (ert-deftest python-info-end-of-block-p-2
()
1984 (python-tests-with-temp-buffer
1986 if width == 0 and height == 0 and \\\\
1987 color == 'red' and emphasis == 'strong' or \\\\
1994 (python-tests-look-at "if width == 0 and")
1995 (should (not (python-info-end-of-block-p)))
1996 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
1997 (should (not (python-info-end-of-block-p)))
1998 (python-tests-look-at "highlight > 100:")
2000 (should (not (python-info-end-of-block-p)))
2001 (python-tests-look-at "raise ValueError(")
2002 (should (not (python-info-end-of-block-p)))
2004 (should (not (python-info-end-of-block-p)))
2005 (goto-char (point-max))
2006 (python-util-forward-comment -
1)
2007 (should (python-info-end-of-block-p))))
2009 (ert-deftest python-info-closing-block-1
()
2010 (python-tests-with-temp-buffer
2012 if request.user.is_authenticated():
2014 profile = request.user.get_profile()
2015 except Profile.DoesNotExist:
2016 profile = Profile.objects.create(user=request.user)
2019 profile.recalculate_stats()
2021 profile.clear_stats()
2026 (python-tests-look-at "try:")
2027 (should (not (python-info-closing-block)))
2028 (python-tests-look-at "except Profile.DoesNotExist:")
2029 (should (= (python-tests-look-at "try:" -
1 t
)
2030 (python-info-closing-block)))
2031 (python-tests-look-at "else:")
2032 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -
1 t
)
2033 (python-info-closing-block)))
2034 (python-tests-look-at "if profile.stats:")
2035 (should (not (python-info-closing-block)))
2036 (python-tests-look-at "else:")
2037 (should (= (python-tests-look-at "if profile.stats:" -
1 t
)
2038 (python-info-closing-block)))
2039 (python-tests-look-at "finally:")
2040 (should (= (python-tests-look-at "else:" -
2 t
)
2041 (python-info-closing-block)))))
2043 (ert-deftest python-info-closing-block-2
()
2044 (python-tests-with-temp-buffer
2046 if request.user.is_authenticated():
2047 profile = Profile.objects.get_or_create(user=request.user)
2049 profile.recalculate_stats()
2056 (python-tests-look-at "'else': 'do it'")
2057 (should (not (python-info-closing-block)))
2058 (python-tests-look-at "'else'")
2059 (should (not (python-info-closing-block)))))
2061 (ert-deftest python-info-line-ends-backslash-p-1
()
2062 (python-tests-with-temp-buffer
2064 objects = Thing.objects.all() \\\\
2074 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
2075 (should (python-info-line-ends-backslash-p 3))
2076 (should (python-info-line-ends-backslash-p 4))
2077 (should (python-info-line-ends-backslash-p 5))
2078 (should (python-info-line-ends-backslash-p 6)) ; ) \...
2079 (should (python-info-line-ends-backslash-p 7))
2080 (should (python-info-line-ends-backslash-p 8))
2081 (should (python-info-line-ends-backslash-p 9))
2082 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
2084 (ert-deftest python-info-beginning-of-backslash-1
()
2085 (python-tests-with-temp-buffer
2087 objects = Thing.objects.all() \\\\
2098 (second (python-tests-look-at ".filter("))
2099 (third (python-tests-look-at ".aggregate(")))
2100 (should (= first
(python-info-beginning-of-backslash 2)))
2101 (should (= second
(python-info-beginning-of-backslash 3)))
2102 (should (= second
(python-info-beginning-of-backslash 4)))
2103 (should (= second
(python-info-beginning-of-backslash 5)))
2104 (should (= second
(python-info-beginning-of-backslash 6)))
2105 (should (= third
(python-info-beginning-of-backslash 7)))
2106 (should (= third
(python-info-beginning-of-backslash 8)))
2107 (should (= third
(python-info-beginning-of-backslash 9)))
2108 (should (not (python-info-beginning-of-backslash 10))))))
2110 (ert-deftest python-info-continuation-line-p-1
()
2111 (python-tests-with-temp-buffer
2113 if width == 0 and height == 0 and \\\\
2114 color == 'red' and emphasis == 'strong' or \\\\
2121 (python-tests-look-at "if width == 0 and height == 0 and")
2122 (should (not (python-info-continuation-line-p)))
2123 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2124 (should (python-info-continuation-line-p))
2125 (python-tests-look-at "highlight > 100:")
2126 (should (python-info-continuation-line-p))
2127 (python-tests-look-at "raise ValueError(")
2128 (should (not (python-info-continuation-line-p)))
2129 (python-tests-look-at "'sorry, you lose'")
2130 (should (python-info-continuation-line-p))
2132 (should (python-info-continuation-line-p))
2133 (python-tests-look-at ")")
2134 (should (python-info-continuation-line-p))
2136 (should (not (python-info-continuation-line-p)))))
2138 (ert-deftest python-info-block-continuation-line-p-1
()
2139 (python-tests-with-temp-buffer
2141 if width == 0 and height == 0 and \\\\
2142 color == 'red' and emphasis == 'strong' or \\\\
2149 (python-tests-look-at "if width == 0 and")
2150 (should (not (python-info-block-continuation-line-p)))
2151 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2152 (should (= (python-info-block-continuation-line-p)
2153 (python-tests-look-at "if width == 0 and" -
1 t
)))
2154 (python-tests-look-at "highlight > 100:")
2155 (should (not (python-info-block-continuation-line-p)))))
2157 (ert-deftest python-info-block-continuation-line-p-2
()
2158 (python-tests-with-temp-buffer
2165 (python-tests-look-at "def foo(a,")
2166 (should (not (python-info-block-continuation-line-p)))
2167 (python-tests-look-at "b,")
2168 (should (= (python-info-block-continuation-line-p)
2169 (python-tests-look-at "def foo(a," -
1 t
)))
2170 (python-tests-look-at "c):")
2171 (should (not (python-info-block-continuation-line-p)))))
2173 (ert-deftest python-info-assignment-continuation-line-p-1
()
2174 (python-tests-with-temp-buffer
2176 data = foo(), bar() \\\\
2180 (python-tests-look-at "data = foo(), bar()")
2181 (should (not (python-info-assignment-continuation-line-p)))
2182 (python-tests-look-at "baz(), 4")
2183 (should (= (python-info-assignment-continuation-line-p)
2184 (python-tests-look-at "foo()," -
1 t
)))
2185 (python-tests-look-at "5, 6")
2186 (should (not (python-info-assignment-continuation-line-p)))))
2188 (ert-deftest python-info-assignment-continuation-line-p-2
()
2189 (python-tests-with-temp-buffer
2191 data = (foo(), bar()
2195 (python-tests-look-at "data = (foo(), bar()")
2196 (should (not (python-info-assignment-continuation-line-p)))
2197 (python-tests-look-at "baz(), 4")
2198 (should (= (python-info-assignment-continuation-line-p)
2199 (python-tests-look-at "(foo()," -
1 t
)))
2200 (python-tests-look-at "5, 6)")
2201 (should (not (python-info-assignment-continuation-line-p)))))
2203 (ert-deftest python-info-looking-at-beginning-of-defun-1
()
2204 (python-tests-with-temp-buffer
2206 def decorat0r(deff):
2217 (python-tests-look-at "def decorat0r(deff):")
2218 (should (python-info-looking-at-beginning-of-defun))
2219 (python-tests-look-at "def foo(arg):")
2220 (should (not (python-info-looking-at-beginning-of-defun)))
2221 (python-tests-look-at "def wrap():")
2222 (should (python-info-looking-at-beginning-of-defun))
2223 (python-tests-look-at "deff()")
2224 (should (not (python-info-looking-at-beginning-of-defun)))))
2226 (ert-deftest python-info-current-line-comment-p-1
()
2227 (python-tests-with-temp-buffer
2230 foo = True # another comment
2234 print ('bar') # print bar
2236 (python-tests-look-at "# this is a comment")
2237 (should (python-info-current-line-comment-p))
2238 (python-tests-look-at "foo = True # another comment")
2239 (should (not (python-info-current-line-comment-p)))
2240 (python-tests-look-at "'#this is a string'")
2241 (should (not (python-info-current-line-comment-p)))
2242 (python-tests-look-at "# more comments")
2243 (should (python-info-current-line-comment-p))
2244 (python-tests-look-at "print ('bar') # print bar")
2245 (should (not (python-info-current-line-comment-p)))))
2247 (ert-deftest python-info-current-line-empty-p
()
2248 (python-tests-with-temp-buffer
2252 foo = True # another comment
2254 (should (python-info-current-line-empty-p))
2255 (python-tests-look-at "# this is a comment")
2256 (should (not (python-info-current-line-empty-p)))
2258 (should (python-info-current-line-empty-p))))
2261 ;;; Utility functions
2263 (ert-deftest python-util-goto-line-1
()
2264 (python-tests-with-temp-buffer
2269 pass" (make-string 20 ?
\n))
2270 (python-util-goto-line 10)
2271 (should (= (line-number-at-pos) 10))
2272 (python-util-goto-line 20)
2273 (should (= (line-number-at-pos) 20))))
2275 (ert-deftest python-util-clone-local-variables-1
()
2276 (let ((buffer (generate-new-buffer
2277 "python-util-clone-local-variables-1"))
2279 '((python-fill-docstring-style . django
)
2280 (python-shell-interpreter .
"python")
2281 (python-shell-interpreter-args .
"manage.py shell")
2282 (python-shell-prompt-regexp .
"In \\[[0-9]+\\]: ")
2283 (python-shell-prompt-output-regexp .
"Out\\[[0-9]+\\]: ")
2284 (python-shell-extra-pythonpaths "/home/user/pylib/")
2285 (python-shell-completion-setup-code
2286 .
"from IPython.core.completerlib import module_completion")
2287 (python-shell-completion-module-string-code
2288 .
"';'.join(module_completion('''%s'''))\n")
2289 (python-shell-completion-string-code
2290 .
"';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
2291 (python-shell-virtualenv-path
2292 .
"/home/user/.virtualenvs/project"))))
2293 (with-current-buffer buffer
2294 (kill-all-local-variables)
2295 (dolist (ccons varcons
)
2296 (set (make-local-variable (car ccons
)) (cdr ccons
))))
2297 (python-tests-with-temp-buffer
2299 (python-util-clone-local-variables buffer
)
2300 (dolist (ccons varcons
)
2302 (equal (symbol-value (car ccons
)) (cdr ccons
)))))
2303 (kill-buffer buffer
)))
2305 (ert-deftest python-util-forward-comment-1
()
2306 (python-tests-with-temp-buffer
2310 # bad indented comment
2311 # more comments" (make-string 9999 ?
\n))
2312 (python-util-forward-comment 1)
2313 (should (= (point) (point-max)))
2314 (python-util-forward-comment -
1)
2315 (should (= (point) (point-min)))))
2318 (provide 'python-tests
)
2322 ;; indent-tabs-mode: nil
2325 ;;; python-tests.el ends here