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 honouring."
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))))))
678 (ert-deftest python-nav-beginning-of-statement-1
()
679 (python-tests-with-temp-buffer
689 v3 = ('this is a string'
694 # this is a comment, yo
695 'continue previous line')
701 (python-tests-look-at "v2 =")
702 (python-util-forward-comment -
1)
703 (should (= (save-excursion
704 (python-nav-beginning-of-statement)
706 (python-tests-look-at "v1 =" -
1 t
)))
707 (python-tests-look-at "v3 =")
708 (python-util-forward-comment -
1)
709 (should (= (save-excursion
710 (python-nav-beginning-of-statement)
712 (python-tests-look-at "v2 =" -
1 t
)))
713 (python-tests-look-at "v4 =")
714 (python-util-forward-comment -
1)
715 (should (= (save-excursion
716 (python-nav-beginning-of-statement)
718 (python-tests-look-at "v3 =" -
1 t
)))
719 (goto-char (point-max))
720 (python-util-forward-comment -
1)
721 (should (= (save-excursion
722 (python-nav-beginning-of-statement)
724 (python-tests-look-at "v4 =" -
1 t
)))))
726 (ert-deftest python-nav-end-of-statement-1
()
727 (python-tests-with-temp-buffer
737 v3 = ('this is a string'
742 # this is a comment, yo
743 'continue previous line')
749 (python-tests-look-at "v1 =")
750 (should (= (save-excursion
751 (python-nav-end-of-statement)
754 (python-tests-look-at "789")
755 (line-end-position))))
756 (python-tests-look-at "v2 =")
757 (should (= (save-excursion
758 (python-nav-end-of-statement)
761 (python-tests-look-at "value4)")
762 (line-end-position))))
763 (python-tests-look-at "v3 =")
764 (should (= (save-excursion
765 (python-nav-end-of-statement)
768 (python-tests-look-at
769 "'continue previous line')")
770 (line-end-position))))
771 (python-tests-look-at "v4 =")
772 (should (= (save-excursion
773 (python-nav-end-of-statement)
776 (goto-char (point-max))
777 (python-util-forward-comment -
1)
780 (ert-deftest python-nav-forward-statement-1
()
781 (python-tests-with-temp-buffer
791 v3 = ('this is a string'
796 # this is a comment, yo
797 'continue previous line')
803 (python-tests-look-at "v1 =")
804 (should (= (save-excursion
805 (python-nav-forward-statement)
807 (python-tests-look-at "v2 =")))
808 (should (= (save-excursion
809 (python-nav-forward-statement)
811 (python-tests-look-at "v3 =")))
812 (should (= (save-excursion
813 (python-nav-forward-statement)
815 (python-tests-look-at "v4 =")))
816 (should (= (save-excursion
817 (python-nav-forward-statement)
821 (ert-deftest python-nav-backward-statement-1
()
822 (python-tests-with-temp-buffer
832 v3 = ('this is a string'
837 # this is a comment, yo
838 'continue previous line')
844 (goto-char (point-max))
845 (should (= (save-excursion
846 (python-nav-backward-statement)
848 (python-tests-look-at "v4 =" -
1)))
849 (should (= (save-excursion
850 (python-nav-backward-statement)
852 (python-tests-look-at "v3 =" -
1)))
853 (should (= (save-excursion
854 (python-nav-backward-statement)
856 (python-tests-look-at "v2 =" -
1)))
857 (should (= (save-excursion
858 (python-nav-backward-statement)
860 (python-tests-look-at "v1 =" -
1)))))
862 (ert-deftest python-nav-backward-statement-2
()
863 :expected-result
:failed
864 (python-tests-with-temp-buffer
875 ;; FIXME: For some reason `python-nav-backward-statement' is moving
876 ;; back two sentences when starting from 'value4)'.
877 (goto-char (point-max))
878 (python-util-forward-comment -
1)
879 (should (= (save-excursion
880 (python-nav-backward-statement)
882 (python-tests-look-at "v2 =" -
1 t
)))))
884 (ert-deftest python-nav-beginning-of-block-1
()
885 (python-tests-with-temp-buffer
887 def decoratorFunctionWithArguments(arg1, arg2, arg3):
888 '''print decorated function call data to stdout.
892 @decoratorFunctionWithArguments('arg1', 'arg2')
893 def func(a, b, c=True):
898 print 'Inside wwrap()'
899 def wrapped_f(*args):
900 print 'Inside wrapped_f()'
901 print 'Decorator arguments:', arg1, arg2, arg3
903 print 'After f(*args)'
907 (python-tests-look-at "return wwrap")
908 (should (= (save-excursion
909 (python-nav-beginning-of-block)
911 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)))
912 (python-tests-look-at "print 'Inside wwrap()'")
913 (should (= (save-excursion
914 (python-nav-beginning-of-block)
916 (python-tests-look-at "def wwrap(f):" -
1)))
917 (python-tests-look-at "print 'After f(*args)'")
919 (should (= (save-excursion
920 (python-nav-beginning-of-block)
922 (python-tests-look-at "def wrapped_f(*args):" -
1)))
923 (python-tests-look-at "return wrapped_f")
924 (should (= (save-excursion
925 (python-nav-beginning-of-block)
927 (python-tests-look-at "def wwrap(f):" -
1)))))
929 (ert-deftest python-nav-end-of-block-1
()
930 (python-tests-with-temp-buffer
932 def decoratorFunctionWithArguments(arg1, arg2, arg3):
933 '''print decorated function call data to stdout.
937 @decoratorFunctionWithArguments('arg1', 'arg2')
938 def func(a, b, c=True):
943 print 'Inside wwrap()'
944 def wrapped_f(*args):
945 print 'Inside wrapped_f()'
946 print 'Decorator arguments:', arg1, arg2, arg3
948 print 'After f(*args)'
952 (python-tests-look-at "def decoratorFunctionWithArguments")
953 (should (= (save-excursion
954 (python-nav-end-of-block)
957 (goto-char (point-max))
958 (python-util-forward-comment -
1)
960 (python-tests-look-at "def wwrap(f):")
961 (should (= (save-excursion
962 (python-nav-end-of-block)
965 (python-tests-look-at "return wrapped_f")
966 (line-end-position))))
968 (should (= (save-excursion
969 (python-nav-end-of-block)
972 (python-tests-look-at "return wrapped_f")
973 (line-end-position))))
974 (python-tests-look-at "f(*args)")
975 (should (= (save-excursion
976 (python-nav-end-of-block)
979 (python-tests-look-at "print 'After f(*args)'")
980 (line-end-position))))))
982 (ert-deftest python-nav-forward-block-1
()
983 "This also accounts as a test for `python-nav-backward-block'."
984 (python-tests-with-temp-buffer
986 if request.user.is_authenticated():
990 profile = request.user.get_profile()
991 except Profile.DoesNotExist:
992 profile = Profile.objects.create(user=request.user)
995 profile.recalculate_stats()
997 profile.clear_stats()
1002 (should (= (save-excursion (python-nav-forward-block))
1003 (python-tests-look-at "if request.user.is_authenticated():")))
1004 (should (= (save-excursion (python-nav-forward-block))
1005 (python-tests-look-at "try:")))
1006 (should (= (save-excursion (python-nav-forward-block))
1007 (python-tests-look-at "except Profile.DoesNotExist:")))
1008 (should (= (save-excursion (python-nav-forward-block))
1009 (python-tests-look-at "else:")))
1010 (should (= (save-excursion (python-nav-forward-block))
1011 (python-tests-look-at "if profile.stats:")))
1012 (should (= (save-excursion (python-nav-forward-block))
1013 (python-tests-look-at "else:")))
1014 (should (= (save-excursion (python-nav-forward-block))
1015 (python-tests-look-at "finally:")))
1016 ;; When point is at the last block, leave it there and return nil
1017 (should (not (save-excursion (python-nav-forward-block))))
1018 ;; Move backwards, and even if the number of moves is less than the
1019 ;; provided argument return the point.
1020 (should (= (save-excursion (python-nav-forward-block -
10))
1021 (python-tests-look-at
1022 "if request.user.is_authenticated():" -
1)))))
1024 (ert-deftest python-nav-lisp-forward-sexp-safe-1
()
1025 (python-tests-with-temp-buffer
1027 profile = Profile.objects.create(user=request.user)
1030 (python-tests-look-at "profile =")
1031 (python-nav-lisp-forward-sexp-safe 4)
1032 (should (looking-at "(user=request.user)"))
1033 (python-tests-look-at "user=request.user")
1034 (python-nav-lisp-forward-sexp-safe -
1)
1035 (should (looking-at "(user=request.user)"))
1036 (python-nav-lisp-forward-sexp-safe -
4)
1037 (should (looking-at "profile ="))
1038 (python-tests-look-at "user=request.user")
1039 (python-nav-lisp-forward-sexp-safe 3)
1040 (should (looking-at ")"))
1041 (python-nav-lisp-forward-sexp-safe 1)
1042 (should (looking-at "$"))
1043 (python-nav-lisp-forward-sexp-safe 1)
1044 (should (looking-at ".notify()"))))
1046 (ert-deftest python-nav-forward-sexp-1
()
1047 (python-tests-with-temp-buffer
1053 (python-tests-look-at "a()")
1054 (python-nav-forward-sexp)
1055 (should (looking-at "$"))
1056 (should (save-excursion
1058 (looking-at "a()")))
1059 (python-nav-forward-sexp)
1060 (should (looking-at "$"))
1061 (should (save-excursion
1063 (looking-at "b()")))
1064 (python-nav-forward-sexp)
1065 (should (looking-at "$"))
1066 (should (save-excursion
1068 (looking-at "c()")))
1069 ;; Movement next to a paren should do what lisp does and
1070 ;; unfortunately It can't change, because otherwise
1071 ;; `blink-matching-open' breaks.
1072 (python-nav-forward-sexp -
1)
1073 (should (looking-at "()"))
1074 (should (save-excursion
1076 (looking-at "c()")))
1077 (python-nav-forward-sexp -
1)
1078 (should (looking-at "c()"))
1079 (python-nav-forward-sexp -
1)
1080 (should (looking-at "b()"))
1081 (python-nav-forward-sexp -
1)
1082 (should (looking-at "a()"))))
1084 (ert-deftest python-nav-forward-sexp-2
()
1085 (python-tests-with-temp-buffer
1094 (python-tests-look-at "aa =")
1095 (python-nav-forward-sexp)
1096 (should (looking-at " = bbb"))
1097 (python-nav-forward-sexp)
1098 (should (looking-at "$"))
1099 (should (save-excursion
1100 (back-to-indentation)
1101 (looking-at "aaa = bbb")))
1102 (python-nav-forward-sexp)
1103 (should (looking-at "$"))
1104 (should (save-excursion
1105 (back-to-indentation)
1106 (looking-at "ccc = ddd")))
1107 (python-nav-forward-sexp)
1108 (should (looking-at "$"))
1109 (should (save-excursion
1110 (back-to-indentation)
1111 (looking-at "eee = fff")))
1112 (python-nav-forward-sexp)
1113 (should (looking-at "$"))
1114 (should (save-excursion
1115 (back-to-indentation)
1116 (looking-at "return ggg")))
1117 (python-nav-forward-sexp -
1)
1118 (should (looking-at "def func():"))))
1120 (ert-deftest python-nav-forward-sexp-3
()
1121 (python-tests-with-temp-buffer
1123 from some_module import some_sub_module
1124 from another_module import another_sub_module
1126 def another_statement():
1129 (python-tests-look-at "some_module")
1130 (python-nav-forward-sexp)
1131 (should (looking-at " import"))
1132 (python-nav-forward-sexp)
1133 (should (looking-at " some_sub_module"))
1134 (python-nav-forward-sexp)
1135 (should (looking-at "$"))
1138 (back-to-indentation)
1140 "from some_module import some_sub_module")))
1141 (python-nav-forward-sexp)
1142 (should (looking-at "$"))
1145 (back-to-indentation)
1147 "from another_module import another_sub_module")))
1148 (python-nav-forward-sexp)
1149 (should (looking-at "$"))
1152 (back-to-indentation)
1155 (python-nav-forward-sexp -
1)
1156 (should (looking-at "def another_statement():"))
1157 (python-nav-forward-sexp -
1)
1158 (should (looking-at "from another_module import another_sub_module"))
1159 (python-nav-forward-sexp -
1)
1160 (should (looking-at "from some_module import some_sub_module"))))
1162 (ert-deftest python-nav-up-list-1
()
1163 (python-tests-with-temp-buffer
1167 return [i for i in range(3)]
1169 (python-tests-look-at "3)]")
1170 (python-nav-up-list)
1171 (should (looking-at "]"))
1172 (python-nav-up-list)
1173 (should (looking-at "$"))))
1175 (ert-deftest python-nav-backward-up-list-1
()
1176 :expected-result
:failed
1177 (python-tests-with-temp-buffer
1181 return [i for i in range(3)]
1183 (python-tests-look-at "3)]")
1184 (python-nav-backward-up-list)
1185 (should (looking-at "(3)\\]"))
1186 (python-nav-backward-up-list)
1188 "\\[i for i in range(3)\\]"))
1189 ;; FIXME: Need to move to beginning-of-statement.
1190 (python-nav-backward-up-list)
1192 "return \\[i for i in range(3)\\]"))
1193 (python-nav-backward-up-list)
1194 (should (looking-at "if True:"))
1195 (python-nav-backward-up-list)
1196 (should (looking-at "def f():"))))
1199 ;;; Shell integration
1201 (defvar python-tests-shell-interpreter
"python")
1203 (ert-deftest python-shell-get-process-name-1
()
1204 "Check process name calculation on different scenarios."
1205 (python-tests-with-temp-buffer
1207 (should (string= (python-shell-get-process-name nil
)
1208 python-shell-buffer-name
))
1209 ;; When the `current-buffer' doesn't have `buffer-file-name', even
1210 ;; if dedicated flag is non-nil should not include its name.
1211 (should (string= (python-shell-get-process-name t
)
1212 python-shell-buffer-name
)))
1213 (python-tests-with-temp-file
1215 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1216 ;; should be respected.
1217 (should (string= (python-shell-get-process-name nil
)
1218 python-shell-buffer-name
))
1220 (python-shell-get-process-name t
)
1221 (format "%s[%s]" python-shell-buffer-name buffer-file-name
)))))
1223 (ert-deftest python-shell-internal-get-process-name-1
()
1224 "Check the internal process name is config-unique."
1225 (let* ((python-shell-interpreter python-tests-shell-interpreter
)
1226 (python-shell-interpreter-args "")
1227 (python-shell-prompt-regexp ">>> ")
1228 (python-shell-prompt-block-regexp "[.][.][.] ")
1229 (python-shell-setup-codes "")
1230 (python-shell-process-environment "")
1231 (python-shell-extra-pythonpaths "")
1232 (python-shell-exec-path "")
1233 (python-shell-virtualenv-path "")
1234 (expected (python-tests-with-temp-buffer
1235 "" (python-shell-internal-get-process-name))))
1236 ;; Same configurations should match.
1239 (python-tests-with-temp-buffer
1240 "" (python-shell-internal-get-process-name))))
1241 (let ((python-shell-interpreter-args "-B"))
1242 ;; A minimal change should generate different names.
1246 (python-tests-with-temp-buffer
1247 "" (python-shell-internal-get-process-name))))))))
1249 (ert-deftest python-shell-parse-command-1
()
1250 "Check the command to execute is calculated correctly.
1251 Using `python-shell-interpreter' and
1252 `python-shell-interpreter-args'."
1253 :expected-result
(if (executable-find python-tests-shell-interpreter
)
1256 (let ((python-shell-interpreter (executable-find
1257 python-tests-shell-interpreter
))
1258 (python-shell-interpreter-args "-B"))
1261 python-shell-interpreter
1262 python-shell-interpreter-args
)
1263 (python-shell-parse-command)))))
1265 (ert-deftest python-shell-calculate-process-environment-1
()
1266 "Test `python-shell-process-environment' modification."
1267 (let* ((original-process-environment process-environment
)
1268 (python-shell-process-environment
1269 '("TESTVAR1=value1" "TESTVAR2=value2"))
1270 (process-environment
1271 (python-shell-calculate-process-environment)))
1272 (should (equal (getenv "TESTVAR1") "value1"))
1273 (should (equal (getenv "TESTVAR2") "value2"))))
1275 (ert-deftest python-shell-calculate-process-environment-2
()
1276 "Test `python-shell-extra-pythonpaths' modification."
1277 (let* ((original-process-environment process-environment
)
1278 (original-pythonpath (getenv "PYTHONPATH"))
1279 (paths '("path1" "path2"))
1280 (python-shell-extra-pythonpaths paths
)
1281 (process-environment
1282 (python-shell-calculate-process-environment)))
1283 (should (equal (getenv "PYTHONPATH")
1285 (mapconcat 'identity paths path-separator
)
1286 path-separator original-pythonpath
)))))
1288 (ert-deftest python-shell-calculate-process-environment-3
()
1289 "Test `python-shell-virtualenv-path' modification."
1290 (let* ((original-process-environment process-environment
)
1291 (original-path (or (getenv "PATH") ""))
1292 (python-shell-virtualenv-path
1293 (directory-file-name user-emacs-directory
))
1294 (process-environment
1295 (python-shell-calculate-process-environment)))
1296 (should (not (getenv "PYTHONHOME")))
1297 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-path
))
1298 (should (equal (getenv "PATH")
1299 (format "%s/bin%s%s"
1300 python-shell-virtualenv-path
1301 path-separator original-path
)))))
1303 (ert-deftest python-shell-calculate-exec-path-1
()
1304 "Test `python-shell-exec-path' modification."
1305 (let* ((original-exec-path exec-path
)
1306 (python-shell-exec-path '("path1" "path2"))
1307 (exec-path (python-shell-calculate-exec-path)))
1310 (append python-shell-exec-path
1311 original-exec-path
)))))
1313 (ert-deftest python-shell-calculate-exec-path-2
()
1314 "Test `python-shell-exec-path' modification."
1315 (let* ((original-exec-path exec-path
)
1316 (python-shell-virtualenv-path
1317 (directory-file-name user-emacs-directory
))
1318 (exec-path (python-shell-calculate-exec-path)))
1322 (format "%s/bin" python-shell-virtualenv-path
)
1323 original-exec-path
))))))
1325 (ert-deftest python-shell-make-comint-1
()
1326 "Check comint creation for global shell buffer."
1327 :expected-result
(if (executable-find python-tests-shell-interpreter
)
1330 (let* ((python-shell-interpreter
1331 (executable-find python-tests-shell-interpreter
))
1332 (proc-name (python-shell-get-process-name nil
))
1334 (python-tests-with-temp-buffer
1335 "" (python-shell-make-comint
1336 (python-shell-parse-command) proc-name
)))
1337 (process (get-buffer-process shell-buffer
)))
1340 (set-process-query-on-exit-flag process nil
)
1341 (should (process-live-p process
))
1342 (with-current-buffer shell-buffer
1343 (should (eq major-mode
'inferior-python-mode
))
1344 (should (string= (buffer-name) (format "*%s*" proc-name
)))))
1345 (kill-buffer shell-buffer
))))
1347 (ert-deftest python-shell-make-comint-2
()
1348 "Check comint creation for internal shell buffer."
1349 :expected-result
(if (executable-find python-tests-shell-interpreter
)
1352 (let* ((python-shell-interpreter
1353 (executable-find python-tests-shell-interpreter
))
1354 (proc-name (python-shell-internal-get-process-name))
1356 (python-tests-with-temp-buffer
1357 "" (python-shell-make-comint
1358 (python-shell-parse-command) proc-name nil t
)))
1359 (process (get-buffer-process shell-buffer
)))
1362 (set-process-query-on-exit-flag process nil
)
1363 (should (process-live-p process
))
1364 (with-current-buffer shell-buffer
1365 (should (eq major-mode
'inferior-python-mode
))
1366 (should (string= (buffer-name) (format " *%s*" proc-name
)))))
1367 (kill-buffer shell-buffer
))))
1369 (ert-deftest python-shell-get-process-1
()
1370 "Check dedicated shell process preference over global."
1371 :expected-result
(if (executable-find python-tests-shell-interpreter
)
1374 (python-tests-with-temp-file
1376 (let* ((python-shell-interpreter
1377 (executable-find python-tests-shell-interpreter
))
1378 (global-proc-name (python-shell-get-process-name nil
))
1379 (dedicated-proc-name (python-shell-get-process-name t
))
1380 (global-shell-buffer
1381 (python-shell-make-comint
1382 (python-shell-parse-command) global-proc-name
))
1383 (dedicated-shell-buffer
1384 (python-shell-make-comint
1385 (python-shell-parse-command) dedicated-proc-name
))
1386 (global-process (get-buffer-process global-shell-buffer
))
1387 (dedicated-process (get-buffer-process dedicated-shell-buffer
)))
1390 (set-process-query-on-exit-flag global-process nil
)
1391 (set-process-query-on-exit-flag dedicated-process nil
)
1392 ;; Prefer dedicated if global also exists.
1393 (should (equal (python-shell-get-process) dedicated-process
))
1394 (kill-buffer dedicated-shell-buffer
)
1395 ;; If there's only global, use it.
1396 (should (equal (python-shell-get-process) global-process
))
1397 (kill-buffer global-shell-buffer
)
1398 ;; No buffer available.
1399 (should (not (python-shell-get-process))))
1400 (ignore-errors (kill-buffer global-shell-buffer
))
1401 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
1403 (ert-deftest python-shell-get-or-create-process-1
()
1404 "Check shell process creation fallback."
1405 :expected-result
:failed
1406 (python-tests-with-temp-file
1408 ;; XXX: Break early until we can skip stuff. We need to mimic
1409 ;; user interaction because `python-shell-get-or-create-process'
1410 ;; asks for all arguments interactively when a shell process
1413 (let* ((python-shell-interpreter
1414 (executable-find python-tests-shell-interpreter
))
1416 (dedicated-process-name (python-shell-get-process-name t
))
1417 (dedicated-process (python-shell-get-or-create-process))
1418 (dedicated-shell-buffer (process-buffer dedicated-process
)))
1421 (set-process-query-on-exit-flag dedicated-process nil
)
1422 ;; Prefer dedicated if not buffer exist.
1423 (should (equal (process-name dedicated-process
)
1424 dedicated-process-name
))
1425 (kill-buffer dedicated-shell-buffer
)
1426 ;; No buffer available.
1427 (should (not (python-shell-get-process))))
1428 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
1430 (ert-deftest python-shell-internal-get-or-create-process-1
()
1431 "Check internal shell process creation fallback."
1432 :expected-result
(if (executable-find python-tests-shell-interpreter
)
1435 (python-tests-with-temp-file
1437 (should (not (process-live-p (python-shell-internal-get-process-name))))
1438 (let* ((python-shell-interpreter
1439 (executable-find python-tests-shell-interpreter
))
1440 (internal-process-name (python-shell-internal-get-process-name))
1441 (internal-process (python-shell-internal-get-or-create-process))
1442 (internal-shell-buffer (process-buffer internal-process
)))
1445 (set-process-query-on-exit-flag internal-process nil
)
1446 (should (equal (process-name internal-process
)
1447 internal-process-name
))
1448 (should (equal internal-process
1449 (python-shell-internal-get-or-create-process)))
1450 ;; No user buffer available.
1451 (should (not (python-shell-get-process)))
1452 (kill-buffer internal-shell-buffer
))
1453 (ignore-errors (kill-buffer internal-shell-buffer
))))))
1456 ;;; Shell completion
1459 ;;; PDB Track integration
1462 ;;; Symbol completion
1481 (ert-deftest python-imenu-prev-index-position-1
()
1483 (python-tests-with-temp-buffer
1485 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1486 '''print decorated function call data to stdout.
1490 @decoratorFunctionWithArguments('arg1', 'arg2')
1491 def func(a, b, c=True):
1496 print 'Inside wwrap()'
1497 def wrapped_f(*args):
1498 print 'Inside wrapped_f()'
1499 print 'Decorator arguments:', arg1, arg2, arg3
1501 print 'After f(*args)'
1505 def test(): # Some comment
1506 'This is a test function'
1524 '(("*Rescan*" . -
99)
1525 ("decoratorFunctionWithArguments" .
2)
1526 ("decoratorFunctionWithArguments.wwrap" .
224)
1527 ("decoratorFunctionWithArguments.wwrap.wrapped_f" .
273)
1536 (should (= (cdr (assoc-string (car elt
) expected
))
1537 (if (markerp (cdr elt
))
1538 (marker-position (cdr elt
))
1540 (imenu--make-index-alist)))))
1545 (ert-deftest python-info-current-defun-1
()
1546 (python-tests-with-temp-buffer
1551 (should (string= "foo" (python-info-current-defun)))
1552 (should (string= "def foo" (python-info-current-defun t)))
1554 (should (not (python-info-current-defun)))
1555 (indent-for-tab-command)
1556 (should (string= "foo" (python-info-current-defun)))
1557 (should (string= "def foo" (python-info-current-defun t)))))
1559 (ert-deftest python-info-current-defun-2
()
1560 (python-tests-with-temp-buffer
1566 return [i for i in range(3)]
1580 (should (string= "C" (python-info-current-defun)))
1581 (should (string= "class C" (python-info-current-defun t)))
1582 (python-tests-look-at "return [i for ")
1583 (should (string= "C.m" (python-info-current-defun)))
1584 (should (string= "def C.m" (python-info-current-defun t)))
1585 (python-tests-look-at "def b():")
1586 (should (string= "C.m.b" (python-info-current-defun)))
1587 (should (string= "def C.m.b" (python-info-current-defun t)))
1589 (indent-for-tab-command)
1590 (python-indent-dedent-line-backspace 1)
1591 (should (string= "C.m" (python-info-current-defun)))
1592 (should (string= "def C.m" (python-info-current-defun t)))
1593 (python-tests-look-at "def c(self):")
1595 (indent-for-tab-command)
1596 (should (string= "C.m.a" (python-info-current-defun)))
1597 (should (string= "def C.m.a" (python-info-current-defun t)))
1598 (python-indent-dedent-line-backspace 1)
1599 (should (string= "C.m" (python-info-current-defun)))
1600 (should (string= "def C.m" (python-info-current-defun t)))
1601 (python-indent-dedent-line-backspace 1)
1602 (should (string= "C" (python-info-current-defun)))
1603 (should (string= "class C" (python-info-current-defun t)))
1604 (python-tests-look-at "def c(self):")
1605 (should (string= "C.c" (python-info-current-defun)))
1606 (should (string= "def C.c" (python-info-current-defun t)))
1607 (python-tests-look-at "do_c()")
1608 (should (string= "C.c" (python-info-current-defun)))
1609 (should (string= "def C.c" (python-info-current-defun t)))))
1611 (ert-deftest python-info-current-defun-3
()
1612 (python-tests-with-temp-buffer
1614 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1615 '''print decorated function call data to stdout.
1619 @decoratorFunctionWithArguments('arg1', 'arg2')
1620 def func(a, b, c=True):
1625 print 'Inside wwrap()'
1626 def wrapped_f(*args):
1627 print 'Inside wrapped_f()'
1628 print 'Decorator arguments:', arg1, arg2, arg3
1630 print 'After f(*args)'
1634 (python-tests-look-at "def wwrap(f):")
1636 (should (not (python-info-current-defun)))
1637 (indent-for-tab-command 1)
1638 (should (string= (python-info-current-defun)
1639 "decoratorFunctionWithArguments"))
1640 (should (string= (python-info-current-defun t)
1641 "def decoratorFunctionWithArguments"))
1642 (python-tests-look-at "def wrapped_f(*args):")
1643 (should (string= (python-info-current-defun)
1644 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
1645 (should (string= (python-info-current-defun t)
1646 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
1647 (python-tests-look-at "return wrapped_f")
1648 (should (string= (python-info-current-defun)
1649 "decoratorFunctionWithArguments.wwrap"))
1650 (should (string= (python-info-current-defun t)
1651 "def decoratorFunctionWithArguments.wwrap"))
1653 (python-tests-look-at "return wwrap")
1654 (should (string= (python-info-current-defun)
1655 "decoratorFunctionWithArguments"))
1656 (should (string= (python-info-current-defun t)
1657 "def decoratorFunctionWithArguments"))))
1659 (ert-deftest python-info-current-symbol-1
()
1660 (python-tests-with-temp-buffer
1670 (python-tests-look-at "self.c()")
1671 (should (string= "self.c" (python-info-current-symbol)))
1672 (should (string= "C.c" (python-info-current-symbol t
)))))
1674 (ert-deftest python-info-current-symbol-2
()
1675 (python-tests-with-temp-buffer
1687 (python-tests-look-at "self.c()")
1688 (should (string= "self.c" (python-info-current-symbol)))
1689 (should (string= "C.M.c" (python-info-current-symbol t
)))))
1691 (ert-deftest python-info-current-symbol-3
()
1692 "Keywords should not be considered symbols."
1693 :expected-result
:failed
1694 (python-tests-with-temp-buffer
1699 ;; FIXME: keywords are not symbols.
1700 (python-tests-look-at "class C")
1701 (should (not (python-info-current-symbol)))
1702 (should (not (python-info-current-symbol t
)))
1703 (python-tests-look-at "C(object)")
1704 (should (string= "C" (python-info-current-symbol)))
1705 (should (string= "class C" (python-info-current-symbol t
)))))
1707 (ert-deftest python-info-statement-starts-block-p-1
()
1708 (python-tests-with-temp-buffer
1710 def long_function_name(
1711 var_one, var_two, var_three,
1715 (python-tests-look-at "def long_function_name")
1716 (should (python-info-statement-starts-block-p))
1717 (python-tests-look-at "print (var_one)")
1718 (python-util-forward-comment -
1)
1719 (should (python-info-statement-starts-block-p))))
1721 (ert-deftest python-info-statement-starts-block-p-2
()
1722 (python-tests-with-temp-buffer
1724 if width == 0 and height == 0 and \\\\
1725 color == 'red' and emphasis == 'strong' or \\\\
1727 raise ValueError('sorry, you lose')
1729 (python-tests-look-at "if width == 0 and")
1730 (should (python-info-statement-starts-block-p))
1731 (python-tests-look-at "raise ValueError(")
1732 (python-util-forward-comment -
1)
1733 (should (python-info-statement-starts-block-p))))
1735 (ert-deftest python-info-statement-ends-block-p-1
()
1736 (python-tests-with-temp-buffer
1738 def long_function_name(
1739 var_one, var_two, var_three,
1743 (python-tests-look-at "print (var_one)")
1744 (should (python-info-statement-ends-block-p))))
1746 (ert-deftest python-info-statement-ends-block-p-2
()
1747 (python-tests-with-temp-buffer
1749 if width == 0 and height == 0 and \\\\
1750 color == 'red' and emphasis == 'strong' or \\\\
1757 (python-tests-look-at "raise ValueError(")
1758 (should (python-info-statement-ends-block-p))))
1760 (ert-deftest python-info-beginning-of-statement-p-1
()
1761 (python-tests-with-temp-buffer
1763 def long_function_name(
1764 var_one, var_two, var_three,
1768 (python-tests-look-at "def long_function_name")
1769 (should (python-info-beginning-of-statement-p))
1771 (should (not (python-info-beginning-of-statement-p)))
1772 (python-tests-look-at "print (var_one)")
1773 (should (python-info-beginning-of-statement-p))
1774 (goto-char (line-beginning-position))
1775 (should (not (python-info-beginning-of-statement-p)))))
1777 (ert-deftest python-info-beginning-of-statement-p-2
()
1778 (python-tests-with-temp-buffer
1780 if width == 0 and height == 0 and \\\\
1781 color == 'red' and emphasis == 'strong' or \\\\
1788 (python-tests-look-at "if width == 0 and")
1789 (should (python-info-beginning-of-statement-p))
1791 (should (not (python-info-beginning-of-statement-p)))
1792 (python-tests-look-at "raise ValueError(")
1793 (should (python-info-beginning-of-statement-p))
1794 (goto-char (line-beginning-position))
1795 (should (not (python-info-beginning-of-statement-p)))))
1797 (ert-deftest python-info-end-of-statement-p-1
()
1798 (python-tests-with-temp-buffer
1800 def long_function_name(
1801 var_one, var_two, var_three,
1805 (python-tests-look-at "def long_function_name")
1806 (should (not (python-info-end-of-statement-p)))
1808 (should (not (python-info-end-of-statement-p)))
1809 (python-tests-look-at "print (var_one)")
1810 (python-util-forward-comment -
1)
1811 (should (python-info-end-of-statement-p))
1812 (python-tests-look-at "print (var_one)")
1813 (should (not (python-info-end-of-statement-p)))
1815 (should (python-info-end-of-statement-p))))
1817 (ert-deftest python-info-end-of-statement-p-2
()
1818 (python-tests-with-temp-buffer
1820 if width == 0 and height == 0 and \\\\
1821 color == 'red' and emphasis == 'strong' or \\\\
1828 (python-tests-look-at "if width == 0 and")
1829 (should (not (python-info-end-of-statement-p)))
1831 (should (not (python-info-end-of-statement-p)))
1832 (python-tests-look-at "raise ValueError(")
1833 (python-util-forward-comment -
1)
1834 (should (python-info-end-of-statement-p))
1835 (python-tests-look-at "raise ValueError(")
1836 (should (not (python-info-end-of-statement-p)))
1838 (should (not (python-info-end-of-statement-p)))
1839 (goto-char (point-max))
1840 (python-util-forward-comment -
1)
1841 (should (python-info-end-of-statement-p))))
1843 (ert-deftest python-info-beginning-of-block-p-1
()
1844 (python-tests-with-temp-buffer
1846 def long_function_name(
1847 var_one, var_two, var_three,
1851 (python-tests-look-at "def long_function_name")
1852 (should (python-info-beginning-of-block-p))
1853 (python-tests-look-at "var_one, var_two, var_three,")
1854 (should (not (python-info-beginning-of-block-p)))
1855 (python-tests-look-at "print (var_one)")
1856 (should (not (python-info-beginning-of-block-p)))))
1858 (ert-deftest python-info-beginning-of-block-p-2
()
1859 (python-tests-with-temp-buffer
1861 if width == 0 and height == 0 and \\\\
1862 color == 'red' and emphasis == 'strong' or \\\\
1869 (python-tests-look-at "if width == 0 and")
1870 (should (python-info-beginning-of-block-p))
1871 (python-tests-look-at "color == 'red' and emphasis")
1872 (should (not (python-info-beginning-of-block-p)))
1873 (python-tests-look-at "raise ValueError(")
1874 (should (not (python-info-beginning-of-block-p)))))
1876 (ert-deftest python-info-end-of-block-p-1
()
1877 (python-tests-with-temp-buffer
1879 def long_function_name(
1880 var_one, var_two, var_three,
1884 (python-tests-look-at "def long_function_name")
1885 (should (not (python-info-end-of-block-p)))
1886 (python-tests-look-at "var_one, var_two, var_three,")
1887 (should (not (python-info-end-of-block-p)))
1888 (python-tests-look-at "var_four):")
1890 (should (not (python-info-end-of-block-p)))
1891 (python-tests-look-at "print (var_one)")
1892 (should (not (python-info-end-of-block-p)))
1894 (should (python-info-end-of-block-p))))
1896 (ert-deftest python-info-end-of-block-p-2
()
1897 (python-tests-with-temp-buffer
1899 if width == 0 and height == 0 and \\\\
1900 color == 'red' and emphasis == 'strong' or \\\\
1907 (python-tests-look-at "if width == 0 and")
1908 (should (not (python-info-end-of-block-p)))
1909 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
1910 (should (not (python-info-end-of-block-p)))
1911 (python-tests-look-at "highlight > 100:")
1913 (should (not (python-info-end-of-block-p)))
1914 (python-tests-look-at "raise ValueError(")
1915 (should (not (python-info-end-of-block-p)))
1917 (should (not (python-info-end-of-block-p)))
1918 (goto-char (point-max))
1919 (python-util-forward-comment -
1)
1920 (should (python-info-end-of-block-p))))
1922 (ert-deftest python-info-closing-block-1
()
1923 (python-tests-with-temp-buffer
1925 if request.user.is_authenticated():
1927 profile = request.user.get_profile()
1928 except Profile.DoesNotExist:
1929 profile = Profile.objects.create(user=request.user)
1932 profile.recalculate_stats()
1934 profile.clear_stats()
1939 (python-tests-look-at "try:")
1940 (should (not (python-info-closing-block)))
1941 (python-tests-look-at "except Profile.DoesNotExist:")
1942 (should (= (python-tests-look-at "try:" -
1 t
)
1943 (python-info-closing-block)))
1944 (python-tests-look-at "else:")
1945 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -
1 t
)
1946 (python-info-closing-block)))
1947 (python-tests-look-at "if profile.stats:")
1948 (should (not (python-info-closing-block)))
1949 (python-tests-look-at "else:")
1950 (should (= (python-tests-look-at "if profile.stats:" -
1 t
)
1951 (python-info-closing-block)))
1952 (python-tests-look-at "finally:")
1953 (should (= (python-tests-look-at "else:" -
2 t
)
1954 (python-info-closing-block)))))
1956 (ert-deftest python-info-closing-block-2
()
1957 (python-tests-with-temp-buffer
1959 if request.user.is_authenticated():
1960 profile = Profile.objects.get_or_create(user=request.user)
1962 profile.recalculate_stats()
1969 (python-tests-look-at "'else': 'do it'")
1970 (should (not (python-info-closing-block)))
1971 (python-tests-look-at "'else'")
1972 (should (not (python-info-closing-block)))))
1974 (ert-deftest python-info-line-ends-backslash-p-1
()
1975 (python-tests-with-temp-buffer
1977 objects = Thing.objects.all() \\\\
1987 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
1988 (should (python-info-line-ends-backslash-p 3))
1989 (should (python-info-line-ends-backslash-p 4))
1990 (should (python-info-line-ends-backslash-p 5))
1991 (should (python-info-line-ends-backslash-p 6)) ; ) \...
1992 (should (python-info-line-ends-backslash-p 7))
1993 (should (python-info-line-ends-backslash-p 8))
1994 (should (python-info-line-ends-backslash-p 9))
1995 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
1997 (ert-deftest python-info-beginning-of-backslash-1
()
1998 (python-tests-with-temp-buffer
2000 objects = Thing.objects.all() \\\\
2011 (second (python-tests-look-at ".filter("))
2012 (third (python-tests-look-at ".aggregate(")))
2013 (should (= first
(python-info-beginning-of-backslash 2)))
2014 (should (= second
(python-info-beginning-of-backslash 3)))
2015 (should (= second
(python-info-beginning-of-backslash 4)))
2016 (should (= second
(python-info-beginning-of-backslash 5)))
2017 (should (= second
(python-info-beginning-of-backslash 6)))
2018 (should (= third
(python-info-beginning-of-backslash 7)))
2019 (should (= third
(python-info-beginning-of-backslash 8)))
2020 (should (= third
(python-info-beginning-of-backslash 9)))
2021 (should (not (python-info-beginning-of-backslash 10))))))
2023 (ert-deftest python-info-continuation-line-p-1
()
2024 (python-tests-with-temp-buffer
2026 if width == 0 and height == 0 and \\\\
2027 color == 'red' and emphasis == 'strong' or \\\\
2034 (python-tests-look-at "if width == 0 and height == 0 and")
2035 (should (not (python-info-continuation-line-p)))
2036 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2037 (should (python-info-continuation-line-p))
2038 (python-tests-look-at "highlight > 100:")
2039 (should (python-info-continuation-line-p))
2040 (python-tests-look-at "raise ValueError(")
2041 (should (not (python-info-continuation-line-p)))
2042 (python-tests-look-at "'sorry, you lose'")
2043 (should (python-info-continuation-line-p))
2045 (should (python-info-continuation-line-p))
2046 (python-tests-look-at ")")
2047 (should (python-info-continuation-line-p))
2049 (should (not (python-info-continuation-line-p)))))
2051 (ert-deftest python-info-block-continuation-line-p-1
()
2052 (python-tests-with-temp-buffer
2054 if width == 0 and height == 0 and \\\\
2055 color == 'red' and emphasis == 'strong' or \\\\
2062 (python-tests-look-at "if width == 0 and")
2063 (should (not (python-info-block-continuation-line-p)))
2064 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2065 (should (= (python-info-block-continuation-line-p)
2066 (python-tests-look-at "if width == 0 and" -
1 t
)))
2067 (python-tests-look-at "highlight > 100:")
2068 (should (not (python-info-block-continuation-line-p)))))
2070 (ert-deftest python-info-block-continuation-line-p-2
()
2071 (python-tests-with-temp-buffer
2078 (python-tests-look-at "def foo(a,")
2079 (should (not (python-info-block-continuation-line-p)))
2080 (python-tests-look-at "b,")
2081 (should (= (python-info-block-continuation-line-p)
2082 (python-tests-look-at "def foo(a," -
1 t
)))
2083 (python-tests-look-at "c):")
2084 (should (not (python-info-block-continuation-line-p)))))
2086 (ert-deftest python-info-assignment-continuation-line-p-1
()
2087 (python-tests-with-temp-buffer
2089 data = foo(), bar() \\\\
2093 (python-tests-look-at "data = foo(), bar()")
2094 (should (not (python-info-assignment-continuation-line-p)))
2095 (python-tests-look-at "baz(), 4")
2096 (should (= (python-info-assignment-continuation-line-p)
2097 (python-tests-look-at "foo()," -
1 t
)))
2098 (python-tests-look-at "5, 6")
2099 (should (not (python-info-assignment-continuation-line-p)))))
2101 (ert-deftest python-info-assignment-continuation-line-p-2
()
2102 (python-tests-with-temp-buffer
2104 data = (foo(), bar()
2108 (python-tests-look-at "data = (foo(), bar()")
2109 (should (not (python-info-assignment-continuation-line-p)))
2110 (python-tests-look-at "baz(), 4")
2111 (should (= (python-info-assignment-continuation-line-p)
2112 (python-tests-look-at "(foo()," -
1 t
)))
2113 (python-tests-look-at "5, 6)")
2114 (should (not (python-info-assignment-continuation-line-p)))))
2116 (ert-deftest python-info-looking-at-beginning-of-defun-1
()
2117 (python-tests-with-temp-buffer
2119 def decorat0r(deff):
2130 (python-tests-look-at "def decorat0r(deff):")
2131 (should (python-info-looking-at-beginning-of-defun))
2132 (python-tests-look-at "def foo(arg):")
2133 (should (not (python-info-looking-at-beginning-of-defun)))
2134 (python-tests-look-at "def wrap():")
2135 (should (python-info-looking-at-beginning-of-defun))
2136 (python-tests-look-at "deff()")
2137 (should (not (python-info-looking-at-beginning-of-defun)))))
2139 (ert-deftest python-info-current-line-comment-p-1
()
2140 (python-tests-with-temp-buffer
2143 foo = True # another comment
2147 print ('bar') # print bar
2149 (python-tests-look-at "# this is a comment")
2150 (should (python-info-current-line-comment-p))
2151 (python-tests-look-at "foo = True # another comment")
2152 (should (not (python-info-current-line-comment-p)))
2153 (python-tests-look-at "'#this is a string'")
2154 (should (not (python-info-current-line-comment-p)))
2155 (python-tests-look-at "# more comments")
2156 (should (python-info-current-line-comment-p))
2157 (python-tests-look-at "print ('bar') # print bar")
2158 (should (not (python-info-current-line-comment-p)))))
2160 (ert-deftest python-info-current-line-empty-p
()
2161 (python-tests-with-temp-buffer
2165 foo = True # another comment
2167 (should (python-info-current-line-empty-p))
2168 (python-tests-look-at "# this is a comment")
2169 (should (not (python-info-current-line-empty-p)))
2171 (should (python-info-current-line-empty-p))))
2174 ;;; Utility functions
2176 (ert-deftest python-util-goto-line-1
()
2177 (python-tests-with-temp-buffer
2182 pass" (make-string 20 ?
\n))
2183 (python-util-goto-line 10)
2184 (should (= (line-number-at-pos) 10))
2185 (python-util-goto-line 20)
2186 (should (= (line-number-at-pos) 20))))
2188 (ert-deftest python-util-clone-local-variables-1
()
2189 (let ((buffer (generate-new-buffer
2190 "python-util-clone-local-variables-1"))
2192 '((python-fill-docstring-style . django
)
2193 (python-shell-interpreter .
"python")
2194 (python-shell-interpreter-args .
"manage.py shell")
2195 (python-shell-prompt-regexp .
"In \\[[0-9]+\\]: ")
2196 (python-shell-prompt-output-regexp .
"Out\\[[0-9]+\\]: ")
2197 (python-shell-extra-pythonpaths "/home/user/pylib/")
2198 (python-shell-completion-setup-code
2199 .
"from IPython.core.completerlib import module_completion")
2200 (python-shell-completion-module-string-code
2201 .
"';'.join(module_completion('''%s'''))\n")
2202 (python-shell-completion-string-code
2203 .
"';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
2204 (python-shell-virtualenv-path
2205 .
"/home/user/.virtualenvs/project"))))
2206 (with-current-buffer buffer
2207 (kill-all-local-variables)
2208 (dolist (ccons varcons
)
2209 (set (make-local-variable (car ccons
)) (cdr ccons
))))
2210 (python-tests-with-temp-buffer
2212 (python-util-clone-local-variables buffer
)
2213 (dolist (ccons varcons
)
2215 (equal (symbol-value (car ccons
)) (cdr ccons
)))))
2216 (kill-buffer buffer
)))
2218 (ert-deftest python-util-forward-comment-1
()
2219 (python-tests-with-temp-buffer
2223 # bad indented comment
2224 # more comments" (make-string 9999 ?
\n))
2225 (python-util-forward-comment 1)
2226 (should (= (point) (point-max)))
2227 (python-util-forward-comment -
1)
2228 (should (= (point) (point-min)))))
2231 (provide 'python-tests
)
2235 ;; indent-tabs-mode: nil
2238 ;;; python-tests.el ends here