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))))
450 (ert-deftest python-nav-beginning-of-defun-1
()
451 (python-tests-with-temp-buffer
453 def decoratorFunctionWithArguments(arg1, arg2, arg3):
454 '''print decorated function call data to stdout.
458 @decoratorFunctionWithArguments('arg1', 'arg2')
459 def func(a, b, c=True):
464 print 'Inside wwrap()'
465 def wrapped_f(*args):
466 print 'Inside wrapped_f()'
467 print 'Decorator arguments:', arg1, arg2, arg3
469 print 'After f(*args)'
473 (python-tests-look-at "return wrap")
474 (should (= (save-excursion
475 (python-nav-beginning-of-defun)
478 (python-tests-look-at "def wrapped_f(*args):" -
1)
481 (python-tests-look-at "def wrapped_f(*args):" -
1)
482 (should (= (save-excursion
483 (python-nav-beginning-of-defun)
486 (python-tests-look-at "def wwrap(f):" -
1)
489 (python-tests-look-at "def wwrap(f):" -
1)
490 (should (= (save-excursion
491 (python-nav-beginning-of-defun)
494 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)
498 (ert-deftest python-nav-beginning-of-defun-2
()
499 (python-tests-with-temp-buffer
515 ;; Nested defuns, are handled with care.
516 (python-tests-look-at "def c(self):")
517 (should (= (save-excursion
518 (python-nav-beginning-of-defun)
521 (python-tests-look-at "def m(self):" -
1)
524 ;; Defuns on same levels should be respected.
525 (python-tests-look-at "def a():" -
1)
526 (should (= (save-excursion
527 (python-nav-beginning-of-defun)
530 (python-tests-look-at "def b():" -
1)
533 ;; Jump to a top level defun.
534 (python-tests-look-at "def b():" -
1)
535 (should (= (save-excursion
536 (python-nav-beginning-of-defun)
539 (python-tests-look-at "def m(self):" -
1)
542 ;; Jump to a top level defun again.
543 (python-tests-look-at "def m(self):" -
1)
544 (should (= (save-excursion
545 (python-nav-beginning-of-defun)
548 (python-tests-look-at "class C(object):" -
1)
552 (ert-deftest python-nav-end-of-defun-1
()
553 (python-tests-with-temp-buffer
569 (should (= (save-excursion
570 (python-tests-look-at "class C(object):")
571 (python-nav-end-of-defun)
575 (should (= (save-excursion
576 (python-tests-look-at "def m(self):")
577 (python-nav-end-of-defun)
580 (python-tests-look-at "def c(self):")
583 (should (= (save-excursion
584 (python-tests-look-at "def b():")
585 (python-nav-end-of-defun)
588 (python-tests-look-at "def b():")
591 (should (= (save-excursion
592 (python-tests-look-at "def c(self):")
593 (python-nav-end-of-defun)
598 (ert-deftest python-nav-end-of-defun-2
()
599 (python-tests-with-temp-buffer
601 def decoratorFunctionWithArguments(arg1, arg2, arg3):
602 '''print decorated function call data to stdout.
606 @decoratorFunctionWithArguments('arg1', 'arg2')
607 def func(a, b, c=True):
612 print 'Inside wwrap()'
613 def wrapped_f(*args):
614 print 'Inside wrapped_f()'
615 print 'Decorator arguments:', arg1, arg2, arg3
617 print 'After f(*args)'
621 (should (= (save-excursion
622 (python-tests-look-at "def decoratorFunctionWithArguments")
623 (python-nav-end-of-defun)
627 (should (= (save-excursion
628 (python-tests-look-at "@decoratorFunctionWithArguments")
629 (python-nav-end-of-defun)
633 (should (= (save-excursion
634 (python-tests-look-at "def wwrap(f):")
635 (python-nav-end-of-defun)
638 (python-tests-look-at "return wwrap")
639 (line-beginning-position))))
640 (should (= (save-excursion
641 (python-tests-look-at "def wrapped_f(*args):")
642 (python-nav-end-of-defun)
645 (python-tests-look-at "return wrapped_f")
646 (line-beginning-position))))
647 (should (= (save-excursion
648 (python-tests-look-at "f(*args)")
649 (python-nav-end-of-defun)
652 (python-tests-look-at "return wrapped_f")
653 (line-beginning-position))))))
656 (ert-deftest python-nav-beginning-of-statement-1
()
657 (python-tests-with-temp-buffer
667 v3 = ('this is a string'
672 # this is a comment, yo
673 'continue previous line')
679 (python-tests-look-at "v2 =")
680 (python-util-forward-comment -
1)
681 (should (= (save-excursion
682 (python-nav-beginning-of-statement)
684 (python-tests-look-at "v1 =" -
1 t
)))
685 (python-tests-look-at "v3 =")
686 (python-util-forward-comment -
1)
687 (should (= (save-excursion
688 (python-nav-beginning-of-statement)
690 (python-tests-look-at "v2 =" -
1 t
)))
691 (python-tests-look-at "v4 =")
692 (python-util-forward-comment -
1)
693 (should (= (save-excursion
694 (python-nav-beginning-of-statement)
696 (python-tests-look-at "v3 =" -
1 t
)))
697 (goto-char (point-max))
698 (python-util-forward-comment -
1)
699 (should (= (save-excursion
700 (python-nav-beginning-of-statement)
702 (python-tests-look-at "v4 =" -
1 t
)))))
704 (ert-deftest python-nav-end-of-statement-1
()
705 (python-tests-with-temp-buffer
715 v3 = ('this is a string'
720 # this is a comment, yo
721 'continue previous line')
727 (python-tests-look-at "v1 =")
728 (should (= (save-excursion
729 (python-nav-end-of-statement)
732 (python-tests-look-at "789")
733 (line-end-position))))
734 (python-tests-look-at "v2 =")
735 (should (= (save-excursion
736 (python-nav-end-of-statement)
739 (python-tests-look-at "value4)")
740 (line-end-position))))
741 (python-tests-look-at "v3 =")
742 (should (= (save-excursion
743 (python-nav-end-of-statement)
746 (python-tests-look-at
747 "'continue previous line')")
748 (line-end-position))))
749 (python-tests-look-at "v4 =")
750 (should (= (save-excursion
751 (python-nav-end-of-statement)
754 (goto-char (point-max))
755 (python-util-forward-comment -
1)
758 (ert-deftest python-nav-forward-statement-1
()
759 (python-tests-with-temp-buffer
769 v3 = ('this is a string'
774 # this is a comment, yo
775 'continue previous line')
781 (python-tests-look-at "v1 =")
782 (should (= (save-excursion
783 (python-nav-forward-statement)
785 (python-tests-look-at "v2 =")))
786 (should (= (save-excursion
787 (python-nav-forward-statement)
789 (python-tests-look-at "v3 =")))
790 (should (= (save-excursion
791 (python-nav-forward-statement)
793 (python-tests-look-at "v4 =")))
794 (should (= (save-excursion
795 (python-nav-forward-statement)
799 (ert-deftest python-nav-backward-statement-1
()
800 (python-tests-with-temp-buffer
810 v3 = ('this is a string'
815 # this is a comment, yo
816 'continue previous line')
822 (goto-char (point-max))
823 (should (= (save-excursion
824 (python-nav-backward-statement)
826 (python-tests-look-at "v4 =" -
1)))
827 (should (= (save-excursion
828 (python-nav-backward-statement)
830 (python-tests-look-at "v3 =" -
1)))
831 (should (= (save-excursion
832 (python-nav-backward-statement)
834 (python-tests-look-at "v2 =" -
1)))
835 (should (= (save-excursion
836 (python-nav-backward-statement)
838 (python-tests-look-at "v1 =" -
1)))))
840 (ert-deftest python-nav-backward-statement-2
()
841 :expected-result
:failed
842 (python-tests-with-temp-buffer
853 ;; FIXME: For some reason `python-nav-backward-statement' is moving
854 ;; back two sentences when starting from 'value4)'.
855 (goto-char (point-max))
856 (python-util-forward-comment -
1)
857 (should (= (save-excursion
858 (python-nav-backward-statement)
860 (python-tests-look-at "v2 =" -
1 t
)))))
862 (ert-deftest python-nav-beginning-of-block-1
()
863 (python-tests-with-temp-buffer
865 def decoratorFunctionWithArguments(arg1, arg2, arg3):
866 '''print decorated function call data to stdout.
870 @decoratorFunctionWithArguments('arg1', 'arg2')
871 def func(a, b, c=True):
876 print 'Inside wwrap()'
877 def wrapped_f(*args):
878 print 'Inside wrapped_f()'
879 print 'Decorator arguments:', arg1, arg2, arg3
881 print 'After f(*args)'
885 (python-tests-look-at "return wwrap")
886 (should (= (save-excursion
887 (python-nav-beginning-of-block)
889 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)))
890 (python-tests-look-at "print 'Inside wwrap()'")
891 (should (= (save-excursion
892 (python-nav-beginning-of-block)
894 (python-tests-look-at "def wwrap(f):" -
1)))
895 (python-tests-look-at "print 'After f(*args)'")
897 (should (= (save-excursion
898 (python-nav-beginning-of-block)
900 (python-tests-look-at "def wrapped_f(*args):" -
1)))
901 (python-tests-look-at "return wrapped_f")
902 (should (= (save-excursion
903 (python-nav-beginning-of-block)
905 (python-tests-look-at "def wwrap(f):" -
1)))))
907 (ert-deftest python-nav-end-of-block-1
()
908 (python-tests-with-temp-buffer
910 def decoratorFunctionWithArguments(arg1, arg2, arg3):
911 '''print decorated function call data to stdout.
915 @decoratorFunctionWithArguments('arg1', 'arg2')
916 def func(a, b, c=True):
921 print 'Inside wwrap()'
922 def wrapped_f(*args):
923 print 'Inside wrapped_f()'
924 print 'Decorator arguments:', arg1, arg2, arg3
926 print 'After f(*args)'
930 (python-tests-look-at "def decoratorFunctionWithArguments")
931 (should (= (save-excursion
932 (python-nav-end-of-block)
935 (goto-char (point-max))
936 (python-util-forward-comment -
1)
938 (python-tests-look-at "def wwrap(f):")
939 (should (= (save-excursion
940 (python-nav-end-of-block)
943 (python-tests-look-at "return wrapped_f")
944 (line-end-position))))
946 (should (= (save-excursion
947 (python-nav-end-of-block)
950 (python-tests-look-at "return wrapped_f")
951 (line-end-position))))
952 (python-tests-look-at "f(*args)")
953 (should (= (save-excursion
954 (python-nav-end-of-block)
957 (python-tests-look-at "print 'After f(*args)'")
958 (line-end-position))))))
960 (ert-deftest python-nav-forward-block-1
()
961 "This also accounts as a test for `python-nav-backward-block'."
962 (python-tests-with-temp-buffer
964 if request.user.is_authenticated():
968 profile = request.user.get_profile()
969 except Profile.DoesNotExist:
970 profile = Profile.objects.create(user=request.user)
973 profile.recalculate_stats()
975 profile.clear_stats()
980 (should (= (save-excursion (python-nav-forward-block))
981 (python-tests-look-at "if request.user.is_authenticated():")))
982 (should (= (save-excursion (python-nav-forward-block))
983 (python-tests-look-at "try:")))
984 (should (= (save-excursion (python-nav-forward-block))
985 (python-tests-look-at "except Profile.DoesNotExist:")))
986 (should (= (save-excursion (python-nav-forward-block))
987 (python-tests-look-at "else:")))
988 (should (= (save-excursion (python-nav-forward-block))
989 (python-tests-look-at "if profile.stats:")))
990 (should (= (save-excursion (python-nav-forward-block))
991 (python-tests-look-at "else:")))
992 (should (= (save-excursion (python-nav-forward-block))
993 (python-tests-look-at "finally:")))
994 ;; When point is at the last block, leave it there and return nil
995 (should (not (save-excursion (python-nav-forward-block))))
996 ;; Move backwards, and even if the number of moves is less than the
997 ;; provided argument return the point.
998 (should (= (save-excursion (python-nav-forward-block -
10))
999 (python-tests-look-at
1000 "if request.user.is_authenticated():" -
1)))))
1002 (ert-deftest python-nav-lisp-forward-sexp-safe-1
()
1003 (python-tests-with-temp-buffer
1005 profile = Profile.objects.create(user=request.user)
1008 (python-tests-look-at "profile =")
1009 (python-nav-lisp-forward-sexp-safe 4)
1010 (should (looking-at "(user=request.user)"))
1011 (python-tests-look-at "user=request.user")
1012 (python-nav-lisp-forward-sexp-safe -
1)
1013 (should (looking-at "(user=request.user)"))
1014 (python-nav-lisp-forward-sexp-safe -
4)
1015 (should (looking-at "profile ="))
1016 (python-tests-look-at "user=request.user")
1017 (python-nav-lisp-forward-sexp-safe 3)
1018 (should (looking-at ")"))
1019 (python-nav-lisp-forward-sexp-safe 1)
1020 (should (looking-at "$"))
1021 (python-nav-lisp-forward-sexp-safe 1)
1022 (should (looking-at ".notify()"))))
1024 (ert-deftest python-nav-forward-sexp-1
()
1025 (python-tests-with-temp-buffer
1031 (python-tests-look-at "a()")
1032 (python-nav-forward-sexp)
1033 (should (looking-at "$"))
1034 (should (save-excursion
1036 (looking-at "a()")))
1037 (python-nav-forward-sexp)
1038 (should (looking-at "$"))
1039 (should (save-excursion
1041 (looking-at "b()")))
1042 (python-nav-forward-sexp)
1043 (should (looking-at "$"))
1044 (should (save-excursion
1046 (looking-at "c()")))
1047 ;; Movement next to a paren should do what lisp does and
1048 ;; unfortunately It can't change, because otherwise
1049 ;; `blink-matching-open' breaks.
1050 (python-nav-forward-sexp -
1)
1051 (should (looking-at "()"))
1052 (should (save-excursion
1054 (looking-at "c()")))
1055 (python-nav-forward-sexp -
1)
1056 (should (looking-at "c()"))
1057 (python-nav-forward-sexp -
1)
1058 (should (looking-at "b()"))
1059 (python-nav-forward-sexp -
1)
1060 (should (looking-at "a()"))))
1062 (ert-deftest python-nav-forward-sexp-2
()
1063 (python-tests-with-temp-buffer
1072 (python-tests-look-at "aa =")
1073 (python-nav-forward-sexp)
1074 (should (looking-at " = bbb"))
1075 (python-nav-forward-sexp)
1076 (should (looking-at "$"))
1077 (should (save-excursion
1078 (back-to-indentation)
1079 (looking-at "aaa = bbb")))
1080 (python-nav-forward-sexp)
1081 (should (looking-at "$"))
1082 (should (save-excursion
1083 (back-to-indentation)
1084 (looking-at "ccc = ddd")))
1085 (python-nav-forward-sexp)
1086 (should (looking-at "$"))
1087 (should (save-excursion
1088 (back-to-indentation)
1089 (looking-at "eee = fff")))
1090 (python-nav-forward-sexp)
1091 (should (looking-at "$"))
1092 (should (save-excursion
1093 (back-to-indentation)
1094 (looking-at "return ggg")))
1095 (python-nav-forward-sexp -
1)
1096 (should (looking-at "def func():"))))
1098 (ert-deftest python-nav-forward-sexp-3
()
1099 (python-tests-with-temp-buffer
1101 from some_module import some_sub_module
1102 from another_module import another_sub_module
1104 def another_statement():
1107 (python-tests-look-at "some_module")
1108 (python-nav-forward-sexp)
1109 (should (looking-at " import"))
1110 (python-nav-forward-sexp)
1111 (should (looking-at " some_sub_module"))
1112 (python-nav-forward-sexp)
1113 (should (looking-at "$"))
1116 (back-to-indentation)
1118 "from some_module import some_sub_module")))
1119 (python-nav-forward-sexp)
1120 (should (looking-at "$"))
1123 (back-to-indentation)
1125 "from another_module import another_sub_module")))
1126 (python-nav-forward-sexp)
1127 (should (looking-at "$"))
1130 (back-to-indentation)
1133 (python-nav-forward-sexp -
1)
1134 (should (looking-at "def another_statement():"))
1135 (python-nav-forward-sexp -
1)
1136 (should (looking-at "from another_module import another_sub_module"))
1137 (python-nav-forward-sexp -
1)
1138 (should (looking-at "from some_module import some_sub_module"))))
1140 (ert-deftest python-nav-up-list-1
()
1141 (python-tests-with-temp-buffer
1145 return [i for i in range(3)]
1147 (python-tests-look-at "3)]")
1148 (python-nav-up-list)
1149 (should (looking-at "]"))
1150 (python-nav-up-list)
1151 (should (looking-at "$"))))
1153 (ert-deftest python-nav-backward-up-list-1
()
1154 :expected-result
:failed
1155 (python-tests-with-temp-buffer
1159 return [i for i in range(3)]
1161 (python-tests-look-at "3)]")
1162 (python-nav-backward-up-list)
1163 (should (looking-at "(3)\\]"))
1164 (python-nav-backward-up-list)
1166 "\\[i for i in range(3)\\]"))
1167 ;; FIXME: Need to move to beginning-of-statement.
1168 (python-nav-backward-up-list)
1170 "return \\[i for i in range(3)\\]"))
1171 (python-nav-backward-up-list)
1172 (should (looking-at "if True:"))
1173 (python-nav-backward-up-list)
1174 (should (looking-at "def f():"))))
1177 ;;; Shell integration
1179 (defvar python-tests-shell-interpreter
"python")
1181 (ert-deftest python-shell-get-process-name-1
()
1182 "Check process name calculation on different scenarios."
1183 (python-tests-with-temp-buffer
1185 (should (string= (python-shell-get-process-name nil
)
1186 python-shell-buffer-name
))
1187 ;; When the `current-buffer' doesn't have `buffer-file-name', even
1188 ;; if dedicated flag is non-nil should not include its name.
1189 (should (string= (python-shell-get-process-name t
)
1190 python-shell-buffer-name
)))
1191 (python-tests-with-temp-file
1193 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1194 ;; should be respected.
1195 (should (string= (python-shell-get-process-name nil
)
1196 python-shell-buffer-name
))
1198 (python-shell-get-process-name t
)
1199 (format "%s[%s]" python-shell-buffer-name buffer-file-name
)))))
1201 (ert-deftest python-shell-internal-get-process-name-1
()
1202 "Check the internal process name is config-unique."
1203 (let* ((python-shell-interpreter python-tests-shell-interpreter
)
1204 (python-shell-interpreter-args "")
1205 (python-shell-prompt-regexp ">>> ")
1206 (python-shell-prompt-block-regexp "[.][.][.] ")
1207 (python-shell-setup-codes "")
1208 (python-shell-process-environment "")
1209 (python-shell-extra-pythonpaths "")
1210 (python-shell-exec-path "")
1211 (python-shell-virtualenv-path "")
1212 (expected (python-tests-with-temp-buffer
1213 "" (python-shell-internal-get-process-name))))
1214 ;; Same configurations should match.
1217 (python-tests-with-temp-buffer
1218 "" (python-shell-internal-get-process-name))))
1219 (let ((python-shell-interpreter-args "-B"))
1220 ;; A minimal change should generate different names.
1224 (python-tests-with-temp-buffer
1225 "" (python-shell-internal-get-process-name))))))))
1227 (ert-deftest python-shell-parse-command-1
()
1228 "Check the command to execute is calculated correctly.
1229 Using `python-shell-interpreter' and
1230 `python-shell-interpreter-args'."
1231 :expected-result
(if (executable-find python-tests-shell-interpreter
)
1234 (let ((python-shell-interpreter (executable-find
1235 python-tests-shell-interpreter
))
1236 (python-shell-interpreter-args "-B"))
1239 python-shell-interpreter
1240 python-shell-interpreter-args
)
1241 (python-shell-parse-command)))))
1243 (ert-deftest python-shell-calculate-process-environment-1
()
1244 "Test `python-shell-process-environment' modification."
1245 (let* ((original-process-environment process-environment
)
1246 (python-shell-process-environment
1247 '("TESTVAR1=value1" "TESTVAR2=value2"))
1248 (process-environment
1249 (python-shell-calculate-process-environment)))
1250 (should (equal (getenv "TESTVAR1") "value1"))
1251 (should (equal (getenv "TESTVAR2") "value2"))))
1253 (ert-deftest python-shell-calculate-process-environment-2
()
1254 "Test `python-shell-extra-pythonpaths' modification."
1255 (let* ((original-process-environment process-environment
)
1256 (original-pythonpath (getenv "PYTHONPATH"))
1257 (paths '("path1" "path2"))
1258 (python-shell-extra-pythonpaths paths
)
1259 (process-environment
1260 (python-shell-calculate-process-environment)))
1261 (should (equal (getenv "PYTHONPATH")
1263 (mapconcat 'identity paths path-separator
)
1264 path-separator original-pythonpath
)))))
1266 (ert-deftest python-shell-calculate-process-environment-3
()
1267 "Test `python-shell-virtualenv-path' modification."
1268 (let* ((original-process-environment process-environment
)
1269 (original-path (or (getenv "PATH") ""))
1270 (python-shell-virtualenv-path
1271 (directory-file-name user-emacs-directory
))
1272 (process-environment
1273 (python-shell-calculate-process-environment)))
1274 (should (not (getenv "PYTHONHOME")))
1275 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-path
))
1276 (should (equal (getenv "PATH")
1277 (format "%s/bin%s%s"
1278 python-shell-virtualenv-path
1279 path-separator original-path
)))))
1281 (ert-deftest python-shell-calculate-exec-path-1
()
1282 "Test `python-shell-exec-path' modification."
1283 (let* ((original-exec-path exec-path
)
1284 (python-shell-exec-path '("path1" "path2"))
1285 (exec-path (python-shell-calculate-exec-path)))
1288 (append python-shell-exec-path
1289 original-exec-path
)))))
1291 (ert-deftest python-shell-calculate-exec-path-2
()
1292 "Test `python-shell-exec-path' modification."
1293 (let* ((original-exec-path exec-path
)
1294 (python-shell-virtualenv-path
1295 (directory-file-name user-emacs-directory
))
1296 (exec-path (python-shell-calculate-exec-path)))
1300 (format "%s/bin" python-shell-virtualenv-path
)
1301 original-exec-path
))))))
1303 (ert-deftest python-shell-make-comint-1
()
1304 "Check comint creation for global shell buffer."
1305 :expected-result
(if (executable-find python-tests-shell-interpreter
)
1308 (let* ((python-shell-interpreter
1309 (executable-find python-tests-shell-interpreter
))
1310 (proc-name (python-shell-get-process-name nil
))
1312 (python-tests-with-temp-buffer
1313 "" (python-shell-make-comint
1314 (python-shell-parse-command) proc-name
)))
1315 (process (get-buffer-process shell-buffer
)))
1318 (set-process-query-on-exit-flag process nil
)
1319 (should (process-live-p process
))
1320 (with-current-buffer shell-buffer
1321 (should (eq major-mode
'inferior-python-mode
))
1322 (should (string= (buffer-name) (format "*%s*" proc-name
)))))
1323 (kill-buffer shell-buffer
))))
1325 (ert-deftest python-shell-make-comint-2
()
1326 "Check comint creation for internal 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-internal-get-process-name))
1334 (python-tests-with-temp-buffer
1335 "" (python-shell-make-comint
1336 (python-shell-parse-command) proc-name nil t
)))
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-get-process-1
()
1348 "Check dedicated shell process preference over global."
1349 :expected-result
(if (executable-find python-tests-shell-interpreter
)
1352 (python-tests-with-temp-file
1354 (let* ((python-shell-interpreter
1355 (executable-find python-tests-shell-interpreter
))
1356 (global-proc-name (python-shell-get-process-name nil
))
1357 (dedicated-proc-name (python-shell-get-process-name t
))
1358 (global-shell-buffer
1359 (python-shell-make-comint
1360 (python-shell-parse-command) global-proc-name
))
1361 (dedicated-shell-buffer
1362 (python-shell-make-comint
1363 (python-shell-parse-command) dedicated-proc-name
))
1364 (global-process (get-buffer-process global-shell-buffer
))
1365 (dedicated-process (get-buffer-process dedicated-shell-buffer
)))
1368 (set-process-query-on-exit-flag global-process nil
)
1369 (set-process-query-on-exit-flag dedicated-process nil
)
1370 ;; Prefer dedicated if global also exists.
1371 (should (equal (python-shell-get-process) dedicated-process
))
1372 (kill-buffer dedicated-shell-buffer
)
1373 ;; If there's only global, use it.
1374 (should (equal (python-shell-get-process) global-process
))
1375 (kill-buffer global-shell-buffer
)
1376 ;; No buffer available.
1377 (should (not (python-shell-get-process))))
1378 (ignore-errors (kill-buffer global-shell-buffer
))
1379 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
1381 (ert-deftest python-shell-get-or-create-process-1
()
1382 "Check shell process creation fallback."
1383 :expected-result
:failed
1384 (python-tests-with-temp-file
1386 ;; XXX: Break early until we can skip stuff. We need to mimic
1387 ;; user interaction because `python-shell-get-or-create-process'
1388 ;; asks for all arguments interactively when a shell process
1391 (let* ((python-shell-interpreter
1392 (executable-find python-tests-shell-interpreter
))
1394 (dedicated-process-name (python-shell-get-process-name t
))
1395 (dedicated-process (python-shell-get-or-create-process))
1396 (dedicated-shell-buffer (process-buffer dedicated-process
)))
1399 (set-process-query-on-exit-flag dedicated-process nil
)
1400 ;; Prefer dedicated if not buffer exist.
1401 (should (equal (process-name dedicated-process
)
1402 dedicated-process-name
))
1403 (kill-buffer dedicated-shell-buffer
)
1404 ;; No buffer available.
1405 (should (not (python-shell-get-process))))
1406 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
1408 (ert-deftest python-shell-internal-get-or-create-process-1
()
1409 "Check internal shell process creation fallback."
1410 :expected-result
(if (executable-find python-tests-shell-interpreter
)
1413 (python-tests-with-temp-file
1415 (should (not (process-live-p (python-shell-internal-get-process-name))))
1416 (let* ((python-shell-interpreter
1417 (executable-find python-tests-shell-interpreter
))
1418 (internal-process-name (python-shell-internal-get-process-name))
1419 (internal-process (python-shell-internal-get-or-create-process))
1420 (internal-shell-buffer (process-buffer internal-process
)))
1423 (set-process-query-on-exit-flag internal-process nil
)
1424 (should (equal (process-name internal-process
)
1425 internal-process-name
))
1426 (should (equal internal-process
1427 (python-shell-internal-get-or-create-process)))
1428 ;; No user buffer available.
1429 (should (not (python-shell-get-process)))
1430 (kill-buffer internal-shell-buffer
))
1431 (ignore-errors (kill-buffer internal-shell-buffer
))))))
1434 ;;; Shell completion
1437 ;;; PDB Track integration
1440 ;;; Symbol completion
1459 (ert-deftest python-imenu-prev-index-position-1
()
1461 (python-tests-with-temp-buffer
1463 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1464 '''print decorated function call data to stdout.
1468 @decoratorFunctionWithArguments('arg1', 'arg2')
1469 def func(a, b, c=True):
1474 print 'Inside wwrap()'
1475 def wrapped_f(*args):
1476 print 'Inside wrapped_f()'
1477 print 'Decorator arguments:', arg1, arg2, arg3
1479 print 'After f(*args)'
1483 def test(): # Some comment
1484 'This is a test function'
1502 '(("*Rescan*" . -
99)
1503 ("decoratorFunctionWithArguments" .
2)
1504 ("decoratorFunctionWithArguments.wwrap" .
224)
1505 ("decoratorFunctionWithArguments.wwrap.wrapped_f" .
273)
1514 (should (= (cdr (assoc-string (car elt
) expected
))
1515 (if (markerp (cdr elt
))
1516 (marker-position (cdr elt
))
1518 (imenu--make-index-alist)))))
1523 (ert-deftest python-info-current-defun-1
()
1524 (python-tests-with-temp-buffer
1529 (should (string= "foo" (python-info-current-defun)))
1530 (should (string= "def foo" (python-info-current-defun t)))
1532 (should (not (python-info-current-defun)))
1533 (indent-for-tab-command)
1534 (should (string= "foo" (python-info-current-defun)))
1535 (should (string= "def foo" (python-info-current-defun t)))))
1537 (ert-deftest python-info-current-defun-2
()
1538 (python-tests-with-temp-buffer
1544 return [i for i in range(3)]
1558 (should (string= "C" (python-info-current-defun)))
1559 (should (string= "class C" (python-info-current-defun t)))
1560 (python-tests-look-at "return [i for ")
1561 (should (string= "C.m" (python-info-current-defun)))
1562 (should (string= "def C.m" (python-info-current-defun t)))
1563 (python-tests-look-at "def b():")
1564 (should (string= "C.m.b" (python-info-current-defun)))
1565 (should (string= "def C.m.b" (python-info-current-defun t)))
1567 (indent-for-tab-command)
1568 (python-indent-dedent-line-backspace 1)
1569 (should (string= "C.m" (python-info-current-defun)))
1570 (should (string= "def C.m" (python-info-current-defun t)))
1571 (python-tests-look-at "def c(self):")
1573 (indent-for-tab-command)
1574 (should (string= "C.m.a" (python-info-current-defun)))
1575 (should (string= "def C.m.a" (python-info-current-defun t)))
1576 (python-indent-dedent-line-backspace 1)
1577 (should (string= "C.m" (python-info-current-defun)))
1578 (should (string= "def C.m" (python-info-current-defun t)))
1579 (python-indent-dedent-line-backspace 1)
1580 (should (string= "C" (python-info-current-defun)))
1581 (should (string= "class C" (python-info-current-defun t)))
1582 (python-tests-look-at "def c(self):")
1583 (should (string= "C.c" (python-info-current-defun)))
1584 (should (string= "def C.c" (python-info-current-defun t)))
1585 (python-tests-look-at "pass")
1586 (should (string= "C.c" (python-info-current-defun)))
1587 (should (string= "def C.c" (python-info-current-defun t)))))
1589 (ert-deftest python-info-current-defun-3
()
1590 (python-tests-with-temp-buffer
1592 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1593 '''print decorated function call data to stdout.
1597 @decoratorFunctionWithArguments('arg1', 'arg2')
1598 def func(a, b, c=True):
1603 print 'Inside wwrap()'
1604 def wrapped_f(*args):
1605 print 'Inside wrapped_f()'
1606 print 'Decorator arguments:', arg1, arg2, arg3
1608 print 'After f(*args)'
1612 (python-tests-look-at "def wwrap(f):")
1614 (should (not (python-info-current-defun)))
1615 (indent-for-tab-command 1)
1616 (should (string= (python-info-current-defun)
1617 "decoratorFunctionWithArguments"))
1618 (should (string= (python-info-current-defun t)
1619 "def decoratorFunctionWithArguments"))
1620 (python-tests-look-at "def wrapped_f(*args):")
1621 (should (string= (python-info-current-defun)
1622 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
1623 (should (string= (python-info-current-defun t)
1624 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
1625 (python-tests-look-at "return wrapped_f")
1626 (should (string= (python-info-current-defun)
1627 "decoratorFunctionWithArguments.wwrap"))
1628 (should (string= (python-info-current-defun t)
1629 "def decoratorFunctionWithArguments.wwrap"))
1631 (python-tests-look-at "return wwrap")
1632 (should (string= (python-info-current-defun)
1633 "decoratorFunctionWithArguments"))
1634 (should (string= (python-info-current-defun t)
1635 "def decoratorFunctionWithArguments"))))
1637 (ert-deftest python-info-current-symbol-1
()
1638 (python-tests-with-temp-buffer
1648 (python-tests-look-at "self.c()")
1649 (should (string= "self.c" (python-info-current-symbol)))
1650 (should (string= "C.c" (python-info-current-symbol t
)))))
1652 (ert-deftest python-info-current-symbol-2
()
1653 (python-tests-with-temp-buffer
1665 (python-tests-look-at "self.c()")
1666 (should (string= "self.c" (python-info-current-symbol)))
1667 (should (string= "C.M.c" (python-info-current-symbol t
)))))
1669 (ert-deftest python-info-current-symbol-3
()
1670 "Keywords should not be considered symbols."
1671 :expected-result
:failed
1672 (python-tests-with-temp-buffer
1677 ;; FIXME: keywords are not symbols.
1678 (python-tests-look-at "class C")
1679 (should (not (python-info-current-symbol)))
1680 (should (not (python-info-current-symbol t
)))
1681 (python-tests-look-at "C(object)")
1682 (should (string= "C" (python-info-current-symbol)))
1683 (should (string= "class C" (python-info-current-symbol t
)))))
1685 (ert-deftest python-info-statement-starts-block-p-1
()
1686 (python-tests-with-temp-buffer
1688 def long_function_name(
1689 var_one, var_two, var_three,
1693 (python-tests-look-at "def long_function_name")
1694 (should (python-info-statement-starts-block-p))
1695 (python-tests-look-at "print (var_one)")
1696 (python-util-forward-comment -
1)
1697 (should (python-info-statement-starts-block-p))))
1699 (ert-deftest python-info-statement-starts-block-p-2
()
1700 (python-tests-with-temp-buffer
1702 if width == 0 and height == 0 and \\\\
1703 color == 'red' and emphasis == 'strong' or \\\\
1705 raise ValueError('sorry, you lose')
1707 (python-tests-look-at "if width == 0 and")
1708 (should (python-info-statement-starts-block-p))
1709 (python-tests-look-at "raise ValueError(")
1710 (python-util-forward-comment -
1)
1711 (should (python-info-statement-starts-block-p))))
1713 (ert-deftest python-info-statement-ends-block-p-1
()
1714 (python-tests-with-temp-buffer
1716 def long_function_name(
1717 var_one, var_two, var_three,
1721 (python-tests-look-at "print (var_one)")
1722 (should (python-info-statement-ends-block-p))))
1724 (ert-deftest python-info-statement-ends-block-p-2
()
1725 (python-tests-with-temp-buffer
1727 if width == 0 and height == 0 and \\\\
1728 color == 'red' and emphasis == 'strong' or \\\\
1735 (python-tests-look-at "raise ValueError(")
1736 (should (python-info-statement-ends-block-p))))
1738 (ert-deftest python-info-beginning-of-statement-p-1
()
1739 (python-tests-with-temp-buffer
1741 def long_function_name(
1742 var_one, var_two, var_three,
1746 (python-tests-look-at "def long_function_name")
1747 (should (python-info-beginning-of-statement-p))
1749 (should (not (python-info-beginning-of-statement-p)))
1750 (python-tests-look-at "print (var_one)")
1751 (should (python-info-beginning-of-statement-p))
1752 (goto-char (line-beginning-position))
1753 (should (not (python-info-beginning-of-statement-p)))))
1755 (ert-deftest python-info-beginning-of-statement-p-2
()
1756 (python-tests-with-temp-buffer
1758 if width == 0 and height == 0 and \\\\
1759 color == 'red' and emphasis == 'strong' or \\\\
1766 (python-tests-look-at "if width == 0 and")
1767 (should (python-info-beginning-of-statement-p))
1769 (should (not (python-info-beginning-of-statement-p)))
1770 (python-tests-look-at "raise ValueError(")
1771 (should (python-info-beginning-of-statement-p))
1772 (goto-char (line-beginning-position))
1773 (should (not (python-info-beginning-of-statement-p)))))
1775 (ert-deftest python-info-end-of-statement-p-1
()
1776 (python-tests-with-temp-buffer
1778 def long_function_name(
1779 var_one, var_two, var_three,
1783 (python-tests-look-at "def long_function_name")
1784 (should (not (python-info-end-of-statement-p)))
1786 (should (not (python-info-end-of-statement-p)))
1787 (python-tests-look-at "print (var_one)")
1788 (python-util-forward-comment -
1)
1789 (should (python-info-end-of-statement-p))
1790 (python-tests-look-at "print (var_one)")
1791 (should (not (python-info-end-of-statement-p)))
1793 (should (python-info-end-of-statement-p))))
1795 (ert-deftest python-info-end-of-statement-p-2
()
1796 (python-tests-with-temp-buffer
1798 if width == 0 and height == 0 and \\\\
1799 color == 'red' and emphasis == 'strong' or \\\\
1806 (python-tests-look-at "if width == 0 and")
1807 (should (not (python-info-end-of-statement-p)))
1809 (should (not (python-info-end-of-statement-p)))
1810 (python-tests-look-at "raise ValueError(")
1811 (python-util-forward-comment -
1)
1812 (should (python-info-end-of-statement-p))
1813 (python-tests-look-at "raise ValueError(")
1814 (should (not (python-info-end-of-statement-p)))
1816 (should (not (python-info-end-of-statement-p)))
1817 (goto-char (point-max))
1818 (python-util-forward-comment -
1)
1819 (should (python-info-end-of-statement-p))))
1821 (ert-deftest python-info-beginning-of-block-p-1
()
1822 (python-tests-with-temp-buffer
1824 def long_function_name(
1825 var_one, var_two, var_three,
1829 (python-tests-look-at "def long_function_name")
1830 (should (python-info-beginning-of-block-p))
1831 (python-tests-look-at "var_one, var_two, var_three,")
1832 (should (not (python-info-beginning-of-block-p)))
1833 (python-tests-look-at "print (var_one)")
1834 (should (not (python-info-beginning-of-block-p)))))
1836 (ert-deftest python-info-beginning-of-block-p-2
()
1837 (python-tests-with-temp-buffer
1839 if width == 0 and height == 0 and \\\\
1840 color == 'red' and emphasis == 'strong' or \\\\
1847 (python-tests-look-at "if width == 0 and")
1848 (should (python-info-beginning-of-block-p))
1849 (python-tests-look-at "color == 'red' and emphasis")
1850 (should (not (python-info-beginning-of-block-p)))
1851 (python-tests-look-at "raise ValueError(")
1852 (should (not (python-info-beginning-of-block-p)))))
1854 (ert-deftest python-info-end-of-block-p-1
()
1855 (python-tests-with-temp-buffer
1857 def long_function_name(
1858 var_one, var_two, var_three,
1862 (python-tests-look-at "def long_function_name")
1863 (should (not (python-info-end-of-block-p)))
1864 (python-tests-look-at "var_one, var_two, var_three,")
1865 (should (not (python-info-end-of-block-p)))
1866 (python-tests-look-at "var_four):")
1868 (should (not (python-info-end-of-block-p)))
1869 (python-tests-look-at "print (var_one)")
1870 (should (not (python-info-end-of-block-p)))
1872 (should (python-info-end-of-block-p))))
1874 (ert-deftest python-info-end-of-block-p-2
()
1875 (python-tests-with-temp-buffer
1877 if width == 0 and height == 0 and \\\\
1878 color == 'red' and emphasis == 'strong' or \\\\
1885 (python-tests-look-at "if width == 0 and")
1886 (should (not (python-info-end-of-block-p)))
1887 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
1888 (should (not (python-info-end-of-block-p)))
1889 (python-tests-look-at "highlight > 100:")
1891 (should (not (python-info-end-of-block-p)))
1892 (python-tests-look-at "raise ValueError(")
1893 (should (not (python-info-end-of-block-p)))
1895 (should (not (python-info-end-of-block-p)))
1896 (goto-char (point-max))
1897 (python-util-forward-comment -
1)
1898 (should (python-info-end-of-block-p))))
1900 (ert-deftest python-info-closing-block-1
()
1901 (python-tests-with-temp-buffer
1903 if request.user.is_authenticated():
1905 profile = request.user.get_profile()
1906 except Profile.DoesNotExist:
1907 profile = Profile.objects.create(user=request.user)
1910 profile.recalculate_stats()
1912 profile.clear_stats()
1917 (python-tests-look-at "try:")
1918 (should (not (python-info-closing-block)))
1919 (python-tests-look-at "except Profile.DoesNotExist:")
1920 (should (= (python-tests-look-at "try:" -
1 t
)
1921 (python-info-closing-block)))
1922 (python-tests-look-at "else:")
1923 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -
1 t
)
1924 (python-info-closing-block)))
1925 (python-tests-look-at "if profile.stats:")
1926 (should (not (python-info-closing-block)))
1927 (python-tests-look-at "else:")
1928 (should (= (python-tests-look-at "if profile.stats:" -
1 t
)
1929 (python-info-closing-block)))
1930 (python-tests-look-at "finally:")
1931 (should (= (python-tests-look-at "else:" -
2 t
)
1932 (python-info-closing-block)))))
1934 (ert-deftest python-info-closing-block-2
()
1935 (python-tests-with-temp-buffer
1937 if request.user.is_authenticated():
1938 profile = Profile.objects.get_or_create(user=request.user)
1940 profile.recalculate_stats()
1947 (python-tests-look-at "'else': 'do it'")
1948 (should (not (python-info-closing-block)))
1949 (python-tests-look-at "'else'")
1950 (should (not (python-info-closing-block)))))
1952 (ert-deftest python-info-line-ends-backslash-p-1
()
1953 (python-tests-with-temp-buffer
1955 objects = Thing.objects.all() \\\\
1965 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
1966 (should (python-info-line-ends-backslash-p 3))
1967 (should (python-info-line-ends-backslash-p 4))
1968 (should (python-info-line-ends-backslash-p 5))
1969 (should (python-info-line-ends-backslash-p 6)) ; ) \...
1970 (should (python-info-line-ends-backslash-p 7))
1971 (should (python-info-line-ends-backslash-p 8))
1972 (should (python-info-line-ends-backslash-p 9))
1973 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
1975 (ert-deftest python-info-beginning-of-backslash-1
()
1976 (python-tests-with-temp-buffer
1978 objects = Thing.objects.all() \\\\
1989 (second (python-tests-look-at ".filter("))
1990 (third (python-tests-look-at ".aggregate(")))
1991 (should (= first
(python-info-beginning-of-backslash 2)))
1992 (should (= second
(python-info-beginning-of-backslash 3)))
1993 (should (= second
(python-info-beginning-of-backslash 4)))
1994 (should (= second
(python-info-beginning-of-backslash 5)))
1995 (should (= second
(python-info-beginning-of-backslash 6)))
1996 (should (= third
(python-info-beginning-of-backslash 7)))
1997 (should (= third
(python-info-beginning-of-backslash 8)))
1998 (should (= third
(python-info-beginning-of-backslash 9)))
1999 (should (not (python-info-beginning-of-backslash 10))))))
2001 (ert-deftest python-info-continuation-line-p-1
()
2002 (python-tests-with-temp-buffer
2004 if width == 0 and height == 0 and \\\\
2005 color == 'red' and emphasis == 'strong' or \\\\
2012 (python-tests-look-at "if width == 0 and height == 0 and")
2013 (should (not (python-info-continuation-line-p)))
2014 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2015 (should (python-info-continuation-line-p))
2016 (python-tests-look-at "highlight > 100:")
2017 (should (python-info-continuation-line-p))
2018 (python-tests-look-at "raise ValueError(")
2019 (should (not (python-info-continuation-line-p)))
2020 (python-tests-look-at "'sorry, you lose'")
2021 (should (python-info-continuation-line-p))
2023 (should (python-info-continuation-line-p))
2024 (python-tests-look-at ")")
2025 (should (python-info-continuation-line-p))
2027 (should (not (python-info-continuation-line-p)))))
2029 (ert-deftest python-info-block-continuation-line-p-1
()
2030 (python-tests-with-temp-buffer
2032 if width == 0 and height == 0 and \\\\
2033 color == 'red' and emphasis == 'strong' or \\\\
2040 (python-tests-look-at "if width == 0 and")
2041 (should (not (python-info-block-continuation-line-p)))
2042 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2043 (should (= (python-info-block-continuation-line-p)
2044 (python-tests-look-at "if width == 0 and" -
1 t
)))
2045 (python-tests-look-at "highlight > 100:")
2046 (should (not (python-info-block-continuation-line-p)))))
2048 (ert-deftest python-info-block-continuation-line-p-2
()
2049 (python-tests-with-temp-buffer
2056 (python-tests-look-at "def foo(a,")
2057 (should (not (python-info-block-continuation-line-p)))
2058 (python-tests-look-at "b,")
2059 (should (= (python-info-block-continuation-line-p)
2060 (python-tests-look-at "def foo(a," -
1 t
)))
2061 (python-tests-look-at "c):")
2062 (should (not (python-info-block-continuation-line-p)))))
2064 (ert-deftest python-info-assignment-continuation-line-p-1
()
2065 (python-tests-with-temp-buffer
2067 data = foo(), bar() \\\\
2071 (python-tests-look-at "data = foo(), bar()")
2072 (should (not (python-info-assignment-continuation-line-p)))
2073 (python-tests-look-at "baz(), 4")
2074 (should (= (python-info-assignment-continuation-line-p)
2075 (python-tests-look-at "foo()," -
1 t
)))
2076 (python-tests-look-at "5, 6")
2077 (should (not (python-info-assignment-continuation-line-p)))))
2079 (ert-deftest python-info-assignment-continuation-line-p-2
()
2080 (python-tests-with-temp-buffer
2082 data = (foo(), bar()
2086 (python-tests-look-at "data = (foo(), bar()")
2087 (should (not (python-info-assignment-continuation-line-p)))
2088 (python-tests-look-at "baz(), 4")
2089 (should (= (python-info-assignment-continuation-line-p)
2090 (python-tests-look-at "(foo()," -
1 t
)))
2091 (python-tests-look-at "5, 6)")
2092 (should (not (python-info-assignment-continuation-line-p)))))
2094 (ert-deftest python-info-looking-at-beginning-of-defun-1
()
2095 (python-tests-with-temp-buffer
2097 def decorat0r(deff):
2108 (python-tests-look-at "def decorat0r(deff):")
2109 (should (python-info-looking-at-beginning-of-defun))
2110 (python-tests-look-at "def foo(arg):")
2111 (should (not (python-info-looking-at-beginning-of-defun)))
2112 (python-tests-look-at "def wrap():")
2113 (should (python-info-looking-at-beginning-of-defun))
2114 (python-tests-look-at "deff()")
2115 (should (not (python-info-looking-at-beginning-of-defun)))))
2117 (ert-deftest python-info-current-line-comment-p-1
()
2118 (python-tests-with-temp-buffer
2121 foo = True # another comment
2125 print ('bar') # print bar
2127 (python-tests-look-at "# this is a comment")
2128 (should (python-info-current-line-comment-p))
2129 (python-tests-look-at "foo = True # another comment")
2130 (should (not (python-info-current-line-comment-p)))
2131 (python-tests-look-at "'#this is a string'")
2132 (should (not (python-info-current-line-comment-p)))
2133 (python-tests-look-at "# more comments")
2134 (should (python-info-current-line-comment-p))
2135 (python-tests-look-at "print ('bar') # print bar")
2136 (should (not (python-info-current-line-comment-p)))))
2138 (ert-deftest python-info-current-line-empty-p
()
2139 (python-tests-with-temp-buffer
2143 foo = True # another comment
2145 (should (python-info-current-line-empty-p))
2146 (python-tests-look-at "# this is a comment")
2147 (should (not (python-info-current-line-empty-p)))
2149 (should (python-info-current-line-empty-p))))
2152 ;;; Utility functions
2154 (ert-deftest python-util-goto-line-1
()
2155 (python-tests-with-temp-buffer
2160 pass" (make-string 20 ?
\n))
2161 (python-util-goto-line 10)
2162 (should (= (line-number-at-pos) 10))
2163 (python-util-goto-line 20)
2164 (should (= (line-number-at-pos) 20))))
2166 (ert-deftest python-util-clone-local-variables-1
()
2167 (let ((buffer (generate-new-buffer
2168 "python-util-clone-local-variables-1"))
2170 '((python-fill-docstring-style . django
)
2171 (python-shell-interpreter .
"python")
2172 (python-shell-interpreter-args .
"manage.py shell")
2173 (python-shell-prompt-regexp .
"In \\[[0-9]+\\]: ")
2174 (python-shell-prompt-output-regexp .
"Out\\[[0-9]+\\]: ")
2175 (python-shell-extra-pythonpaths "/home/user/pylib/")
2176 (python-shell-completion-setup-code
2177 .
"from IPython.core.completerlib import module_completion")
2178 (python-shell-completion-module-string-code
2179 .
"';'.join(module_completion('''%s'''))\n")
2180 (python-shell-completion-string-code
2181 .
"';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
2182 (python-shell-virtualenv-path
2183 .
"/home/user/.virtualenvs/project"))))
2184 (with-current-buffer buffer
2185 (kill-all-local-variables)
2186 (dolist (ccons varcons
)
2187 (set (make-local-variable (car ccons
)) (cdr ccons
))))
2188 (python-tests-with-temp-buffer
2190 (python-util-clone-local-variables buffer
)
2191 (dolist (ccons varcons
)
2193 (equal (symbol-value (car ccons
)) (cdr ccons
)))))
2194 (kill-buffer buffer
)))
2196 (ert-deftest python-util-forward-comment-1
()
2197 (python-tests-with-temp-buffer
2201 # bad indented comment
2202 # more comments" (make-string 9999 ?
\n))
2203 (python-util-forward-comment 1)
2204 (should (= (point) (point-max)))
2205 (python-util-forward-comment -
1)
2206 (should (= (point) (point-min)))))
2209 (provide 'python-tests
)
2213 ;; indent-tabs-mode: nil
2216 ;;; python-tests.el ends here