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' enabeld 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 (defun python-tests-look-at (string &optional num restore-point)
38 "Move point at beginning of STRING in the current buffer.
39 Optional argument NUM defaults to 1 and is an integer indicating
40 how many ocurrences must be found, when positive the search is
41 done forwards, otherwise backwards. When RESTORE-POINT is
42 non-nil the point is not moved but the position found is still
43 returned. When searching forward and point is already looking at
44 STRING, it is skipped so the next STRING occurrence is selected."
45 (let* ((num (or num 1))
46 (starting-point (point))
47 (string (regexp-quote string))
48 (search-fn (if (> num 0) #'re-search-forward #'re-search-backward))
49 (deinc-fn (if (> num 0) #'1- #'1+))
53 (while (not (= num 0))
56 ;; Moving forward and already looking at STRING, skip it.
57 (forward-char (length (match-string-no-properties 0))))
58 (and (not (funcall search-fn string nil t))
61 ;; `re-search-forward' leaves point at the end of the
62 ;; occurrence, move back so point is at the beginning
64 (forward-char (- (length (match-string-no-properties 0)))))
66 num (funcall deinc-fn num)
67 found-point (point))))
69 (and restore-point (goto-char starting-point)))))
72 ;;; Tests for your tests, so you can test while you test.
74 (ert-deftest python-tests-look-at-1 ()
75 "Test forward movement."
76 (python-tests-with-temp-buffer
77 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
78 sed do eiusmod tempor incididunt ut labore et dolore magna
80 (let ((expected (save-excursion
82 (re-search-forward "et" nil t))
85 (should (= (python-tests-look-at "et" 3 t) expected))
86 ;; Even if NUM is bigger than found occurrences the point of last
87 ;; one should be returned.
88 (should (= (python-tests-look-at "et" 6 t) expected))
89 ;; If already looking at STRING, it should skip it.
90 (dotimes (i 2) (re-search-forward "et"))
92 (should (= (python-tests-look-at "et") expected)))))
94 (ert-deftest python-tests-look-at-2 ()
95 "Test backward movement."
96 (python-tests-with-temp-buffer
97 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
98 sed do eiusmod tempor incididunt ut labore et dolore magna
102 (re-search-forward "et" nil t)
106 (re-search-forward "et" nil t))
107 (should (= (python-tests-look-at "et" -3 t) expected))
108 (should (= (python-tests-look-at "et" -6 t) expected)))))
114 ;;; Python specialized rx
117 ;;; Font-lock and syntax
122 ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
124 (ert-deftest python-indent-pep8-1 ()
126 (python-tests-with-temp-buffer
127 "# Aligned with opening delimiter
128 foo = long_function_name(var_one, var_two,
131 (should (eq (car (python-indent-context)) 'no-indent))
132 (should (= (python-indent-calculate-indentation) 0))
133 (python-tests-look-at "foo = long_function_name(var_one, var_two,")
134 (should (eq (car (python-indent-context)) 'after-line))
135 (should (= (python-indent-calculate-indentation) 0))
136 (python-tests-look-at "var_three, var_four)")
137 (should (eq (car (python-indent-context)) 'inside-paren))
138 (should (= (python-indent-calculate-indentation) 25))))
140 (ert-deftest python-indent-pep8-2 ()
142 (python-tests-with-temp-buffer
143 "# More indentation included to distinguish this from the rest.
144 def long_function_name(
145 var_one, var_two, var_three,
149 (should (eq (car (python-indent-context)) 'no-indent))
150 (should (= (python-indent-calculate-indentation) 0))
151 (python-tests-look-at "def long_function_name(")
152 (should (eq (car (python-indent-context)) 'after-line))
153 (should (= (python-indent-calculate-indentation) 0))
154 (python-tests-look-at "var_one, var_two, var_three,")
155 (should (eq (car (python-indent-context)) 'inside-paren))
156 (should (= (python-indent-calculate-indentation) 8))
157 (python-tests-look-at "var_four):")
158 (should (eq (car (python-indent-context)) 'inside-paren))
159 (should (= (python-indent-calculate-indentation) 8))
160 (python-tests-look-at "print (var_one)")
161 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
162 (should (= (python-indent-calculate-indentation) 4))))
164 (ert-deftest python-indent-pep8-3 ()
166 (python-tests-with-temp-buffer
167 "# Extra indentation is not necessary.
168 foo = long_function_name(
172 (should (eq (car (python-indent-context)) 'no-indent))
173 (should (= (python-indent-calculate-indentation) 0))
174 (python-tests-look-at "foo = long_function_name(")
175 (should (eq (car (python-indent-context)) 'after-line))
176 (should (= (python-indent-calculate-indentation) 0))
177 (python-tests-look-at "var_one, var_two,")
178 (should (eq (car (python-indent-context)) 'inside-paren))
179 (should (= (python-indent-calculate-indentation) 4))
180 (python-tests-look-at "var_three, var_four)")
181 (should (eq (car (python-indent-context)) 'inside-paren))
182 (should (= (python-indent-calculate-indentation) 4))))
184 (ert-deftest python-indent-inside-paren-1 ()
185 "The most simple inside-paren case that shouldn't fail."
186 (python-tests-with-temp-buffer
204 (python-tests-look-at "data = {")
205 (should (eq (car (python-indent-context)) 'after-line))
206 (should (= (python-indent-calculate-indentation) 0))
207 (python-tests-look-at "'key':")
208 (should (eq (car (python-indent-context)) 'inside-paren))
209 (should (= (python-indent-calculate-indentation) 4))
210 (python-tests-look-at "{")
211 (should (eq (car (python-indent-context)) 'inside-paren))
212 (should (= (python-indent-calculate-indentation) 4))
213 (python-tests-look-at "'objlist': [")
214 (should (eq (car (python-indent-context)) 'inside-paren))
215 (should (= (python-indent-calculate-indentation) 8))
216 (python-tests-look-at "{")
217 (should (eq (car (python-indent-context)) 'inside-paren))
218 (should (= (python-indent-calculate-indentation) 12))
219 (python-tests-look-at "'pk': 1,")
220 (should (eq (car (python-indent-context)) 'inside-paren))
221 (should (= (python-indent-calculate-indentation) 16))
222 (python-tests-look-at "'name': 'first',")
223 (should (eq (car (python-indent-context)) 'inside-paren))
224 (should (= (python-indent-calculate-indentation) 16))
225 (python-tests-look-at "},")
226 (should (eq (car (python-indent-context)) 'inside-paren))
227 (should (= (python-indent-calculate-indentation) 12))
228 (python-tests-look-at "{")
229 (should (eq (car (python-indent-context)) 'inside-paren))
230 (should (= (python-indent-calculate-indentation) 12))
231 (python-tests-look-at "'pk': 2,")
232 (should (eq (car (python-indent-context)) 'inside-paren))
233 (should (= (python-indent-calculate-indentation) 16))
234 (python-tests-look-at "'name': 'second',")
235 (should (eq (car (python-indent-context)) 'inside-paren))
236 (should (= (python-indent-calculate-indentation) 16))
237 (python-tests-look-at "}")
238 (should (eq (car (python-indent-context)) 'inside-paren))
239 (should (= (python-indent-calculate-indentation) 12))
240 (python-tests-look-at "]")
241 (should (eq (car (python-indent-context)) 'inside-paren))
242 (should (= (python-indent-calculate-indentation) 8))
243 (python-tests-look-at "}")
244 (should (eq (car (python-indent-context)) 'inside-paren))
245 (should (= (python-indent-calculate-indentation) 4))
246 (python-tests-look-at "}")
247 (should (eq (car (python-indent-context)) 'inside-paren))
248 (should (= (python-indent-calculate-indentation) 0))))
250 (ert-deftest python-indent-inside-paren-2 ()
251 "Another more compact paren group style."
252 (python-tests-with-temp-buffer
263 (python-tests-look-at "data = {")
264 (should (eq (car (python-indent-context)) 'after-line))
265 (should (= (python-indent-calculate-indentation) 0))
266 (python-tests-look-at "'objlist': [")
267 (should (eq (car (python-indent-context)) 'inside-paren))
268 (should (= (python-indent-calculate-indentation) 4))
269 (python-tests-look-at "{'pk': 1,")
270 (should (eq (car (python-indent-context)) 'inside-paren))
271 (should (= (python-indent-calculate-indentation) 8))
272 (python-tests-look-at "'name': 'first'},")
273 (should (eq (car (python-indent-context)) 'inside-paren))
274 (should (= (python-indent-calculate-indentation) 9))
275 (python-tests-look-at "{'pk': 2,")
276 (should (eq (car (python-indent-context)) 'inside-paren))
277 (should (= (python-indent-calculate-indentation) 8))
278 (python-tests-look-at "'name': 'second'}")
279 (should (eq (car (python-indent-context)) 'inside-paren))
280 (should (= (python-indent-calculate-indentation) 9))
281 (python-tests-look-at "]")
282 (should (eq (car (python-indent-context)) 'inside-paren))
283 (should (= (python-indent-calculate-indentation) 4))
284 (python-tests-look-at "}}")
285 (should (eq (car (python-indent-context)) 'inside-paren))
286 (should (= (python-indent-calculate-indentation) 0))
287 (python-tests-look-at "}")
288 (should (eq (car (python-indent-context)) 'inside-paren))
289 (should (= (python-indent-calculate-indentation) 0))))
291 (ert-deftest python-indent-after-block-1 ()
292 "The most simple after-block case that shouldn't fail."
293 (python-tests-with-temp-buffer
295 def foo(a, b, c=True):
297 (should (eq (car (python-indent-context)) 'no-indent))
298 (should (= (python-indent-calculate-indentation) 0))
299 (goto-char (point-max))
300 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
301 (should (= (python-indent-calculate-indentation) 4))))
303 (ert-deftest python-indent-after-block-2 ()
304 "A weird (malformed) multiline block statement."
305 (python-tests-with-temp-buffer
311 (goto-char (point-max))
312 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
313 (should (= (python-indent-calculate-indentation) 4))))
315 (ert-deftest python-indent-dedenters-1 ()
316 "Check all dedenters."
317 (python-tests-with-temp-buffer
327 except (IndexError, AttributeError):
330 print ('nor a, nor b are true')
332 (python-tests-look-at "if a:")
333 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
334 (should (= (python-indent-calculate-indentation) 4))
335 (python-tests-look-at "print (a)")
336 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
337 (should (= (python-indent-calculate-indentation) 8))
338 (python-tests-look-at "elif b:")
339 (should (eq (car (python-indent-context)) 'after-line))
340 (should (= (python-indent-calculate-indentation) 4))
341 (python-tests-look-at "print (b)")
342 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
343 (should (= (python-indent-calculate-indentation) 8))
344 (python-tests-look-at "else:")
345 (should (eq (car (python-indent-context)) 'after-line))
346 (should (= (python-indent-calculate-indentation) 4))
347 (python-tests-look-at "try:")
348 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
349 (should (= (python-indent-calculate-indentation) 8))
350 (python-tests-look-at "print (c.pop())")
351 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
352 (should (= (python-indent-calculate-indentation) 12))
353 (python-tests-look-at "except (IndexError, AttributeError):")
354 (should (eq (car (python-indent-context)) 'after-line))
355 (should (= (python-indent-calculate-indentation) 8))
356 (python-tests-look-at "print (c)")
357 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
358 (should (= (python-indent-calculate-indentation) 12))
359 (python-tests-look-at "finally:")
360 (should (eq (car (python-indent-context)) 'after-line))
361 (should (= (python-indent-calculate-indentation) 8))
362 (python-tests-look-at "print ('nor a, nor b are true')")
363 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
364 (should (= (python-indent-calculate-indentation) 12))))
366 (ert-deftest python-indent-after-backslash-1 ()
367 "The most common case."
368 (python-tests-with-temp-buffer
370 from foo.bar.baz import something, something_1 \\\\
371 something_2 something_3, \\\\
372 something_4, something_5
374 (python-tests-look-at "from foo.bar.baz import something, something_1")
375 (should (eq (car (python-indent-context)) 'after-line))
376 (should (= (python-indent-calculate-indentation) 0))
377 (python-tests-look-at "something_2 something_3,")
378 (should (eq (car (python-indent-context)) 'after-backslash))
379 (should (= (python-indent-calculate-indentation) 4))
380 (python-tests-look-at "something_4, something_5")
381 (should (eq (car (python-indent-context)) 'after-backslash))
382 (should (= (python-indent-calculate-indentation) 4))
383 (goto-char (point-max))
384 (should (eq (car (python-indent-context)) 'after-line))
385 (should (= (python-indent-calculate-indentation) 0))))
387 (ert-deftest python-indent-after-backslash-2 ()
388 "A pretty extreme complicated case."
389 (python-tests-with-temp-buffer
391 objects = Thing.objects.all() \\\\
401 (python-tests-look-at "objects = Thing.objects.all()")
402 (should (eq (car (python-indent-context)) 'after-line))
403 (should (= (python-indent-calculate-indentation) 0))
404 (python-tests-look-at ".filter(")
405 (should (eq (car (python-indent-context)) 'after-backslash))
406 (should (= (python-indent-calculate-indentation) 23))
407 (python-tests-look-at "type='toy',")
408 (should (eq (car (python-indent-context)) 'inside-paren))
409 (should (= (python-indent-calculate-indentation) 27))
410 (python-tests-look-at "status='bought'")
411 (should (eq (car (python-indent-context)) 'inside-paren))
412 (should (= (python-indent-calculate-indentation) 27))
413 (python-tests-look-at ") \\\\")
414 (should (eq (car (python-indent-context)) 'inside-paren))
415 (should (= (python-indent-calculate-indentation) 23))
416 (python-tests-look-at ".aggregate(")
417 (should (eq (car (python-indent-context)) 'after-backslash))
418 (should (= (python-indent-calculate-indentation) 23))
419 (python-tests-look-at "Sum('amount')")
420 (should (eq (car (python-indent-context)) 'inside-paren))
421 (should (= (python-indent-calculate-indentation) 27))
422 (python-tests-look-at ") \\\\")
423 (should (eq (car (python-indent-context)) 'inside-paren))
424 (should (= (python-indent-calculate-indentation) 23))
425 (python-tests-look-at ".values_list()")
426 (should (eq (car (python-indent-context)) 'after-backslash))
427 (should (= (python-indent-calculate-indentation) 23))
429 (should (eq (car (python-indent-context)) 'after-line))
430 (should (= (python-indent-calculate-indentation) 0))))
435 (ert-deftest python-nav-beginning-of-defun-1 ()
436 (python-tests-with-temp-buffer
438 def decoratorFunctionWithArguments(arg1, arg2, arg3):
439 '''print decorated function call data to stdout.
443 @decoratorFunctionWithArguments('arg1', 'arg2')
444 def func(a, b, c=True):
449 print 'Inside wwrap()'
450 def wrapped_f(*args):
451 print 'Inside wrapped_f()'
452 print 'Decorator arguments:', arg1, arg2, arg3
454 print 'After f(*args)'
458 (python-tests-look-at "return wrap")
459 (should (= (save-excursion
460 (python-nav-beginning-of-defun)
463 (python-tests-look-at "def wrapped_f(*args):" -1)
466 (python-tests-look-at "def wrapped_f(*args):" -1)
467 (should (= (save-excursion
468 (python-nav-beginning-of-defun)
471 (python-tests-look-at "def wwrap(f):" -1)
474 (python-tests-look-at "def wwrap(f):" -1)
475 (should (= (save-excursion
476 (python-nav-beginning-of-defun)
479 (python-tests-look-at "def decoratorFunctionWithArguments" -1)
483 (ert-deftest python-nav-beginning-of-defun-2 ()
484 (python-tests-with-temp-buffer
500 ;; Nested defuns, are handled with care.
501 (python-tests-look-at "def c(self):")
502 (should (= (save-excursion
503 (python-nav-beginning-of-defun)
506 (python-tests-look-at "def m(self):" -1)
509 ;; Defuns on same levels should be respected.
510 (python-tests-look-at "def a():" -1)
511 (should (= (save-excursion
512 (python-nav-beginning-of-defun)
515 (python-tests-look-at "def b():" -1)
518 ;; Jump to a top level defun.
519 (python-tests-look-at "def b():" -1)
520 (should (= (save-excursion
521 (python-nav-beginning-of-defun)
524 (python-tests-look-at "def m(self):" -1)
527 ;; Jump to a top level defun again.
528 (python-tests-look-at "def m(self):" -1)
529 (should (= (save-excursion
530 (python-nav-beginning-of-defun)
533 (python-tests-look-at "class C(object):" -1)
537 (ert-deftest python-nav-end-of-defun-1 ()
538 (python-tests-with-temp-buffer
554 (should (= (save-excursion
555 (python-tests-look-at "class C(object):")
556 (python-nav-end-of-defun)
560 (should (= (save-excursion
561 (python-tests-look-at "def m(self):")
562 (python-nav-end-of-defun)
565 (python-tests-look-at "def c(self):")
568 (should (= (save-excursion
569 (python-tests-look-at "def b():")
570 (python-nav-end-of-defun)
573 (python-tests-look-at "def b():")
576 (should (= (save-excursion
577 (python-tests-look-at "def c(self):")
578 (python-nav-end-of-defun)
583 (ert-deftest python-nav-end-of-defun-2 ()
584 (python-tests-with-temp-buffer
586 def decoratorFunctionWithArguments(arg1, arg2, arg3):
587 '''print decorated function call data to stdout.
591 @decoratorFunctionWithArguments('arg1', 'arg2')
592 def func(a, b, c=True):
597 print 'Inside wwrap()'
598 def wrapped_f(*args):
599 print 'Inside wrapped_f()'
600 print 'Decorator arguments:', arg1, arg2, arg3
602 print 'After f(*args)'
606 (should (= (save-excursion
607 (python-tests-look-at "def decoratorFunctionWithArguments")
608 (python-nav-end-of-defun)
612 (should (= (save-excursion
613 (python-tests-look-at "@decoratorFunctionWithArguments")
614 (python-nav-end-of-defun)
618 (should (= (save-excursion
619 (python-tests-look-at "def wwrap(f):")
620 (python-nav-end-of-defun)
623 (python-tests-look-at "return wwrap")
624 (line-beginning-position))))
625 (should (= (save-excursion
626 (python-tests-look-at "def wrapped_f(*args):")
627 (python-nav-end-of-defun)
630 (python-tests-look-at "return wrapped_f")
631 (line-beginning-position))))
632 (should (= (save-excursion
633 (python-tests-look-at "f(*args)")
634 (python-nav-end-of-defun)
637 (python-tests-look-at "return wrapped_f")
638 (line-beginning-position))))))
641 (ert-deftest python-nav-beginning-of-statement-1 ()
642 (python-tests-with-temp-buffer
652 v3 = ('this is a string'
657 # this is a comment, yo
658 'continue previous line')
664 (python-tests-look-at "v2 =")
665 (python-util-forward-comment -1)
666 (should (= (save-excursion
667 (python-nav-beginning-of-statement)
669 (python-tests-look-at "v1 =" -1 t)))
670 (python-tests-look-at "v3 =")
671 (python-util-forward-comment -1)
672 (should (= (save-excursion
673 (python-nav-beginning-of-statement)
675 (python-tests-look-at "v2 =" -1 t)))
676 (python-tests-look-at "v4 =")
677 (python-util-forward-comment -1)
678 (should (= (save-excursion
679 (python-nav-beginning-of-statement)
681 (python-tests-look-at "v3 =" -1 t)))
682 (goto-char (point-max))
683 (python-util-forward-comment -1)
684 (should (= (save-excursion
685 (python-nav-beginning-of-statement)
687 (python-tests-look-at "v4 =" -1 t)))))
689 (ert-deftest python-nav-end-of-statement-1 ()
690 (python-tests-with-temp-buffer
700 v3 = ('this is a string'
705 # this is a comment, yo
706 'continue previous line')
712 (python-tests-look-at "v1 =")
713 (should (= (save-excursion
714 (python-nav-end-of-statement)
717 (python-tests-look-at "789")
718 (line-end-position))))
719 (python-tests-look-at "v2 =")
720 (should (= (save-excursion
721 (python-nav-end-of-statement)
724 (python-tests-look-at "value4)")
725 (line-end-position))))
726 (python-tests-look-at "v3 =")
727 (should (= (save-excursion
728 (python-nav-end-of-statement)
731 (python-tests-look-at
732 "'continue previous line')")
733 (line-end-position))))
734 (python-tests-look-at "v4 =")
735 (should (= (save-excursion
736 (python-nav-end-of-statement)
739 (goto-char (point-max))
740 (python-util-forward-comment -1)
743 (ert-deftest python-nav-forward-statement-1 ()
744 (python-tests-with-temp-buffer
754 v3 = ('this is a string'
759 # this is a comment, yo
760 'continue previous line')
766 (python-tests-look-at "v1 =")
767 (should (= (save-excursion
768 (python-nav-forward-statement)
770 (python-tests-look-at "v2 =")))
771 (should (= (save-excursion
772 (python-nav-forward-statement)
774 (python-tests-look-at "v3 =")))
775 (should (= (save-excursion
776 (python-nav-forward-statement)
778 (python-tests-look-at "v4 =")))
779 (should (= (save-excursion
780 (python-nav-forward-statement)
784 (ert-deftest python-nav-backward-statement-1 ()
785 (python-tests-with-temp-buffer
795 v3 = ('this is a string'
800 # this is a comment, yo
801 'continue previous line')
807 (goto-char (point-max))
808 (should (= (save-excursion
809 (python-nav-backward-statement)
811 (python-tests-look-at "v4 =" -1)))
812 (should (= (save-excursion
813 (python-nav-backward-statement)
815 (python-tests-look-at "v3 =" -1)))
816 (should (= (save-excursion
817 (python-nav-backward-statement)
819 (python-tests-look-at "v2 =" -1)))
820 (should (= (save-excursion
821 (python-nav-backward-statement)
823 (python-tests-look-at "v1 =" -1)))))
825 (ert-deftest python-nav-backward-statement-2 ()
826 :expected-result :failed
827 (python-tests-with-temp-buffer
838 ;; FIXME: For some reason `python-nav-backward-statement' is moving
839 ;; back two sentences when starting from 'value4)'.
840 (goto-char (point-max))
841 (python-util-forward-comment -1)
842 (should (= (save-excursion
843 (python-nav-backward-statement)
845 (python-tests-look-at "v2 =" -1 t)))))
847 (ert-deftest python-nav-beginning-of-block-1 ()
848 (python-tests-with-temp-buffer
850 def decoratorFunctionWithArguments(arg1, arg2, arg3):
851 '''print decorated function call data to stdout.
855 @decoratorFunctionWithArguments('arg1', 'arg2')
856 def func(a, b, c=True):
861 print 'Inside wwrap()'
862 def wrapped_f(*args):
863 print 'Inside wrapped_f()'
864 print 'Decorator arguments:', arg1, arg2, arg3
866 print 'After f(*args)'
870 (python-tests-look-at "return wwrap")
871 (should (= (save-excursion
872 (python-nav-beginning-of-block)
874 (python-tests-look-at "def decoratorFunctionWithArguments" -1)))
875 (python-tests-look-at "print 'Inside wwrap()'")
876 (should (= (save-excursion
877 (python-nav-beginning-of-block)
879 (python-tests-look-at "def wwrap(f):" -1)))
880 (python-tests-look-at "print 'After f(*args)'")
882 (should (= (save-excursion
883 (python-nav-beginning-of-block)
885 (python-tests-look-at "def wrapped_f(*args):" -1)))
886 (python-tests-look-at "return wrapped_f")
887 (should (= (save-excursion
888 (python-nav-beginning-of-block)
890 (python-tests-look-at "def wwrap(f):" -1)))))
892 (ert-deftest python-nav-end-of-block-1 ()
893 (python-tests-with-temp-buffer
895 def decoratorFunctionWithArguments(arg1, arg2, arg3):
896 '''print decorated function call data to stdout.
900 @decoratorFunctionWithArguments('arg1', 'arg2')
901 def func(a, b, c=True):
906 print 'Inside wwrap()'
907 def wrapped_f(*args):
908 print 'Inside wrapped_f()'
909 print 'Decorator arguments:', arg1, arg2, arg3
911 print 'After f(*args)'
915 (python-tests-look-at "def decoratorFunctionWithArguments")
916 (should (= (save-excursion
917 (python-nav-end-of-block)
920 (goto-char (point-max))
921 (python-util-forward-comment -1)
923 (python-tests-look-at "def wwrap(f):")
924 (should (= (save-excursion
925 (python-nav-end-of-block)
928 (python-tests-look-at "return wrapped_f")
929 (line-end-position))))
931 (should (= (save-excursion
932 (python-nav-end-of-block)
935 (python-tests-look-at "return wrapped_f")
936 (line-end-position))))
937 (python-tests-look-at "f(*args)")
938 (should (= (save-excursion
939 (python-nav-end-of-block)
942 (python-tests-look-at "print 'After f(*args)'")
943 (line-end-position))))))
945 (ert-deftest python-nav-forward-block-1 ()
946 "This also accounts as a test for `python-nav-backward-block'."
947 (python-tests-with-temp-buffer
949 if request.user.is_authenticated():
953 profile = request.user.get_profile()
954 except Profile.DoesNotExist:
955 profile = Profile.objects.create(user=request.user)
958 profile.recalculate_stats()
960 profile.clear_stats()
965 (should (= (save-excursion (python-nav-forward-block))
966 (python-tests-look-at "if request.user.is_authenticated():")))
967 (should (= (save-excursion (python-nav-forward-block))
968 (python-tests-look-at "try:")))
969 (should (= (save-excursion (python-nav-forward-block))
970 (python-tests-look-at "except Profile.DoesNotExist:")))
971 (should (= (save-excursion (python-nav-forward-block))
972 (python-tests-look-at "else:")))
973 (should (= (save-excursion (python-nav-forward-block))
974 (python-tests-look-at "if profile.stats:")))
975 (should (= (save-excursion (python-nav-forward-block))
976 (python-tests-look-at "else:")))
977 (should (= (save-excursion (python-nav-forward-block))
978 (python-tests-look-at "finally:")))
979 ;; When point is at the last block, leave it there and return nil
980 (should (not (save-excursion (python-nav-forward-block))))
981 ;; Move backwards, and even if the number of moves is less than the
982 ;; provided argument return the point.
983 (should (= (save-excursion (python-nav-forward-block -10))
984 (python-tests-look-at
985 "if request.user.is_authenticated():" -1)))))
987 (ert-deftest python-nav-lisp-forward-sexp-safe-1 ()
988 (python-tests-with-temp-buffer
990 profile = Profile.objects.create(user=request.user)
993 (python-tests-look-at "profile =")
994 (python-nav-lisp-forward-sexp-safe 4)
995 (should (looking-at "(user=request.user)"))
996 (python-tests-look-at "user=request.user")
997 (python-nav-lisp-forward-sexp-safe -1)
998 (should (looking-at "(user=request.user)"))
999 (python-nav-lisp-forward-sexp-safe -4)
1000 (should (looking-at "profile ="))
1001 (python-tests-look-at "user=request.user")
1002 (python-nav-lisp-forward-sexp-safe 3)
1003 (should (looking-at ")"))
1004 (python-nav-lisp-forward-sexp-safe 1)
1005 (should (looking-at "$"))
1006 (python-nav-lisp-forward-sexp-safe 1)
1007 (should (looking-at ".notify()"))))
1009 (ert-deftest python-nav-forward-sexp-1 ()
1010 (python-tests-with-temp-buffer
1016 (python-tests-look-at "a()")
1017 (python-nav-forward-sexp)
1018 (should (looking-at "$"))
1019 (should (save-excursion
1021 (looking-at "a()")))
1022 (python-nav-forward-sexp)
1023 (should (looking-at "$"))
1024 (should (save-excursion
1026 (looking-at "b()")))
1027 (python-nav-forward-sexp)
1028 (should (looking-at "$"))
1029 (should (save-excursion
1031 (looking-at "c()")))
1032 ;; Movement next to a paren should do what lisp does and
1033 ;; unfortunately It can't change, because otherwise
1034 ;; `blink-matching-open' breaks.
1035 (python-nav-forward-sexp -1)
1036 (should (looking-at "()"))
1037 (should (save-excursion
1039 (looking-at "c()")))
1040 (python-nav-forward-sexp -1)
1041 (should (looking-at "c()"))
1042 (python-nav-forward-sexp -1)
1043 (should (looking-at "b()"))
1044 (python-nav-forward-sexp -1)
1045 (should (looking-at "a()"))))
1047 (ert-deftest python-nav-forward-sexp-2 ()
1048 (python-tests-with-temp-buffer
1057 (python-tests-look-at "aa =")
1058 (python-nav-forward-sexp)
1059 (should (looking-at " = bbb"))
1060 (python-nav-forward-sexp)
1061 (should (looking-at "$"))
1062 (should (save-excursion
1063 (back-to-indentation)
1064 (looking-at "aaa = bbb")))
1065 (python-nav-forward-sexp)
1066 (should (looking-at "$"))
1067 (should (save-excursion
1068 (back-to-indentation)
1069 (looking-at "ccc = ddd")))
1070 (python-nav-forward-sexp)
1071 (should (looking-at "$"))
1072 (should (save-excursion
1073 (back-to-indentation)
1074 (looking-at "eee = fff")))
1075 (python-nav-forward-sexp)
1076 (should (looking-at "$"))
1077 (should (save-excursion
1078 (back-to-indentation)
1079 (looking-at "return ggg")))
1080 (python-nav-forward-sexp -1)
1081 (should (looking-at "def func():"))))
1083 (ert-deftest python-nav-forward-sexp-3 ()
1084 (python-tests-with-temp-buffer
1086 from some_module import some_sub_module
1087 from another_module import another_sub_module
1089 def another_statement():
1092 (python-tests-look-at "some_module")
1093 (python-nav-forward-sexp)
1094 (should (looking-at " import"))
1095 (python-nav-forward-sexp)
1096 (should (looking-at " some_sub_module"))
1097 (python-nav-forward-sexp)
1098 (should (looking-at "$"))
1101 (back-to-indentation)
1103 "from some_module import some_sub_module")))
1104 (python-nav-forward-sexp)
1105 (should (looking-at "$"))
1108 (back-to-indentation)
1110 "from another_module import another_sub_module")))
1111 (python-nav-forward-sexp)
1112 (should (looking-at "$"))
1115 (back-to-indentation)
1118 (python-nav-forward-sexp -1)
1119 (should (looking-at "def another_statement():"))
1120 (python-nav-forward-sexp -1)
1121 (should (looking-at "from another_module import another_sub_module"))
1122 (python-nav-forward-sexp -1)
1123 (should (looking-at "from some_module import some_sub_module"))))
1125 (ert-deftest python-nav-up-list-1 ()
1126 (python-tests-with-temp-buffer
1130 return [i for i in range(3)]
1132 (python-tests-look-at "3)]")
1133 (python-nav-up-list)
1134 (should (looking-at "]"))
1135 (python-nav-up-list)
1136 (should (looking-at "$"))))
1138 (ert-deftest python-nav-backward-up-list-1 ()
1139 :expected-result :failed
1140 (python-tests-with-temp-buffer
1144 return [i for i in range(3)]
1146 (python-tests-look-at "3)]")
1147 (python-nav-backward-up-list)
1148 (should (looking-at "(3)\\]"))
1149 (python-nav-backward-up-list)
1151 "\\[i for i in range(3)\\]"))
1152 ;; FIXME: Need to move to beginning-of-statement.
1153 (python-nav-backward-up-list)
1155 "return \\[i for i in range(3)\\]"))
1156 (python-nav-backward-up-list)
1157 (should (looking-at "if True:"))
1158 (python-nav-backward-up-list)
1159 (should (looking-at "def f():"))))
1162 ;;; Shell integration
1165 ;;; Shell completion
1168 ;;; PDB Track integration
1171 ;;; Symbol completion
1190 (ert-deftest python-imenu-prev-index-position-1 ()
1192 (python-tests-with-temp-buffer
1194 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1195 '''print decorated function call data to stdout.
1199 @decoratorFunctionWithArguments('arg1', 'arg2')
1200 def func(a, b, c=True):
1205 print 'Inside wwrap()'
1206 def wrapped_f(*args):
1207 print 'Inside wrapped_f()'
1208 print 'Decorator arguments:', arg1, arg2, arg3
1210 print 'After f(*args)'
1214 def test(): # Some comment
1215 'This is a test function'
1233 '(("*Rescan*" . -99)
1234 ("decoratorFunctionWithArguments" . 2)
1235 ("decoratorFunctionWithArguments.wwrap" . 224)
1236 ("decoratorFunctionWithArguments.wwrap.wrapped_f" . 273)
1245 (should (= (cdr (assoc-string (car elt) expected))
1246 (if (markerp (cdr elt))
1247 (marker-position (cdr elt))
1249 (imenu--make-index-alist)))))
1254 (ert-deftest python-info-current-defun-1 ()
1255 (python-tests-with-temp-buffer
1260 (should (string= "foo" (python-info-current-defun)))
1261 (should (string= "def foo" (python-info-current-defun t)))
1263 (should (not (python-info-current-defun)))
1264 (indent-for-tab-command)
1265 (should (string= "foo" (python-info-current-defun)))
1266 (should (string= "def foo" (python-info-current-defun t)))))
1268 (ert-deftest python-info-current-defun-2 ()
1269 (python-tests-with-temp-buffer
1275 return [i for i in range(3)]
1289 (should (string= "C" (python-info-current-defun)))
1290 (should (string= "class C" (python-info-current-defun t)))
1291 (python-tests-look-at "return [i for ")
1292 (should (string= "C.m" (python-info-current-defun)))
1293 (should (string= "def C.m" (python-info-current-defun t)))
1294 (python-tests-look-at "def b():")
1295 (should (string= "C.m.b" (python-info-current-defun)))
1296 (should (string= "def C.m.b" (python-info-current-defun t)))
1298 (indent-for-tab-command)
1299 (python-indent-dedent-line-backspace 1)
1300 (should (string= "C.m" (python-info-current-defun)))
1301 (should (string= "def C.m" (python-info-current-defun t)))
1302 (python-tests-look-at "def c(self):")
1304 (indent-for-tab-command)
1305 (should (string= "C.m.a" (python-info-current-defun)))
1306 (should (string= "def C.m.a" (python-info-current-defun t)))
1307 (python-indent-dedent-line-backspace 1)
1308 (should (string= "C.m" (python-info-current-defun)))
1309 (should (string= "def C.m" (python-info-current-defun t)))
1310 (python-indent-dedent-line-backspace 1)
1311 (should (string= "C" (python-info-current-defun)))
1312 (should (string= "class C" (python-info-current-defun t)))
1313 (python-tests-look-at "def c(self):")
1314 (should (string= "C.c" (python-info-current-defun)))
1315 (should (string= "def C.c" (python-info-current-defun t)))
1316 (python-tests-look-at "pass")
1317 (should (string= "C.c" (python-info-current-defun)))
1318 (should (string= "def C.c" (python-info-current-defun t)))))
1320 (ert-deftest python-info-current-defun-3 ()
1321 (python-tests-with-temp-buffer
1323 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1324 '''print decorated function call data to stdout.
1328 @decoratorFunctionWithArguments('arg1', 'arg2')
1329 def func(a, b, c=True):
1334 print 'Inside wwrap()'
1335 def wrapped_f(*args):
1336 print 'Inside wrapped_f()'
1337 print 'Decorator arguments:', arg1, arg2, arg3
1339 print 'After f(*args)'
1343 (python-tests-look-at "def wwrap(f):")
1345 (should (not (python-info-current-defun)))
1346 (indent-for-tab-command 1)
1347 (should (string= (python-info-current-defun)
1348 "decoratorFunctionWithArguments"))
1349 (should (string= (python-info-current-defun t)
1350 "def decoratorFunctionWithArguments"))
1351 (python-tests-look-at "def wrapped_f(*args):")
1352 (should (string= (python-info-current-defun)
1353 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
1354 (should (string= (python-info-current-defun t)
1355 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
1356 (python-tests-look-at "return wrapped_f")
1357 (should (string= (python-info-current-defun)
1358 "decoratorFunctionWithArguments.wwrap"))
1359 (should (string= (python-info-current-defun t)
1360 "def decoratorFunctionWithArguments.wwrap"))
1362 (python-tests-look-at "return wwrap")
1363 (should (string= (python-info-current-defun)
1364 "decoratorFunctionWithArguments"))
1365 (should (string= (python-info-current-defun t)
1366 "def decoratorFunctionWithArguments"))))
1368 (ert-deftest python-info-current-symbol-1 ()
1369 (python-tests-with-temp-buffer
1379 (python-tests-look-at "self.c()")
1380 (should (string= "self.c" (python-info-current-symbol)))
1381 (should (string= "C.c" (python-info-current-symbol t)))))
1383 (ert-deftest python-info-current-symbol-2 ()
1384 (python-tests-with-temp-buffer
1396 (python-tests-look-at "self.c()")
1397 (should (string= "self.c" (python-info-current-symbol)))
1398 (should (string= "C.M.c" (python-info-current-symbol t)))))
1400 (ert-deftest python-info-current-symbol-3 ()
1401 "Keywords should not be considered symbols."
1402 :expected-result :failed
1403 (python-tests-with-temp-buffer
1408 ;; FIXME: keywords are not symbols.
1409 (python-tests-look-at "class C")
1410 (should (not (python-info-current-symbol)))
1411 (should (not (python-info-current-symbol t)))
1412 (python-tests-look-at "C(object)")
1413 (should (string= "C" (python-info-current-symbol)))
1414 (should (string= "class C" (python-info-current-symbol t)))))
1416 (ert-deftest python-info-statement-starts-block-p-1 ()
1417 (python-tests-with-temp-buffer
1419 def long_function_name(
1420 var_one, var_two, var_three,
1424 (python-tests-look-at "def long_function_name")
1425 (should (python-info-statement-starts-block-p))
1426 (python-tests-look-at "print (var_one)")
1427 (python-util-forward-comment -1)
1428 (should (python-info-statement-starts-block-p))))
1430 (ert-deftest python-info-statement-starts-block-p-2 ()
1431 (python-tests-with-temp-buffer
1433 if width == 0 and height == 0 and \\\\
1434 color == 'red' and emphasis == 'strong' or \\\\
1436 raise ValueError('sorry, you lose')
1438 (python-tests-look-at "if width == 0 and")
1439 (should (python-info-statement-starts-block-p))
1440 (python-tests-look-at "raise ValueError(")
1441 (python-util-forward-comment -1)
1442 (should (python-info-statement-starts-block-p))))
1444 (ert-deftest python-info-statement-ends-block-p-1 ()
1445 (python-tests-with-temp-buffer
1447 def long_function_name(
1448 var_one, var_two, var_three,
1452 (python-tests-look-at "print (var_one)")
1453 (should (python-info-statement-ends-block-p))))
1455 (ert-deftest python-info-statement-ends-block-p-2 ()
1456 (python-tests-with-temp-buffer
1458 if width == 0 and height == 0 and \\\\
1459 color == 'red' and emphasis == 'strong' or \\\\
1466 (python-tests-look-at "raise ValueError(")
1467 (should (python-info-statement-ends-block-p))))
1469 (ert-deftest python-info-beginning-of-statement-p-1 ()
1470 (python-tests-with-temp-buffer
1472 def long_function_name(
1473 var_one, var_two, var_three,
1477 (python-tests-look-at "def long_function_name")
1478 (should (python-info-beginning-of-statement-p))
1480 (should (not (python-info-beginning-of-statement-p)))
1481 (python-tests-look-at "print (var_one)")
1482 (should (python-info-beginning-of-statement-p))
1483 (goto-char (line-beginning-position))
1484 (should (not (python-info-beginning-of-statement-p)))))
1486 (ert-deftest python-info-beginning-of-statement-p-2 ()
1487 (python-tests-with-temp-buffer
1489 if width == 0 and height == 0 and \\\\
1490 color == 'red' and emphasis == 'strong' or \\\\
1497 (python-tests-look-at "if width == 0 and")
1498 (should (python-info-beginning-of-statement-p))
1500 (should (not (python-info-beginning-of-statement-p)))
1501 (python-tests-look-at "raise ValueError(")
1502 (should (python-info-beginning-of-statement-p))
1503 (goto-char (line-beginning-position))
1504 (should (not (python-info-beginning-of-statement-p)))))
1506 (ert-deftest python-info-end-of-statement-p-1 ()
1507 (python-tests-with-temp-buffer
1509 def long_function_name(
1510 var_one, var_two, var_three,
1514 (python-tests-look-at "def long_function_name")
1515 (should (not (python-info-end-of-statement-p)))
1517 (should (not (python-info-end-of-statement-p)))
1518 (python-tests-look-at "print (var_one)")
1519 (python-util-forward-comment -1)
1520 (should (python-info-end-of-statement-p))
1521 (python-tests-look-at "print (var_one)")
1522 (should (not (python-info-end-of-statement-p)))
1524 (should (python-info-end-of-statement-p))))
1526 (ert-deftest python-info-end-of-statement-p-2 ()
1527 (python-tests-with-temp-buffer
1529 if width == 0 and height == 0 and \\\\
1530 color == 'red' and emphasis == 'strong' or \\\\
1537 (python-tests-look-at "if width == 0 and")
1538 (should (not (python-info-end-of-statement-p)))
1540 (should (not (python-info-end-of-statement-p)))
1541 (python-tests-look-at "raise ValueError(")
1542 (python-util-forward-comment -1)
1543 (should (python-info-end-of-statement-p))
1544 (python-tests-look-at "raise ValueError(")
1545 (should (not (python-info-end-of-statement-p)))
1547 (should (not (python-info-end-of-statement-p)))
1548 (goto-char (point-max))
1549 (python-util-forward-comment -1)
1550 (should (python-info-end-of-statement-p))))
1552 (ert-deftest python-info-beginning-of-block-p-1 ()
1553 (python-tests-with-temp-buffer
1555 def long_function_name(
1556 var_one, var_two, var_three,
1560 (python-tests-look-at "def long_function_name")
1561 (should (python-info-beginning-of-block-p))
1562 (python-tests-look-at "var_one, var_two, var_three,")
1563 (should (not (python-info-beginning-of-block-p)))
1564 (python-tests-look-at "print (var_one)")
1565 (should (not (python-info-beginning-of-block-p)))))
1567 (ert-deftest python-info-beginning-of-block-p-2 ()
1568 (python-tests-with-temp-buffer
1570 if width == 0 and height == 0 and \\\\
1571 color == 'red' and emphasis == 'strong' or \\\\
1578 (python-tests-look-at "if width == 0 and")
1579 (should (python-info-beginning-of-block-p))
1580 (python-tests-look-at "color == 'red' and emphasis")
1581 (should (not (python-info-beginning-of-block-p)))
1582 (python-tests-look-at "raise ValueError(")
1583 (should (not (python-info-beginning-of-block-p)))))
1585 (ert-deftest python-info-end-of-block-p-1 ()
1586 (python-tests-with-temp-buffer
1588 def long_function_name(
1589 var_one, var_two, var_three,
1593 (python-tests-look-at "def long_function_name")
1594 (should (not (python-info-end-of-block-p)))
1595 (python-tests-look-at "var_one, var_two, var_three,")
1596 (should (not (python-info-end-of-block-p)))
1597 (python-tests-look-at "var_four):")
1599 (should (not (python-info-end-of-block-p)))
1600 (python-tests-look-at "print (var_one)")
1601 (should (not (python-info-end-of-block-p)))
1603 (should (python-info-end-of-block-p))))
1605 (ert-deftest python-info-end-of-block-p-2 ()
1606 (python-tests-with-temp-buffer
1608 if width == 0 and height == 0 and \\\\
1609 color == 'red' and emphasis == 'strong' or \\\\
1616 (python-tests-look-at "if width == 0 and")
1617 (should (not (python-info-end-of-block-p)))
1618 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
1619 (should (not (python-info-end-of-block-p)))
1620 (python-tests-look-at "highlight > 100:")
1622 (should (not (python-info-end-of-block-p)))
1623 (python-tests-look-at "raise ValueError(")
1624 (should (not (python-info-end-of-block-p)))
1626 (should (not (python-info-end-of-block-p)))
1627 (goto-char (point-max))
1628 (python-util-forward-comment -1)
1629 (should (python-info-end-of-block-p))))
1631 (ert-deftest python-info-closing-block-1 ()
1632 (python-tests-with-temp-buffer
1634 if request.user.is_authenticated():
1636 profile = request.user.get_profile()
1637 except Profile.DoesNotExist:
1638 profile = Profile.objects.create(user=request.user)
1641 profile.recalculate_stats()
1643 profile.clear_stats()
1648 (python-tests-look-at "try:")
1649 (should (not (python-info-closing-block)))
1650 (python-tests-look-at "except Profile.DoesNotExist:")
1651 (should (= (python-tests-look-at "try:" -1 t)
1652 (python-info-closing-block)))
1653 (python-tests-look-at "else:")
1654 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -1 t)
1655 (python-info-closing-block)))
1656 (python-tests-look-at "if profile.stats:")
1657 (should (not (python-info-closing-block)))
1658 (python-tests-look-at "else:")
1659 (should (= (python-tests-look-at "if profile.stats:" -1 t)
1660 (python-info-closing-block)))
1661 (python-tests-look-at "finally:")
1662 (should (= (python-tests-look-at "else:" -2 t)
1663 (python-info-closing-block)))))
1665 (ert-deftest python-info-closing-block-2 ()
1666 (python-tests-with-temp-buffer
1668 if request.user.is_authenticated():
1669 profile = Profile.objects.get_or_create(user=request.user)
1671 profile.recalculate_stats()
1678 (python-tests-look-at "'else': 'do it'")
1679 (should (not (python-info-closing-block)))
1680 (python-tests-look-at "'else'")
1681 (should (not (python-info-closing-block)))))
1683 (ert-deftest python-info-line-ends-backslash-p-1 ()
1684 (python-tests-with-temp-buffer
1686 objects = Thing.objects.all() \\\\
1696 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
1697 (should (python-info-line-ends-backslash-p 3))
1698 (should (python-info-line-ends-backslash-p 4))
1699 (should (python-info-line-ends-backslash-p 5))
1700 (should (python-info-line-ends-backslash-p 6)) ; ) \...
1701 (should (python-info-line-ends-backslash-p 7))
1702 (should (python-info-line-ends-backslash-p 8))
1703 (should (python-info-line-ends-backslash-p 9))
1704 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
1706 (ert-deftest python-info-beginning-of-backslash-1 ()
1707 (python-tests-with-temp-buffer
1709 objects = Thing.objects.all() \\\\
1720 (second (python-tests-look-at ".filter("))
1721 (third (python-tests-look-at ".aggregate(")))
1722 (should (= first (python-info-beginning-of-backslash 2)))
1723 (should (= second (python-info-beginning-of-backslash 3)))
1724 (should (= second (python-info-beginning-of-backslash 4)))
1725 (should (= second (python-info-beginning-of-backslash 5)))
1726 (should (= second (python-info-beginning-of-backslash 6)))
1727 (should (= third (python-info-beginning-of-backslash 7)))
1728 (should (= third (python-info-beginning-of-backslash 8)))
1729 (should (= third (python-info-beginning-of-backslash 9)))
1730 (should (not (python-info-beginning-of-backslash 10))))))
1732 (ert-deftest python-info-continuation-line-p-1 ()
1733 (python-tests-with-temp-buffer
1735 if width == 0 and height == 0 and \\\\
1736 color == 'red' and emphasis == 'strong' or \\\\
1743 (python-tests-look-at "if width == 0 and height == 0 and")
1744 (should (not (python-info-continuation-line-p)))
1745 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
1746 (should (python-info-continuation-line-p))
1747 (python-tests-look-at "highlight > 100:")
1748 (should (python-info-continuation-line-p))
1749 (python-tests-look-at "raise ValueError(")
1750 (should (not (python-info-continuation-line-p)))
1751 (python-tests-look-at "'sorry, you lose'")
1752 (should (python-info-continuation-line-p))
1754 (should (python-info-continuation-line-p))
1755 (python-tests-look-at ")")
1756 (should (python-info-continuation-line-p))
1758 (should (not (python-info-continuation-line-p)))))
1760 (ert-deftest python-info-block-continuation-line-p-1 ()
1761 (python-tests-with-temp-buffer
1763 if width == 0 and height == 0 and \\\\
1764 color == 'red' and emphasis == 'strong' or \\\\
1771 (python-tests-look-at "if width == 0 and")
1772 (should (not (python-info-block-continuation-line-p)))
1773 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
1774 (should (= (python-info-block-continuation-line-p)
1775 (python-tests-look-at "if width == 0 and" -1 t)))
1776 (python-tests-look-at "highlight > 100:")
1777 (should (not (python-info-block-continuation-line-p)))))
1779 (ert-deftest python-info-block-continuation-line-p-2 ()
1780 (python-tests-with-temp-buffer
1787 (python-tests-look-at "def foo(a,")
1788 (should (not (python-info-block-continuation-line-p)))
1789 (python-tests-look-at "b,")
1790 (should (= (python-info-block-continuation-line-p)
1791 (python-tests-look-at "def foo(a," -1 t)))
1792 (python-tests-look-at "c):")
1793 (should (not (python-info-block-continuation-line-p)))))
1795 (ert-deftest python-info-assignment-continuation-line-p-1 ()
1796 (python-tests-with-temp-buffer
1798 data = foo(), bar() \\\\
1802 (python-tests-look-at "data = foo(), bar()")
1803 (should (not (python-info-assignment-continuation-line-p)))
1804 (python-tests-look-at "baz(), 4")
1805 (should (= (python-info-assignment-continuation-line-p)
1806 (python-tests-look-at "foo()," -1 t)))
1807 (python-tests-look-at "5, 6")
1808 (should (not (python-info-assignment-continuation-line-p)))))
1810 (ert-deftest python-info-assignment-continuation-line-p-2 ()
1811 (python-tests-with-temp-buffer
1813 data = (foo(), bar()
1817 (python-tests-look-at "data = (foo(), bar()")
1818 (should (not (python-info-assignment-continuation-line-p)))
1819 (python-tests-look-at "baz(), 4")
1820 (should (= (python-info-assignment-continuation-line-p)
1821 (python-tests-look-at "(foo()," -1 t)))
1822 (python-tests-look-at "5, 6)")
1823 (should (not (python-info-assignment-continuation-line-p)))))
1825 (ert-deftest python-info-looking-at-beginning-of-defun-1 ()
1826 (python-tests-with-temp-buffer
1828 def decorat0r(deff):
1839 (python-tests-look-at "def decorat0r(deff):")
1840 (should (python-info-looking-at-beginning-of-defun))
1841 (python-tests-look-at "def foo(arg):")
1842 (should (not (python-info-looking-at-beginning-of-defun)))
1843 (python-tests-look-at "def wrap():")
1844 (should (python-info-looking-at-beginning-of-defun))
1845 (python-tests-look-at "deff()")
1846 (should (not (python-info-looking-at-beginning-of-defun)))))
1848 (ert-deftest python-info-current-line-comment-p-1 ()
1849 (python-tests-with-temp-buffer
1852 foo = True # another comment
1856 print ('bar') # print bar
1858 (python-tests-look-at "# this is a comment")
1859 (should (python-info-current-line-comment-p))
1860 (python-tests-look-at "foo = True # another comment")
1861 (should (not (python-info-current-line-comment-p)))
1862 (python-tests-look-at "'#this is a string'")
1863 (should (not (python-info-current-line-comment-p)))
1864 (python-tests-look-at "# more comments")
1865 (should (python-info-current-line-comment-p))
1866 (python-tests-look-at "print ('bar') # print bar")
1867 (should (not (python-info-current-line-comment-p)))))
1869 (ert-deftest python-info-current-line-empty-p ()
1870 (python-tests-with-temp-buffer
1874 foo = True # another comment
1876 (should (python-info-current-line-empty-p))
1877 (python-tests-look-at "# this is a comment")
1878 (should (not (python-info-current-line-empty-p)))
1880 (should (python-info-current-line-empty-p))))
1883 ;;; Utility functions
1885 (ert-deftest python-util-goto-line-1 ()
1886 (python-tests-with-temp-buffer
1891 pass" (make-string 20 ?\n))
1892 (python-util-goto-line 10)
1893 (should (= (line-number-at-pos) 10))
1894 (python-util-goto-line 20)
1895 (should (= (line-number-at-pos) 20))))
1897 (ert-deftest python-util-clone-local-variables-1 ()
1898 (let ((buffer (generate-new-buffer
1899 "python-util-clone-local-variables-1"))
1901 '((python-fill-docstring-style . django)
1902 (python-shell-interpreter . "python")
1903 (python-shell-interpreter-args . "manage.py shell")
1904 (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
1905 (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
1906 (python-shell-extra-pythonpaths "/home/user/pylib/")
1907 (python-shell-completion-setup-code
1908 . "from IPython.core.completerlib import module_completion")
1909 (python-shell-completion-module-string-code
1910 . "';'.join(module_completion('''%s'''))\n")
1911 (python-shell-completion-string-code
1912 . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
1913 (python-shell-virtualenv-path
1914 . "/home/user/.virtualenvs/project"))))
1915 (with-current-buffer buffer
1916 (kill-all-local-variables)
1917 (dolist (ccons varcons)
1918 (set (make-local-variable (car ccons)) (cdr ccons))))
1919 (python-tests-with-temp-buffer
1921 (python-util-clone-local-variables buffer)
1922 (dolist (ccons varcons)
1924 (equal (symbol-value (car ccons)) (cdr ccons)))))
1925 (kill-buffer buffer)))
1927 (ert-deftest python-util-forward-comment-1 ()
1928 (python-tests-with-temp-buffer
1932 # bad indented comment
1933 # more comments" (make-string 9999 ?\n))
1934 (python-util-forward-comment 1)
1935 (should (= (point) (point-max)))
1936 (python-util-forward-comment -1)
1937 (should (= (point) (point-min)))))
1940 (provide 'python-tests)
1944 ;; indent-tabs-mode: nil
1947 ;;; python-tests.el ends here