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/>.
27 (defmacro python-tests-with-temp-buffer
(contents &rest body
)
28 "Create a `python-mode' enabled temp buffer with CONTENTS.
29 BODY is code to be executed within the temp buffer. Point is
30 always located at the beginning of buffer."
31 (declare (indent 1) (debug t
))
35 (goto-char (point-min))
38 (defmacro python-tests-with-temp-file
(contents &rest body
)
39 "Create a `python-mode' enabled file with CONTENTS.
40 BODY is code to be executed within the temp buffer. Point is
41 always located at the beginning of buffer."
42 (declare (indent 1) (debug t
))
43 ;; temp-file never actually used for anything?
44 `(let* ((temp-file (make-temp-file "python-tests" nil
".py"))
45 (buffer (find-file-noselect temp-file
)))
47 (with-current-buffer buffer
50 (goto-char (point-min))
52 (and buffer
(kill-buffer buffer
))
53 (delete-file temp-file
))))
55 (defun python-tests-look-at (string &optional num restore-point
)
56 "Move point at beginning of STRING in the current buffer.
57 Optional argument NUM defaults to 1 and is an integer indicating
58 how many occurrences must be found, when positive the search is
59 done forwards, otherwise backwards. When RESTORE-POINT is
60 non-nil the point is not moved but the position found is still
61 returned. When searching forward and point is already looking at
62 STRING, it is skipped so the next STRING occurrence is selected."
63 (let* ((num (or num
1))
64 (starting-point (point))
65 (string (regexp-quote string
))
66 (search-fn (if (> num
0) #'re-search-forward
#'re-search-backward
))
67 (deinc-fn (if (> num
0) #'1-
#'1+))
71 (while (not (= num
0))
74 ;; Moving forward and already looking at STRING, skip it.
75 (forward-char (length (match-string-no-properties 0))))
76 (and (not (funcall search-fn string nil t
))
79 ;; `re-search-forward' leaves point at the end of the
80 ;; occurrence, move back so point is at the beginning
82 (forward-char (- (length (match-string-no-properties 0)))))
84 num
(funcall deinc-fn num
)
85 found-point
(point))))
87 (and restore-point
(goto-char starting-point
)))))
90 ;;; Tests for your tests, so you can test while you test.
92 (ert-deftest python-tests-look-at-1
()
93 "Test forward movement."
94 (python-tests-with-temp-buffer
95 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
96 sed do eiusmod tempor incididunt ut labore et dolore magna
98 (let ((expected (save-excursion
100 (re-search-forward "et" nil t
))
103 (should (= (python-tests-look-at "et" 3 t
) expected
))
104 ;; Even if NUM is bigger than found occurrences the point of last
105 ;; one should be returned.
106 (should (= (python-tests-look-at "et" 6 t
) expected
))
107 ;; If already looking at STRING, it should skip it.
108 (dotimes (i 2) (re-search-forward "et"))
110 (should (= (python-tests-look-at "et") expected
)))))
112 (ert-deftest python-tests-look-at-2
()
113 "Test backward movement."
114 (python-tests-with-temp-buffer
115 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
116 sed do eiusmod tempor incididunt ut labore et dolore magna
120 (re-search-forward "et" nil t
)
124 (re-search-forward "et" nil t
))
125 (should (= (python-tests-look-at "et" -
3 t
) expected
))
126 (should (= (python-tests-look-at "et" -
6 t
) expected
)))))
132 ;;; Python specialized rx
135 ;;; Font-lock and syntax
140 ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
142 (ert-deftest python-indent-pep8-1
()
144 (python-tests-with-temp-buffer
145 "# Aligned with opening delimiter
146 foo = long_function_name(var_one, var_two,
149 (should (eq (car (python-indent-context)) 'no-indent
))
150 (should (= (python-indent-calculate-indentation) 0))
151 (python-tests-look-at "foo = long_function_name(var_one, var_two,")
152 (should (eq (car (python-indent-context)) 'after-line
))
153 (should (= (python-indent-calculate-indentation) 0))
154 (python-tests-look-at "var_three, var_four)")
155 (should (eq (car (python-indent-context)) 'inside-paren
))
156 (should (= (python-indent-calculate-indentation) 25))))
158 (ert-deftest python-indent-pep8-2
()
160 (python-tests-with-temp-buffer
161 "# More indentation included to distinguish this from the rest.
162 def long_function_name(
163 var_one, var_two, var_three,
167 (should (eq (car (python-indent-context)) 'no-indent
))
168 (should (= (python-indent-calculate-indentation) 0))
169 (python-tests-look-at "def long_function_name(")
170 (should (eq (car (python-indent-context)) 'after-line
))
171 (should (= (python-indent-calculate-indentation) 0))
172 (python-tests-look-at "var_one, var_two, var_three,")
173 (should (eq (car (python-indent-context)) 'inside-paren
))
174 (should (= (python-indent-calculate-indentation) 8))
175 (python-tests-look-at "var_four):")
176 (should (eq (car (python-indent-context)) 'inside-paren
))
177 (should (= (python-indent-calculate-indentation) 8))
178 (python-tests-look-at "print (var_one)")
179 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
180 (should (= (python-indent-calculate-indentation) 4))))
182 (ert-deftest python-indent-pep8-3
()
184 (python-tests-with-temp-buffer
185 "# Extra indentation is not necessary.
186 foo = long_function_name(
190 (should (eq (car (python-indent-context)) 'no-indent
))
191 (should (= (python-indent-calculate-indentation) 0))
192 (python-tests-look-at "foo = long_function_name(")
193 (should (eq (car (python-indent-context)) 'after-line
))
194 (should (= (python-indent-calculate-indentation) 0))
195 (python-tests-look-at "var_one, var_two,")
196 (should (eq (car (python-indent-context)) 'inside-paren
))
197 (should (= (python-indent-calculate-indentation) 4))
198 (python-tests-look-at "var_three, var_four)")
199 (should (eq (car (python-indent-context)) 'inside-paren
))
200 (should (= (python-indent-calculate-indentation) 4))))
202 (ert-deftest python-indent-after-comment-1
()
203 "The most simple after-comment case that shouldn't fail."
204 (python-tests-with-temp-buffer
205 "# Contents will be modified to correct indentation
207 def _on_child_complete(self, child_future):
208 if self.in_terminal_state():
210 # We only complete when all our async children have entered a
211 # terminal state. At that point, if any child failed, we fail
212 # with the exception with which the first child failed.
214 (python-tests-look-at "# We only complete")
215 (should (eq (car (python-indent-context)) 'after-line
))
216 (should (= (python-indent-calculate-indentation) 8))
217 (python-tests-look-at "# terminal state")
218 (should (eq (car (python-indent-context)) 'after-comment
))
219 (should (= (python-indent-calculate-indentation) 8))
220 (python-tests-look-at "# with the exception")
221 (should (eq (car (python-indent-context)) 'after-comment
))
222 ;; This one indents relative to previous block, even given the fact
223 ;; that it was under-indented.
224 (should (= (python-indent-calculate-indentation) 4))
225 (python-tests-look-at "# terminal state" -
1)
226 ;; It doesn't hurt to check again.
227 (should (eq (car (python-indent-context)) 'after-comment
))
229 (should (= (current-indentation) 8))
230 (python-tests-look-at "# with the exception")
231 (should (eq (car (python-indent-context)) 'after-comment
))
232 ;; Now everything should be lined up.
233 (should (= (python-indent-calculate-indentation) 8))))
235 (ert-deftest python-indent-after-comment-2
()
236 "Test after-comment in weird cases."
237 (python-tests-with-temp-buffer
238 "# Contents will be modified to correct indentation
242 # This comment is badly indented just because.
243 # But we won't mess with the user in this line.
245 now_we_do_mess_cause_this_is_not_a_comment = 1
249 (python-tests-look-at "# I don't do much")
250 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
251 (should (= (python-indent-calculate-indentation) 4))
252 (python-tests-look-at "return arg")
253 ;; Comment here just gets ignored, this line is not a comment so
254 ;; the rules won't apply here.
255 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
256 (should (= (python-indent-calculate-indentation) 4))
257 (python-tests-look-at "# This comment is badly")
258 (should (eq (car (python-indent-context)) 'after-line
))
259 ;; The return keyword moves indentation backwards 4 spaces, but
260 ;; let's assume this comment was placed there because the user
261 ;; wanted to (manually adding spaces or whatever).
262 (should (= (python-indent-calculate-indentation) 0))
263 (python-tests-look-at "# but we won't mess")
264 (should (eq (car (python-indent-context)) 'after-comment
))
265 (should (= (python-indent-calculate-indentation) 4))
266 ;; Behave the same for blank lines: potentially a comment.
268 (should (eq (car (python-indent-context)) 'after-comment
))
269 (should (= (python-indent-calculate-indentation) 4))
270 (python-tests-look-at "now_we_do_mess")
271 ;; Here is where comment indentation starts to get ignored and
272 ;; where the user can't freely indent anymore.
273 (should (eq (car (python-indent-context)) 'after-line
))
274 (should (= (python-indent-calculate-indentation) 0))
275 (python-tests-look-at "# yeah, that.")
276 (should (eq (car (python-indent-context)) 'after-line
))
277 (should (= (python-indent-calculate-indentation) 0))))
279 (ert-deftest python-indent-inside-paren-1
()
280 "The most simple inside-paren case that shouldn't fail."
281 (python-tests-with-temp-buffer
299 (python-tests-look-at "data = {")
300 (should (eq (car (python-indent-context)) 'after-line
))
301 (should (= (python-indent-calculate-indentation) 0))
302 (python-tests-look-at "'key':")
303 (should (eq (car (python-indent-context)) 'inside-paren
))
304 (should (= (python-indent-calculate-indentation) 4))
305 (python-tests-look-at "{")
306 (should (eq (car (python-indent-context)) 'inside-paren
))
307 (should (= (python-indent-calculate-indentation) 4))
308 (python-tests-look-at "'objlist': [")
309 (should (eq (car (python-indent-context)) 'inside-paren
))
310 (should (= (python-indent-calculate-indentation) 8))
311 (python-tests-look-at "{")
312 (should (eq (car (python-indent-context)) 'inside-paren
))
313 (should (= (python-indent-calculate-indentation) 12))
314 (python-tests-look-at "'pk': 1,")
315 (should (eq (car (python-indent-context)) 'inside-paren
))
316 (should (= (python-indent-calculate-indentation) 16))
317 (python-tests-look-at "'name': 'first',")
318 (should (eq (car (python-indent-context)) 'inside-paren
))
319 (should (= (python-indent-calculate-indentation) 16))
320 (python-tests-look-at "},")
321 (should (eq (car (python-indent-context)) 'inside-paren
))
322 (should (= (python-indent-calculate-indentation) 12))
323 (python-tests-look-at "{")
324 (should (eq (car (python-indent-context)) 'inside-paren
))
325 (should (= (python-indent-calculate-indentation) 12))
326 (python-tests-look-at "'pk': 2,")
327 (should (eq (car (python-indent-context)) 'inside-paren
))
328 (should (= (python-indent-calculate-indentation) 16))
329 (python-tests-look-at "'name': 'second',")
330 (should (eq (car (python-indent-context)) 'inside-paren
))
331 (should (= (python-indent-calculate-indentation) 16))
332 (python-tests-look-at "}")
333 (should (eq (car (python-indent-context)) 'inside-paren
))
334 (should (= (python-indent-calculate-indentation) 12))
335 (python-tests-look-at "]")
336 (should (eq (car (python-indent-context)) 'inside-paren
))
337 (should (= (python-indent-calculate-indentation) 8))
338 (python-tests-look-at "}")
339 (should (eq (car (python-indent-context)) 'inside-paren
))
340 (should (= (python-indent-calculate-indentation) 4))
341 (python-tests-look-at "}")
342 (should (eq (car (python-indent-context)) 'inside-paren
))
343 (should (= (python-indent-calculate-indentation) 0))))
345 (ert-deftest python-indent-inside-paren-2
()
346 "Another more compact paren group style."
347 (python-tests-with-temp-buffer
358 (python-tests-look-at "data = {")
359 (should (eq (car (python-indent-context)) 'after-line
))
360 (should (= (python-indent-calculate-indentation) 0))
361 (python-tests-look-at "'objlist': [")
362 (should (eq (car (python-indent-context)) 'inside-paren
))
363 (should (= (python-indent-calculate-indentation) 4))
364 (python-tests-look-at "{'pk': 1,")
365 (should (eq (car (python-indent-context)) 'inside-paren
))
366 (should (= (python-indent-calculate-indentation) 8))
367 (python-tests-look-at "'name': 'first'},")
368 (should (eq (car (python-indent-context)) 'inside-paren
))
369 (should (= (python-indent-calculate-indentation) 9))
370 (python-tests-look-at "{'pk': 2,")
371 (should (eq (car (python-indent-context)) 'inside-paren
))
372 (should (= (python-indent-calculate-indentation) 8))
373 (python-tests-look-at "'name': 'second'}")
374 (should (eq (car (python-indent-context)) 'inside-paren
))
375 (should (= (python-indent-calculate-indentation) 9))
376 (python-tests-look-at "]")
377 (should (eq (car (python-indent-context)) 'inside-paren
))
378 (should (= (python-indent-calculate-indentation) 4))
379 (python-tests-look-at "}}")
380 (should (eq (car (python-indent-context)) 'inside-paren
))
381 (should (= (python-indent-calculate-indentation) 0))
382 (python-tests-look-at "}")
383 (should (eq (car (python-indent-context)) 'inside-paren
))
384 (should (= (python-indent-calculate-indentation) 0))))
386 (ert-deftest python-indent-after-block-1
()
387 "The most simple after-block case that shouldn't fail."
388 (python-tests-with-temp-buffer
390 def foo(a, b, c=True):
392 (should (eq (car (python-indent-context)) 'no-indent
))
393 (should (= (python-indent-calculate-indentation) 0))
394 (goto-char (point-max))
395 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
396 (should (= (python-indent-calculate-indentation) 4))))
398 (ert-deftest python-indent-after-block-2
()
399 "A weird (malformed) multiline block statement."
400 (python-tests-with-temp-buffer
406 (goto-char (point-max))
407 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
408 (should (= (python-indent-calculate-indentation) 4))))
410 (ert-deftest python-indent-dedenters-1
()
411 "Check all dedenters."
412 (python-tests-with-temp-buffer
422 except (IndexError, AttributeError):
425 print ('nor a, nor b are true')
427 (python-tests-look-at "if a:")
428 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
429 (should (= (python-indent-calculate-indentation) 4))
430 (python-tests-look-at "print (a)")
431 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
432 (should (= (python-indent-calculate-indentation) 8))
433 (python-tests-look-at "elif b:")
434 (should (eq (car (python-indent-context)) 'after-line
))
435 (should (= (python-indent-calculate-indentation) 4))
436 (python-tests-look-at "print (b)")
437 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
438 (should (= (python-indent-calculate-indentation) 8))
439 (python-tests-look-at "else:")
440 (should (eq (car (python-indent-context)) 'after-line
))
441 (should (= (python-indent-calculate-indentation) 4))
442 (python-tests-look-at "try:")
443 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
444 (should (= (python-indent-calculate-indentation) 8))
445 (python-tests-look-at "print (c.pop())")
446 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
447 (should (= (python-indent-calculate-indentation) 12))
448 (python-tests-look-at "except (IndexError, AttributeError):")
449 (should (eq (car (python-indent-context)) 'after-line
))
450 (should (= (python-indent-calculate-indentation) 8))
451 (python-tests-look-at "print (c)")
452 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
453 (should (= (python-indent-calculate-indentation) 12))
454 (python-tests-look-at "finally:")
455 (should (eq (car (python-indent-context)) 'after-line
))
456 (should (= (python-indent-calculate-indentation) 8))
457 (python-tests-look-at "print ('nor a, nor b are true')")
458 (should (eq (car (python-indent-context)) 'after-beginning-of-block
))
459 (should (= (python-indent-calculate-indentation) 12))))
461 (ert-deftest python-indent-dedenters-2
()
462 "Check one-liner block special case.."
463 (python-tests-with-temp-buffer
468 if cond: print 'True'
474 (python-tests-look-at "else: print 'False'")
475 ;; When a block has code after ":" it's just considered a simple
476 ;; line as that's a common thing to happen in one-liners.
477 (should (eq (car (python-indent-context)) 'after-line
))
478 (should (= (python-indent-calculate-indentation) 4))
479 (python-tests-look-at "else:")
480 (should (eq (car (python-indent-context)) 'after-line
))
481 (should (= (python-indent-calculate-indentation) 0))))
483 (ert-deftest python-indent-after-backslash-1
()
484 "The most common case."
485 (python-tests-with-temp-buffer
487 from foo.bar.baz import something, something_1 \\\\
488 something_2 something_3, \\\\
489 something_4, something_5
491 (python-tests-look-at "from foo.bar.baz import something, something_1")
492 (should (eq (car (python-indent-context)) 'after-line
))
493 (should (= (python-indent-calculate-indentation) 0))
494 (python-tests-look-at "something_2 something_3,")
495 (should (eq (car (python-indent-context)) 'after-backslash
))
496 (should (= (python-indent-calculate-indentation) 4))
497 (python-tests-look-at "something_4, something_5")
498 (should (eq (car (python-indent-context)) 'after-backslash
))
499 (should (= (python-indent-calculate-indentation) 4))
500 (goto-char (point-max))
501 (should (eq (car (python-indent-context)) 'after-line
))
502 (should (= (python-indent-calculate-indentation) 0))))
504 (ert-deftest python-indent-after-backslash-2
()
505 "A pretty extreme complicated case."
506 (python-tests-with-temp-buffer
508 objects = Thing.objects.all() \\\\
518 (python-tests-look-at "objects = Thing.objects.all()")
519 (should (eq (car (python-indent-context)) 'after-line
))
520 (should (= (python-indent-calculate-indentation) 0))
521 (python-tests-look-at ".filter(")
522 (should (eq (car (python-indent-context)) 'after-backslash
))
523 (should (= (python-indent-calculate-indentation) 23))
524 (python-tests-look-at "type='toy',")
525 (should (eq (car (python-indent-context)) 'inside-paren
))
526 (should (= (python-indent-calculate-indentation) 27))
527 (python-tests-look-at "status='bought'")
528 (should (eq (car (python-indent-context)) 'inside-paren
))
529 (should (= (python-indent-calculate-indentation) 27))
530 (python-tests-look-at ") \\\\")
531 (should (eq (car (python-indent-context)) 'inside-paren
))
532 (should (= (python-indent-calculate-indentation) 23))
533 (python-tests-look-at ".aggregate(")
534 (should (eq (car (python-indent-context)) 'after-backslash
))
535 (should (= (python-indent-calculate-indentation) 23))
536 (python-tests-look-at "Sum('amount')")
537 (should (eq (car (python-indent-context)) 'inside-paren
))
538 (should (= (python-indent-calculate-indentation) 27))
539 (python-tests-look-at ") \\\\")
540 (should (eq (car (python-indent-context)) 'inside-paren
))
541 (should (= (python-indent-calculate-indentation) 23))
542 (python-tests-look-at ".values_list()")
543 (should (eq (car (python-indent-context)) 'after-backslash
))
544 (should (= (python-indent-calculate-indentation) 23))
546 (should (eq (car (python-indent-context)) 'after-line
))
547 (should (= (python-indent-calculate-indentation) 0))))
549 (ert-deftest python-indent-block-enders-1
()
550 "Test `python-indent-block-enders' value honoring."
551 (python-tests-with-temp-buffer
564 (python-tests-look-at "3)")
566 (should (= (python-indent-calculate-indentation) 8))
567 (python-tests-look-at "pass")
569 (should (= (python-indent-calculate-indentation) 8))))
571 (ert-deftest python-indent-block-enders-2
()
572 "Test `python-indent-block-enders' value honoring."
573 (python-tests-with-temp-buffer
576 '''raise lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
578 eiusmod tempor incididunt ut labore et dolore magna aliqua.
581 \"return (1, 2, 3).\"
587 (python-tests-look-at "def")
588 (should (= (python-indent-calculate-indentation) 4))
589 (python-tests-look-at "if")
590 (should (= (python-indent-calculate-indentation) 8))))
595 (ert-deftest python-nav-beginning-of-defun-1
()
596 (python-tests-with-temp-buffer
598 def decoratorFunctionWithArguments(arg1, arg2, arg3):
599 '''print decorated function call data to stdout.
603 @decoratorFunctionWithArguments('arg1', 'arg2')
604 def func(a, b, c=True):
609 print 'Inside wwrap()'
610 def wrapped_f(*args):
611 print 'Inside wrapped_f()'
612 print 'Decorator arguments:', arg1, arg2, arg3
614 print 'After f(*args)'
618 (python-tests-look-at "return wrap")
619 (should (= (save-excursion
620 (python-nav-beginning-of-defun)
623 (python-tests-look-at "def wrapped_f(*args):" -
1)
626 (python-tests-look-at "def wrapped_f(*args):" -
1)
627 (should (= (save-excursion
628 (python-nav-beginning-of-defun)
631 (python-tests-look-at "def wwrap(f):" -
1)
634 (python-tests-look-at "def wwrap(f):" -
1)
635 (should (= (save-excursion
636 (python-nav-beginning-of-defun)
639 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)
643 (ert-deftest python-nav-beginning-of-defun-2
()
644 (python-tests-with-temp-buffer
660 ;; Nested defuns, are handled with care.
661 (python-tests-look-at "def c(self):")
662 (should (= (save-excursion
663 (python-nav-beginning-of-defun)
666 (python-tests-look-at "def m(self):" -
1)
669 ;; Defuns on same levels should be respected.
670 (python-tests-look-at "def a():" -
1)
671 (should (= (save-excursion
672 (python-nav-beginning-of-defun)
675 (python-tests-look-at "def b():" -
1)
678 ;; Jump to a top level defun.
679 (python-tests-look-at "def b():" -
1)
680 (should (= (save-excursion
681 (python-nav-beginning-of-defun)
684 (python-tests-look-at "def m(self):" -
1)
687 ;; Jump to a top level defun again.
688 (python-tests-look-at "def m(self):" -
1)
689 (should (= (save-excursion
690 (python-nav-beginning-of-defun)
693 (python-tests-look-at "class C(object):" -
1)
697 (ert-deftest python-nav-end-of-defun-1
()
698 (python-tests-with-temp-buffer
714 (should (= (save-excursion
715 (python-tests-look-at "class C(object):")
716 (python-nav-end-of-defun)
720 (should (= (save-excursion
721 (python-tests-look-at "def m(self):")
722 (python-nav-end-of-defun)
725 (python-tests-look-at "def c(self):")
728 (should (= (save-excursion
729 (python-tests-look-at "def b():")
730 (python-nav-end-of-defun)
733 (python-tests-look-at "def b():")
736 (should (= (save-excursion
737 (python-tests-look-at "def c(self):")
738 (python-nav-end-of-defun)
743 (ert-deftest python-nav-end-of-defun-2
()
744 (python-tests-with-temp-buffer
746 def decoratorFunctionWithArguments(arg1, arg2, arg3):
747 '''print decorated function call data to stdout.
751 @decoratorFunctionWithArguments('arg1', 'arg2')
752 def func(a, b, c=True):
757 print 'Inside wwrap()'
758 def wrapped_f(*args):
759 print 'Inside wrapped_f()'
760 print 'Decorator arguments:', arg1, arg2, arg3
762 print 'After f(*args)'
766 (should (= (save-excursion
767 (python-tests-look-at "def decoratorFunctionWithArguments")
768 (python-nav-end-of-defun)
772 (should (= (save-excursion
773 (python-tests-look-at "@decoratorFunctionWithArguments")
774 (python-nav-end-of-defun)
778 (should (= (save-excursion
779 (python-tests-look-at "def wwrap(f):")
780 (python-nav-end-of-defun)
783 (python-tests-look-at "return wwrap")
784 (line-beginning-position))))
785 (should (= (save-excursion
786 (python-tests-look-at "def wrapped_f(*args):")
787 (python-nav-end-of-defun)
790 (python-tests-look-at "return wrapped_f")
791 (line-beginning-position))))
792 (should (= (save-excursion
793 (python-tests-look-at "f(*args)")
794 (python-nav-end-of-defun)
797 (python-tests-look-at "return wrapped_f")
798 (line-beginning-position))))))
800 (ert-deftest python-nav-backward-defun-1
()
801 (python-tests-with-temp-buffer
827 (goto-char (point-max))
828 (should (= (save-excursion (python-nav-backward-defun))
829 (python-tests-look-at " def c(self): # c" -
1)))
830 (should (= (save-excursion (python-nav-backward-defun))
831 (python-tests-look-at " def d(self): # d" -
1)))
832 (should (= (save-excursion (python-nav-backward-defun))
833 (python-tests-look-at " class C(object): # C" -
1)))
834 (should (= (save-excursion (python-nav-backward-defun))
835 (python-tests-look-at " class B(object): # B" -
1)))
836 (should (= (save-excursion (python-nav-backward-defun))
837 (python-tests-look-at " def b(self): # b" -
1)))
838 (should (= (save-excursion (python-nav-backward-defun))
839 (python-tests-look-at " def a(self): # a" -
1)))
840 (should (= (save-excursion (python-nav-backward-defun))
841 (python-tests-look-at "class A(object): # A" -
1)))
842 (should (not (python-nav-backward-defun)))))
844 (ert-deftest python-nav-backward-defun-2
()
845 (python-tests-with-temp-buffer
847 def decoratorFunctionWithArguments(arg1, arg2, arg3):
848 '''print decorated function call data to stdout.
852 @decoratorFunctionWithArguments('arg1', 'arg2')
853 def func(a, b, c=True):
858 print 'Inside wwrap()'
859 def wrapped_f(*args):
860 print 'Inside wrapped_f()'
861 print 'Decorator arguments:', arg1, arg2, arg3
863 print 'After f(*args)'
867 (goto-char (point-max))
868 (should (= (save-excursion (python-nav-backward-defun))
869 (python-tests-look-at " def wrapped_f(*args):" -
1)))
870 (should (= (save-excursion (python-nav-backward-defun))
871 (python-tests-look-at " def wwrap(f):" -
1)))
872 (should (= (save-excursion (python-nav-backward-defun))
873 (python-tests-look-at "def decoratorFunctionWithArguments(arg1, arg2, arg3):" -
1)))
874 (should (not (python-nav-backward-defun)))))
876 (ert-deftest python-nav-backward-defun-3
()
877 (python-tests-with-temp-buffer
893 (goto-char (point-min))
894 (let ((point (python-tests-look-at "class A(object):")))
895 (should (not (python-nav-backward-defun)))
896 (should (= point
(point))))))
898 (ert-deftest python-nav-forward-defun-1
()
899 (python-tests-with-temp-buffer
925 (goto-char (point-min))
926 (should (= (save-excursion (python-nav-forward-defun))
927 (python-tests-look-at "(object): # A")))
928 (should (= (save-excursion (python-nav-forward-defun))
929 (python-tests-look-at "(self): # a")))
930 (should (= (save-excursion (python-nav-forward-defun))
931 (python-tests-look-at "(self): # b")))
932 (should (= (save-excursion (python-nav-forward-defun))
933 (python-tests-look-at "(object): # B")))
934 (should (= (save-excursion (python-nav-forward-defun))
935 (python-tests-look-at "(object): # C")))
936 (should (= (save-excursion (python-nav-forward-defun))
937 (python-tests-look-at "(self): # d")))
938 (should (= (save-excursion (python-nav-forward-defun))
939 (python-tests-look-at "(self): # c")))
940 (should (not (python-nav-forward-defun)))))
942 (ert-deftest python-nav-forward-defun-2
()
943 (python-tests-with-temp-buffer
945 def decoratorFunctionWithArguments(arg1, arg2, arg3):
946 '''print decorated function call data to stdout.
950 @decoratorFunctionWithArguments('arg1', 'arg2')
951 def func(a, b, c=True):
956 print 'Inside wwrap()'
957 def wrapped_f(*args):
958 print 'Inside wrapped_f()'
959 print 'Decorator arguments:', arg1, arg2, arg3
961 print 'After f(*args)'
965 (goto-char (point-min))
966 (should (= (save-excursion (python-nav-forward-defun))
967 (python-tests-look-at "(arg1, arg2, arg3):")))
968 (should (= (save-excursion (python-nav-forward-defun))
969 (python-tests-look-at "(f):")))
970 (should (= (save-excursion (python-nav-forward-defun))
971 (python-tests-look-at "(*args):")))
972 (should (not (python-nav-forward-defun)))))
974 (ert-deftest python-nav-forward-defun-3
()
975 (python-tests-with-temp-buffer
991 (goto-char (point-min))
992 (let ((point (python-tests-look-at "(object):")))
993 (should (not (python-nav-forward-defun)))
994 (should (= point
(point))))))
996 (ert-deftest python-nav-beginning-of-statement-1
()
997 (python-tests-with-temp-buffer
1007 v3 = ('this is a string'
1012 # this is a comment, yo
1013 'continue previous line')
1019 (python-tests-look-at "v2 =")
1020 (python-util-forward-comment -
1)
1021 (should (= (save-excursion
1022 (python-nav-beginning-of-statement)
1024 (python-tests-look-at "v1 =" -
1 t
)))
1025 (python-tests-look-at "v3 =")
1026 (python-util-forward-comment -
1)
1027 (should (= (save-excursion
1028 (python-nav-beginning-of-statement)
1030 (python-tests-look-at "v2 =" -
1 t
)))
1031 (python-tests-look-at "v4 =")
1032 (python-util-forward-comment -
1)
1033 (should (= (save-excursion
1034 (python-nav-beginning-of-statement)
1036 (python-tests-look-at "v3 =" -
1 t
)))
1037 (goto-char (point-max))
1038 (python-util-forward-comment -
1)
1039 (should (= (save-excursion
1040 (python-nav-beginning-of-statement)
1042 (python-tests-look-at "v4 =" -
1 t
)))))
1044 (ert-deftest python-nav-end-of-statement-1
()
1045 (python-tests-with-temp-buffer
1055 v3 = ('this is a string'
1060 # this is a comment, yo
1061 'continue previous line')
1067 (python-tests-look-at "v1 =")
1068 (should (= (save-excursion
1069 (python-nav-end-of-statement)
1072 (python-tests-look-at "789")
1073 (line-end-position))))
1074 (python-tests-look-at "v2 =")
1075 (should (= (save-excursion
1076 (python-nav-end-of-statement)
1079 (python-tests-look-at "value4)")
1080 (line-end-position))))
1081 (python-tests-look-at "v3 =")
1082 (should (= (save-excursion
1083 (python-nav-end-of-statement)
1086 (python-tests-look-at
1087 "'continue previous line')")
1088 (line-end-position))))
1089 (python-tests-look-at "v4 =")
1090 (should (= (save-excursion
1091 (python-nav-end-of-statement)
1094 (goto-char (point-max))
1095 (python-util-forward-comment -
1)
1098 (ert-deftest python-nav-forward-statement-1
()
1099 (python-tests-with-temp-buffer
1109 v3 = ('this is a string'
1114 # this is a comment, yo
1115 'continue previous line')
1121 (python-tests-look-at "v1 =")
1122 (should (= (save-excursion
1123 (python-nav-forward-statement)
1125 (python-tests-look-at "v2 =")))
1126 (should (= (save-excursion
1127 (python-nav-forward-statement)
1129 (python-tests-look-at "v3 =")))
1130 (should (= (save-excursion
1131 (python-nav-forward-statement)
1133 (python-tests-look-at "v4 =")))
1134 (should (= (save-excursion
1135 (python-nav-forward-statement)
1139 (ert-deftest python-nav-backward-statement-1
()
1140 (python-tests-with-temp-buffer
1150 v3 = ('this is a string'
1155 # this is a comment, yo
1156 'continue previous line')
1162 (goto-char (point-max))
1163 (should (= (save-excursion
1164 (python-nav-backward-statement)
1166 (python-tests-look-at "v4 =" -
1)))
1167 (should (= (save-excursion
1168 (python-nav-backward-statement)
1170 (python-tests-look-at "v3 =" -
1)))
1171 (should (= (save-excursion
1172 (python-nav-backward-statement)
1174 (python-tests-look-at "v2 =" -
1)))
1175 (should (= (save-excursion
1176 (python-nav-backward-statement)
1178 (python-tests-look-at "v1 =" -
1)))))
1180 (ert-deftest python-nav-backward-statement-2
()
1181 :expected-result
:failed
1182 (python-tests-with-temp-buffer
1193 ;; FIXME: For some reason `python-nav-backward-statement' is moving
1194 ;; back two sentences when starting from 'value4)'.
1195 (goto-char (point-max))
1196 (python-util-forward-comment -
1)
1197 (should (= (save-excursion
1198 (python-nav-backward-statement)
1200 (python-tests-look-at "v2 =" -
1 t
)))))
1202 (ert-deftest python-nav-beginning-of-block-1
()
1203 (python-tests-with-temp-buffer
1205 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1206 '''print decorated function call data to stdout.
1210 @decoratorFunctionWithArguments('arg1', 'arg2')
1211 def func(a, b, c=True):
1216 print 'Inside wwrap()'
1217 def wrapped_f(*args):
1218 print 'Inside wrapped_f()'
1219 print 'Decorator arguments:', arg1, arg2, arg3
1221 print 'After f(*args)'
1225 (python-tests-look-at "return wwrap")
1226 (should (= (save-excursion
1227 (python-nav-beginning-of-block)
1229 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)))
1230 (python-tests-look-at "print 'Inside wwrap()'")
1231 (should (= (save-excursion
1232 (python-nav-beginning-of-block)
1234 (python-tests-look-at "def wwrap(f):" -
1)))
1235 (python-tests-look-at "print 'After f(*args)'")
1237 (should (= (save-excursion
1238 (python-nav-beginning-of-block)
1240 (python-tests-look-at "def wrapped_f(*args):" -
1)))
1241 (python-tests-look-at "return wrapped_f")
1242 (should (= (save-excursion
1243 (python-nav-beginning-of-block)
1245 (python-tests-look-at "def wwrap(f):" -
1)))))
1247 (ert-deftest python-nav-end-of-block-1
()
1248 (python-tests-with-temp-buffer
1250 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1251 '''print decorated function call data to stdout.
1255 @decoratorFunctionWithArguments('arg1', 'arg2')
1256 def func(a, b, c=True):
1261 print 'Inside wwrap()'
1262 def wrapped_f(*args):
1263 print 'Inside wrapped_f()'
1264 print 'Decorator arguments:', arg1, arg2, arg3
1266 print 'After f(*args)'
1270 (python-tests-look-at "def decoratorFunctionWithArguments")
1271 (should (= (save-excursion
1272 (python-nav-end-of-block)
1275 (goto-char (point-max))
1276 (python-util-forward-comment -
1)
1278 (python-tests-look-at "def wwrap(f):")
1279 (should (= (save-excursion
1280 (python-nav-end-of-block)
1283 (python-tests-look-at "return wrapped_f")
1284 (line-end-position))))
1286 (should (= (save-excursion
1287 (python-nav-end-of-block)
1290 (python-tests-look-at "return wrapped_f")
1291 (line-end-position))))
1292 (python-tests-look-at "f(*args)")
1293 (should (= (save-excursion
1294 (python-nav-end-of-block)
1297 (python-tests-look-at "print 'After f(*args)'")
1298 (line-end-position))))))
1300 (ert-deftest python-nav-forward-block-1
()
1301 "This also accounts as a test for `python-nav-backward-block'."
1302 (python-tests-with-temp-buffer
1304 if request.user.is_authenticated():
1308 profile = request.user.get_profile()
1309 except Profile.DoesNotExist:
1310 profile = Profile.objects.create(user=request.user)
1313 profile.recalculate_stats()
1315 profile.clear_stats()
1320 (should (= (save-excursion (python-nav-forward-block))
1321 (python-tests-look-at "if request.user.is_authenticated():")))
1322 (should (= (save-excursion (python-nav-forward-block))
1323 (python-tests-look-at "try:")))
1324 (should (= (save-excursion (python-nav-forward-block))
1325 (python-tests-look-at "except Profile.DoesNotExist:")))
1326 (should (= (save-excursion (python-nav-forward-block))
1327 (python-tests-look-at "else:")))
1328 (should (= (save-excursion (python-nav-forward-block))
1329 (python-tests-look-at "if profile.stats:")))
1330 (should (= (save-excursion (python-nav-forward-block))
1331 (python-tests-look-at "else:")))
1332 (should (= (save-excursion (python-nav-forward-block))
1333 (python-tests-look-at "finally:")))
1334 ;; When point is at the last block, leave it there and return nil
1335 (should (not (save-excursion (python-nav-forward-block))))
1336 ;; Move backwards, and even if the number of moves is less than the
1337 ;; provided argument return the point.
1338 (should (= (save-excursion (python-nav-forward-block -
10))
1339 (python-tests-look-at
1340 "if request.user.is_authenticated():" -
1)))))
1342 (ert-deftest python-nav-lisp-forward-sexp-safe-1
()
1343 (python-tests-with-temp-buffer
1345 profile = Profile.objects.create(user=request.user)
1348 (python-tests-look-at "profile =")
1349 (python-nav-lisp-forward-sexp-safe 4)
1350 (should (looking-at "(user=request.user)"))
1351 (python-tests-look-at "user=request.user")
1352 (python-nav-lisp-forward-sexp-safe -
1)
1353 (should (looking-at "(user=request.user)"))
1354 (python-nav-lisp-forward-sexp-safe -
4)
1355 (should (looking-at "profile ="))
1356 (python-tests-look-at "user=request.user")
1357 (python-nav-lisp-forward-sexp-safe 3)
1358 (should (looking-at ")"))
1359 (python-nav-lisp-forward-sexp-safe 1)
1360 (should (looking-at "$"))
1361 (python-nav-lisp-forward-sexp-safe 1)
1362 (should (looking-at ".notify()"))))
1364 (ert-deftest python-nav-forward-sexp-1
()
1365 (python-tests-with-temp-buffer
1371 (python-tests-look-at "a()")
1372 (python-nav-forward-sexp)
1373 (should (looking-at "$"))
1374 (should (save-excursion
1376 (looking-at "a()")))
1377 (python-nav-forward-sexp)
1378 (should (looking-at "$"))
1379 (should (save-excursion
1381 (looking-at "b()")))
1382 (python-nav-forward-sexp)
1383 (should (looking-at "$"))
1384 (should (save-excursion
1386 (looking-at "c()")))
1387 ;; Movement next to a paren should do what lisp does and
1388 ;; unfortunately It can't change, because otherwise
1389 ;; `blink-matching-open' breaks.
1390 (python-nav-forward-sexp -
1)
1391 (should (looking-at "()"))
1392 (should (save-excursion
1394 (looking-at "c()")))
1395 (python-nav-forward-sexp -
1)
1396 (should (looking-at "c()"))
1397 (python-nav-forward-sexp -
1)
1398 (should (looking-at "b()"))
1399 (python-nav-forward-sexp -
1)
1400 (should (looking-at "a()"))))
1402 (ert-deftest python-nav-forward-sexp-2
()
1403 (python-tests-with-temp-buffer
1412 (python-tests-look-at "aa =")
1413 (python-nav-forward-sexp)
1414 (should (looking-at " = bbb"))
1415 (python-nav-forward-sexp)
1416 (should (looking-at "$"))
1417 (should (save-excursion
1418 (back-to-indentation)
1419 (looking-at "aaa = bbb")))
1420 (python-nav-forward-sexp)
1421 (should (looking-at "$"))
1422 (should (save-excursion
1423 (back-to-indentation)
1424 (looking-at "ccc = ddd")))
1425 (python-nav-forward-sexp)
1426 (should (looking-at "$"))
1427 (should (save-excursion
1428 (back-to-indentation)
1429 (looking-at "eee = fff")))
1430 (python-nav-forward-sexp)
1431 (should (looking-at "$"))
1432 (should (save-excursion
1433 (back-to-indentation)
1434 (looking-at "return ggg")))
1435 (python-nav-forward-sexp -
1)
1436 (should (looking-at "def func():"))))
1438 (ert-deftest python-nav-forward-sexp-3
()
1439 (python-tests-with-temp-buffer
1441 from some_module import some_sub_module
1442 from another_module import another_sub_module
1444 def another_statement():
1447 (python-tests-look-at "some_module")
1448 (python-nav-forward-sexp)
1449 (should (looking-at " import"))
1450 (python-nav-forward-sexp)
1451 (should (looking-at " some_sub_module"))
1452 (python-nav-forward-sexp)
1453 (should (looking-at "$"))
1456 (back-to-indentation)
1458 "from some_module import some_sub_module")))
1459 (python-nav-forward-sexp)
1460 (should (looking-at "$"))
1463 (back-to-indentation)
1465 "from another_module import another_sub_module")))
1466 (python-nav-forward-sexp)
1467 (should (looking-at "$"))
1470 (back-to-indentation)
1473 (python-nav-forward-sexp -
1)
1474 (should (looking-at "def another_statement():"))
1475 (python-nav-forward-sexp -
1)
1476 (should (looking-at "from another_module import another_sub_module"))
1477 (python-nav-forward-sexp -
1)
1478 (should (looking-at "from some_module import some_sub_module"))))
1480 (ert-deftest python-nav-up-list-1
()
1481 (python-tests-with-temp-buffer
1485 return [i for i in range(3)]
1487 (python-tests-look-at "3)]")
1488 (python-nav-up-list)
1489 (should (looking-at "]"))
1490 (python-nav-up-list)
1491 (should (looking-at "$"))))
1493 (ert-deftest python-nav-backward-up-list-1
()
1494 :expected-result
:failed
1495 (python-tests-with-temp-buffer
1499 return [i for i in range(3)]
1501 (python-tests-look-at "3)]")
1502 (python-nav-backward-up-list)
1503 (should (looking-at "(3)\\]"))
1504 (python-nav-backward-up-list)
1506 "\\[i for i in range(3)\\]"))
1507 ;; FIXME: Need to move to beginning-of-statement.
1508 (python-nav-backward-up-list)
1510 "return \\[i for i in range(3)\\]"))
1511 (python-nav-backward-up-list)
1512 (should (looking-at "if True:"))
1513 (python-nav-backward-up-list)
1514 (should (looking-at "def f():"))))
1517 ;;; Shell integration
1519 (defvar python-tests-shell-interpreter
"python")
1521 (ert-deftest python-shell-get-process-name-1
()
1522 "Check process name calculation on different scenarios."
1523 (python-tests-with-temp-buffer
1525 (should (string= (python-shell-get-process-name nil
)
1526 python-shell-buffer-name
))
1527 ;; When the `current-buffer' doesn't have `buffer-file-name', even
1528 ;; if dedicated flag is non-nil should not include its name.
1529 (should (string= (python-shell-get-process-name t
)
1530 python-shell-buffer-name
)))
1531 (python-tests-with-temp-file
1533 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1534 ;; should be respected.
1535 (should (string= (python-shell-get-process-name nil
)
1536 python-shell-buffer-name
))
1538 (python-shell-get-process-name t
)
1539 (format "%s[%s]" python-shell-buffer-name buffer-file-name
)))))
1541 (ert-deftest python-shell-internal-get-process-name-1
()
1542 "Check the internal process name is config-unique."
1543 (let* ((python-shell-interpreter python-tests-shell-interpreter
)
1544 (python-shell-interpreter-args "")
1545 (python-shell-prompt-regexp ">>> ")
1546 (python-shell-prompt-block-regexp "[.][.][.] ")
1547 (python-shell-setup-codes "")
1548 (python-shell-process-environment "")
1549 (python-shell-extra-pythonpaths "")
1550 (python-shell-exec-path "")
1551 (python-shell-virtualenv-path "")
1552 (expected (python-tests-with-temp-buffer
1553 "" (python-shell-internal-get-process-name))))
1554 ;; Same configurations should match.
1557 (python-tests-with-temp-buffer
1558 "" (python-shell-internal-get-process-name))))
1559 (let ((python-shell-interpreter-args "-B"))
1560 ;; A minimal change should generate different names.
1564 (python-tests-with-temp-buffer
1565 "" (python-shell-internal-get-process-name))))))))
1567 (ert-deftest python-shell-parse-command-1
()
1568 "Check the command to execute is calculated correctly.
1569 Using `python-shell-interpreter' and
1570 `python-shell-interpreter-args'."
1571 (skip-unless (executable-find python-tests-shell-interpreter
))
1572 (let ((python-shell-interpreter (executable-find
1573 python-tests-shell-interpreter
))
1574 (python-shell-interpreter-args "-B"))
1577 python-shell-interpreter
1578 python-shell-interpreter-args
)
1579 (python-shell-parse-command)))))
1581 (ert-deftest python-shell-calculate-process-environment-1
()
1582 "Test `python-shell-process-environment' modification."
1583 (let* ((original-process-environment process-environment
)
1584 (python-shell-process-environment
1585 '("TESTVAR1=value1" "TESTVAR2=value2"))
1586 (process-environment
1587 (python-shell-calculate-process-environment)))
1588 (should (equal (getenv "TESTVAR1") "value1"))
1589 (should (equal (getenv "TESTVAR2") "value2"))))
1591 (ert-deftest python-shell-calculate-process-environment-2
()
1592 "Test `python-shell-extra-pythonpaths' modification."
1593 (let* ((original-process-environment process-environment
)
1594 (original-pythonpath (getenv "PYTHONPATH"))
1595 (paths '("path1" "path2"))
1596 (python-shell-extra-pythonpaths paths
)
1597 (process-environment
1598 (python-shell-calculate-process-environment)))
1599 (should (equal (getenv "PYTHONPATH")
1601 (mapconcat 'identity paths path-separator
)
1602 path-separator original-pythonpath
)))))
1604 (ert-deftest python-shell-calculate-process-environment-3
()
1605 "Test `python-shell-virtualenv-path' modification."
1606 (let* ((original-process-environment process-environment
)
1607 (original-path (or (getenv "PATH") ""))
1608 (python-shell-virtualenv-path
1609 (directory-file-name user-emacs-directory
))
1610 (process-environment
1611 (python-shell-calculate-process-environment)))
1612 (should (not (getenv "PYTHONHOME")))
1613 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-path
))
1614 (should (equal (getenv "PATH")
1615 (format "%s/bin%s%s"
1616 python-shell-virtualenv-path
1617 path-separator original-path
)))))
1619 (ert-deftest python-shell-calculate-exec-path-1
()
1620 "Test `python-shell-exec-path' modification."
1621 (let* ((original-exec-path exec-path
)
1622 (python-shell-exec-path '("path1" "path2"))
1623 (exec-path (python-shell-calculate-exec-path)))
1626 (append python-shell-exec-path
1627 original-exec-path
)))))
1629 (ert-deftest python-shell-calculate-exec-path-2
()
1630 "Test `python-shell-exec-path' modification."
1631 (let* ((original-exec-path exec-path
)
1632 (python-shell-virtualenv-path
1633 (directory-file-name user-emacs-directory
))
1634 (exec-path (python-shell-calculate-exec-path)))
1638 (format "%s/bin" python-shell-virtualenv-path
)
1639 original-exec-path
))))))
1641 (ert-deftest python-shell-make-comint-1
()
1642 "Check comint creation for global shell buffer."
1643 (skip-unless (executable-find python-tests-shell-interpreter
))
1644 ;; The interpreter can get killed too quickly to allow it to clean
1645 ;; up the tempfiles that the default python-shell-setup-codes create,
1646 ;; so it leaves tempfiles behind, which is a minor irritation.
1647 (let* ((python-shell-setup-codes nil
)
1648 (python-shell-interpreter
1649 (executable-find python-tests-shell-interpreter
))
1650 (proc-name (python-shell-get-process-name nil
))
1652 (python-tests-with-temp-buffer
1653 "" (python-shell-make-comint
1654 (python-shell-parse-command) proc-name
)))
1655 (process (get-buffer-process shell-buffer
)))
1658 (set-process-query-on-exit-flag process nil
)
1659 (should (process-live-p process
))
1660 (with-current-buffer shell-buffer
1661 (should (eq major-mode
'inferior-python-mode
))
1662 (should (string= (buffer-name) (format "*%s*" proc-name
)))))
1663 (kill-buffer shell-buffer
))))
1665 (ert-deftest python-shell-make-comint-2
()
1666 "Check comint creation for internal shell buffer."
1667 (skip-unless (executable-find python-tests-shell-interpreter
))
1668 (let* ((python-shell-setup-codes nil
)
1669 (python-shell-interpreter
1670 (executable-find python-tests-shell-interpreter
))
1671 (proc-name (python-shell-internal-get-process-name))
1673 (python-tests-with-temp-buffer
1674 "" (python-shell-make-comint
1675 (python-shell-parse-command) proc-name nil t
)))
1676 (process (get-buffer-process shell-buffer
)))
1679 (set-process-query-on-exit-flag process nil
)
1680 (should (process-live-p process
))
1681 (with-current-buffer shell-buffer
1682 (should (eq major-mode
'inferior-python-mode
))
1683 (should (string= (buffer-name) (format " *%s*" proc-name
)))))
1684 (kill-buffer shell-buffer
))))
1686 (ert-deftest python-shell-get-process-1
()
1687 "Check dedicated shell process preference over global."
1688 (skip-unless (executable-find python-tests-shell-interpreter
))
1689 (python-tests-with-temp-file
1691 (let* ((python-shell-setup-codes nil
)
1692 (python-shell-interpreter
1693 (executable-find python-tests-shell-interpreter
))
1694 (global-proc-name (python-shell-get-process-name nil
))
1695 (dedicated-proc-name (python-shell-get-process-name t
))
1696 (global-shell-buffer
1697 (python-shell-make-comint
1698 (python-shell-parse-command) global-proc-name
))
1699 (dedicated-shell-buffer
1700 (python-shell-make-comint
1701 (python-shell-parse-command) dedicated-proc-name
))
1702 (global-process (get-buffer-process global-shell-buffer
))
1703 (dedicated-process (get-buffer-process dedicated-shell-buffer
)))
1706 (set-process-query-on-exit-flag global-process nil
)
1707 (set-process-query-on-exit-flag dedicated-process nil
)
1708 ;; Prefer dedicated if global also exists.
1709 (should (equal (python-shell-get-process) dedicated-process
))
1710 (kill-buffer dedicated-shell-buffer
)
1711 ;; If there's only global, use it.
1712 (should (equal (python-shell-get-process) global-process
))
1713 (kill-buffer global-shell-buffer
)
1714 ;; No buffer available.
1715 (should (not (python-shell-get-process))))
1716 (ignore-errors (kill-buffer global-shell-buffer
))
1717 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
1719 (ert-deftest python-shell-get-or-create-process-1
()
1720 "Check shell process creation fallback."
1721 :expected-result
:failed
1722 (python-tests-with-temp-file
1724 ;; XXX: Break early until we can skip stuff. We need to mimic
1725 ;; user interaction because `python-shell-get-or-create-process'
1726 ;; asks for all arguments interactively when a shell process
1729 (let* ((python-shell-interpreter
1730 (executable-find python-tests-shell-interpreter
))
1732 (dedicated-process-name (python-shell-get-process-name t
))
1733 (dedicated-process (python-shell-get-or-create-process))
1734 (dedicated-shell-buffer (process-buffer dedicated-process
)))
1737 (set-process-query-on-exit-flag dedicated-process nil
)
1738 ;; Prefer dedicated if not buffer exist.
1739 (should (equal (process-name dedicated-process
)
1740 dedicated-process-name
))
1741 (kill-buffer dedicated-shell-buffer
)
1742 ;; No buffer available.
1743 (should (not (python-shell-get-process))))
1744 (ignore-errors (kill-buffer dedicated-shell-buffer
))))))
1746 (ert-deftest python-shell-internal-get-or-create-process-1
()
1747 "Check internal shell process creation fallback."
1748 (skip-unless (executable-find python-tests-shell-interpreter
))
1749 (python-tests-with-temp-file
1751 (should (not (process-live-p (python-shell-internal-get-process-name))))
1752 (let* ((python-shell-interpreter
1753 (executable-find python-tests-shell-interpreter
))
1754 (internal-process-name (python-shell-internal-get-process-name))
1755 (internal-process (python-shell-internal-get-or-create-process))
1756 (internal-shell-buffer (process-buffer internal-process
)))
1759 (set-process-query-on-exit-flag internal-process nil
)
1760 (should (equal (process-name internal-process
)
1761 internal-process-name
))
1762 (should (equal internal-process
1763 (python-shell-internal-get-or-create-process)))
1764 ;; No user buffer available.
1765 (should (not (python-shell-get-process)))
1766 (kill-buffer internal-shell-buffer
))
1767 (ignore-errors (kill-buffer internal-shell-buffer
))))))
1770 ;;; Shell completion
1773 ;;; PDB Track integration
1776 ;;; Symbol completion
1796 (ert-deftest python-imenu-create-index-1
()
1797 (python-tests-with-temp-buffer
1799 class Foo(models.Model):
1803 class Bar(models.Model):
1807 def decorator(arg1, arg2, arg3):
1808 '''print decorated function call data to stdout.
1812 @decorator('arg1', 'arg2')
1813 def func(a, b, c=True):
1819 def wrapped_f(*args):
1821 print ('Decorator arguments:', arg1, arg2, arg3)
1823 print ('called f(*args)')
1841 (goto-char (point-max))
1844 (cons "Foo (class)" (copy-marker 2))
1845 (cons "Bar (class)" (copy-marker 38))
1848 (cons "*function definition*" (copy-marker 74))
1851 (cons "*function definition*" (copy-marker 254))
1852 (cons "wrapped_f (def)" (copy-marker 294))))
1855 (cons "*class definition*" (copy-marker 519))
1856 (cons "a (def)" (copy-marker 539))
1857 (cons "b (def)" (copy-marker 570))
1860 (cons "*class definition*" (copy-marker 601))
1861 (cons "c (def)" (copy-marker 626)))))
1862 (python-imenu-create-index)))))
1864 (ert-deftest python-imenu-create-index-2
()
1865 (python-tests-with-temp-buffer
1875 (goto-char (point-max))
1880 (cons "*class definition*" (copy-marker 2))
1883 (cons "*function definition*" (copy-marker 21))
1884 (cons "foo1 (def)" (copy-marker 40)))
1885 (cons "foobar (def)" (copy-marker 78))))
1886 (python-imenu-create-index)))))
1888 (ert-deftest python-imenu-create-index-3
()
1889 (python-tests-with-temp-buffer
1898 (goto-char (point-max))
1903 (cons "*class definition*" (copy-marker 2))
1906 (cons "*function definition*" (copy-marker 21))
1907 (cons "foo1 (def)" (copy-marker 40))
1908 (cons "foo2 (def)" (copy-marker 77)))))
1909 (python-imenu-create-index)))))
1911 (ert-deftest python-imenu-create-index-4
()
1912 (python-tests-with-temp-buffer
1925 (goto-char (point-max))
1930 (cons "*class definition*" (copy-marker 2))
1933 (cons "*class definition*" (copy-marker 21))
1934 (cons "__init__ (def)" (copy-marker 44))
1935 (cons "__str__ (def)" (copy-marker 90)))
1936 (cons "__init__ (def)" (copy-marker 135))))
1937 (python-imenu-create-index)))))
1939 (ert-deftest python-imenu-create-flat-index-1
()
1940 (python-tests-with-temp-buffer
1942 class Foo(models.Model):
1946 class Bar(models.Model):
1950 def decorator(arg1, arg2, arg3):
1951 '''print decorated function call data to stdout.
1955 @decorator('arg1', 'arg2')
1956 def func(a, b, c=True):
1962 def wrapped_f(*args):
1964 print ('Decorator arguments:', arg1, arg2, arg3)
1966 print ('called f(*args)')
1984 (goto-char (point-max))
1986 (list (cons "Foo" (copy-marker 2))
1987 (cons "Bar" (copy-marker 38))
1988 (cons "decorator" (copy-marker 74))
1989 (cons "decorator.wrap" (copy-marker 254))
1990 (cons "decorator.wrap.wrapped_f" (copy-marker 294))
1991 (cons "Baz" (copy-marker 519))
1992 (cons "Baz.a" (copy-marker 539))
1993 (cons "Baz.b" (copy-marker 570))
1994 (cons "Baz.Frob" (copy-marker 601))
1995 (cons "Baz.Frob.c" (copy-marker 626)))
1996 (python-imenu-create-flat-index)))))
1998 (ert-deftest python-imenu-create-flat-index-2
()
1999 (python-tests-with-temp-buffer
2012 (goto-char (point-max))
2015 (cons "Foo" (copy-marker 2))
2016 (cons "Foo.Bar" (copy-marker 21))
2017 (cons "Foo.Bar.__init__" (copy-marker 44))
2018 (cons "Foo.Bar.__str__" (copy-marker 90))
2019 (cons "Foo.__init__" (copy-marker 135)))
2020 (python-imenu-create-flat-index)))))
2025 (ert-deftest python-info-current-defun-1
()
2026 (python-tests-with-temp-buffer
2031 (should (string= "foo" (python-info-current-defun)))
2032 (should (string= "def foo" (python-info-current-defun t)))
2034 (should (not (python-info-current-defun)))
2035 (indent-for-tab-command)
2036 (should (string= "foo" (python-info-current-defun)))
2037 (should (string= "def foo" (python-info-current-defun t)))))
2039 (ert-deftest python-info-current-defun-2
()
2040 (python-tests-with-temp-buffer
2046 return [i for i in range(3)]
2060 (should (string= "C" (python-info-current-defun)))
2061 (should (string= "class C" (python-info-current-defun t)))
2062 (python-tests-look-at "return [i for ")
2063 (should (string= "C.m" (python-info-current-defun)))
2064 (should (string= "def C.m" (python-info-current-defun t)))
2065 (python-tests-look-at "def b():")
2066 (should (string= "C.m.b" (python-info-current-defun)))
2067 (should (string= "def C.m.b" (python-info-current-defun t)))
2069 (indent-for-tab-command)
2070 (python-indent-dedent-line-backspace 1)
2071 (should (string= "C.m" (python-info-current-defun)))
2072 (should (string= "def C.m" (python-info-current-defun t)))
2073 (python-tests-look-at "def c(self):")
2075 (indent-for-tab-command)
2076 (should (string= "C.m.a" (python-info-current-defun)))
2077 (should (string= "def C.m.a" (python-info-current-defun t)))
2078 (python-indent-dedent-line-backspace 1)
2079 (should (string= "C.m" (python-info-current-defun)))
2080 (should (string= "def C.m" (python-info-current-defun t)))
2081 (python-indent-dedent-line-backspace 1)
2082 (should (string= "C" (python-info-current-defun)))
2083 (should (string= "class C" (python-info-current-defun t)))
2084 (python-tests-look-at "def c(self):")
2085 (should (string= "C.c" (python-info-current-defun)))
2086 (should (string= "def C.c" (python-info-current-defun t)))
2087 (python-tests-look-at "do_c()")
2088 (should (string= "C.c" (python-info-current-defun)))
2089 (should (string= "def C.c" (python-info-current-defun t)))))
2091 (ert-deftest python-info-current-defun-3
()
2092 (python-tests-with-temp-buffer
2094 def decoratorFunctionWithArguments(arg1, arg2, arg3):
2095 '''print decorated function call data to stdout.
2099 @decoratorFunctionWithArguments('arg1', 'arg2')
2100 def func(a, b, c=True):
2105 print 'Inside wwrap()'
2106 def wrapped_f(*args):
2107 print 'Inside wrapped_f()'
2108 print 'Decorator arguments:', arg1, arg2, arg3
2110 print 'After f(*args)'
2114 (python-tests-look-at "def wwrap(f):")
2116 (should (not (python-info-current-defun)))
2117 (indent-for-tab-command 1)
2118 (should (string= (python-info-current-defun)
2119 "decoratorFunctionWithArguments"))
2120 (should (string= (python-info-current-defun t)
2121 "def decoratorFunctionWithArguments"))
2122 (python-tests-look-at "def wrapped_f(*args):")
2123 (should (string= (python-info-current-defun)
2124 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
2125 (should (string= (python-info-current-defun t)
2126 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
2127 (python-tests-look-at "return wrapped_f")
2128 (should (string= (python-info-current-defun)
2129 "decoratorFunctionWithArguments.wwrap"))
2130 (should (string= (python-info-current-defun t)
2131 "def decoratorFunctionWithArguments.wwrap"))
2133 (python-tests-look-at "return wwrap")
2134 (should (string= (python-info-current-defun)
2135 "decoratorFunctionWithArguments"))
2136 (should (string= (python-info-current-defun t)
2137 "def decoratorFunctionWithArguments"))))
2139 (ert-deftest python-info-current-symbol-1
()
2140 (python-tests-with-temp-buffer
2150 (python-tests-look-at "self.c()")
2151 (should (string= "self.c" (python-info-current-symbol)))
2152 (should (string= "C.c" (python-info-current-symbol t
)))))
2154 (ert-deftest python-info-current-symbol-2
()
2155 (python-tests-with-temp-buffer
2167 (python-tests-look-at "self.c()")
2168 (should (string= "self.c" (python-info-current-symbol)))
2169 (should (string= "C.M.c" (python-info-current-symbol t
)))))
2171 (ert-deftest python-info-current-symbol-3
()
2172 "Keywords should not be considered symbols."
2173 :expected-result
:failed
2174 (python-tests-with-temp-buffer
2179 ;; FIXME: keywords are not symbols.
2180 (python-tests-look-at "class C")
2181 (should (not (python-info-current-symbol)))
2182 (should (not (python-info-current-symbol t
)))
2183 (python-tests-look-at "C(object)")
2184 (should (string= "C" (python-info-current-symbol)))
2185 (should (string= "class C" (python-info-current-symbol t
)))))
2187 (ert-deftest python-info-statement-starts-block-p-1
()
2188 (python-tests-with-temp-buffer
2190 def long_function_name(
2191 var_one, var_two, var_three,
2195 (python-tests-look-at "def long_function_name")
2196 (should (python-info-statement-starts-block-p))
2197 (python-tests-look-at "print (var_one)")
2198 (python-util-forward-comment -
1)
2199 (should (python-info-statement-starts-block-p))))
2201 (ert-deftest python-info-statement-starts-block-p-2
()
2202 (python-tests-with-temp-buffer
2204 if width == 0 and height == 0 and \\\\
2205 color == 'red' and emphasis == 'strong' or \\\\
2207 raise ValueError('sorry, you lose')
2209 (python-tests-look-at "if width == 0 and")
2210 (should (python-info-statement-starts-block-p))
2211 (python-tests-look-at "raise ValueError(")
2212 (python-util-forward-comment -
1)
2213 (should (python-info-statement-starts-block-p))))
2215 (ert-deftest python-info-statement-ends-block-p-1
()
2216 (python-tests-with-temp-buffer
2218 def long_function_name(
2219 var_one, var_two, var_three,
2223 (python-tests-look-at "print (var_one)")
2224 (should (python-info-statement-ends-block-p))))
2226 (ert-deftest python-info-statement-ends-block-p-2
()
2227 (python-tests-with-temp-buffer
2229 if width == 0 and height == 0 and \\\\
2230 color == 'red' and emphasis == 'strong' or \\\\
2237 (python-tests-look-at "raise ValueError(")
2238 (should (python-info-statement-ends-block-p))))
2240 (ert-deftest python-info-beginning-of-statement-p-1
()
2241 (python-tests-with-temp-buffer
2243 def long_function_name(
2244 var_one, var_two, var_three,
2248 (python-tests-look-at "def long_function_name")
2249 (should (python-info-beginning-of-statement-p))
2251 (should (not (python-info-beginning-of-statement-p)))
2252 (python-tests-look-at "print (var_one)")
2253 (should (python-info-beginning-of-statement-p))
2254 (goto-char (line-beginning-position))
2255 (should (not (python-info-beginning-of-statement-p)))))
2257 (ert-deftest python-info-beginning-of-statement-p-2
()
2258 (python-tests-with-temp-buffer
2260 if width == 0 and height == 0 and \\\\
2261 color == 'red' and emphasis == 'strong' or \\\\
2268 (python-tests-look-at "if width == 0 and")
2269 (should (python-info-beginning-of-statement-p))
2271 (should (not (python-info-beginning-of-statement-p)))
2272 (python-tests-look-at "raise ValueError(")
2273 (should (python-info-beginning-of-statement-p))
2274 (goto-char (line-beginning-position))
2275 (should (not (python-info-beginning-of-statement-p)))))
2277 (ert-deftest python-info-end-of-statement-p-1
()
2278 (python-tests-with-temp-buffer
2280 def long_function_name(
2281 var_one, var_two, var_three,
2285 (python-tests-look-at "def long_function_name")
2286 (should (not (python-info-end-of-statement-p)))
2288 (should (not (python-info-end-of-statement-p)))
2289 (python-tests-look-at "print (var_one)")
2290 (python-util-forward-comment -
1)
2291 (should (python-info-end-of-statement-p))
2292 (python-tests-look-at "print (var_one)")
2293 (should (not (python-info-end-of-statement-p)))
2295 (should (python-info-end-of-statement-p))))
2297 (ert-deftest python-info-end-of-statement-p-2
()
2298 (python-tests-with-temp-buffer
2300 if width == 0 and height == 0 and \\\\
2301 color == 'red' and emphasis == 'strong' or \\\\
2308 (python-tests-look-at "if width == 0 and")
2309 (should (not (python-info-end-of-statement-p)))
2311 (should (not (python-info-end-of-statement-p)))
2312 (python-tests-look-at "raise ValueError(")
2313 (python-util-forward-comment -
1)
2314 (should (python-info-end-of-statement-p))
2315 (python-tests-look-at "raise ValueError(")
2316 (should (not (python-info-end-of-statement-p)))
2318 (should (not (python-info-end-of-statement-p)))
2319 (goto-char (point-max))
2320 (python-util-forward-comment -
1)
2321 (should (python-info-end-of-statement-p))))
2323 (ert-deftest python-info-beginning-of-block-p-1
()
2324 (python-tests-with-temp-buffer
2326 def long_function_name(
2327 var_one, var_two, var_three,
2331 (python-tests-look-at "def long_function_name")
2332 (should (python-info-beginning-of-block-p))
2333 (python-tests-look-at "var_one, var_two, var_three,")
2334 (should (not (python-info-beginning-of-block-p)))
2335 (python-tests-look-at "print (var_one)")
2336 (should (not (python-info-beginning-of-block-p)))))
2338 (ert-deftest python-info-beginning-of-block-p-2
()
2339 (python-tests-with-temp-buffer
2341 if width == 0 and height == 0 and \\\\
2342 color == 'red' and emphasis == 'strong' or \\\\
2349 (python-tests-look-at "if width == 0 and")
2350 (should (python-info-beginning-of-block-p))
2351 (python-tests-look-at "color == 'red' and emphasis")
2352 (should (not (python-info-beginning-of-block-p)))
2353 (python-tests-look-at "raise ValueError(")
2354 (should (not (python-info-beginning-of-block-p)))))
2356 (ert-deftest python-info-end-of-block-p-1
()
2357 (python-tests-with-temp-buffer
2359 def long_function_name(
2360 var_one, var_two, var_three,
2364 (python-tests-look-at "def long_function_name")
2365 (should (not (python-info-end-of-block-p)))
2366 (python-tests-look-at "var_one, var_two, var_three,")
2367 (should (not (python-info-end-of-block-p)))
2368 (python-tests-look-at "var_four):")
2370 (should (not (python-info-end-of-block-p)))
2371 (python-tests-look-at "print (var_one)")
2372 (should (not (python-info-end-of-block-p)))
2374 (should (python-info-end-of-block-p))))
2376 (ert-deftest python-info-end-of-block-p-2
()
2377 (python-tests-with-temp-buffer
2379 if width == 0 and height == 0 and \\\\
2380 color == 'red' and emphasis == 'strong' or \\\\
2387 (python-tests-look-at "if width == 0 and")
2388 (should (not (python-info-end-of-block-p)))
2389 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2390 (should (not (python-info-end-of-block-p)))
2391 (python-tests-look-at "highlight > 100:")
2393 (should (not (python-info-end-of-block-p)))
2394 (python-tests-look-at "raise ValueError(")
2395 (should (not (python-info-end-of-block-p)))
2397 (should (not (python-info-end-of-block-p)))
2398 (goto-char (point-max))
2399 (python-util-forward-comment -
1)
2400 (should (python-info-end-of-block-p))))
2402 (ert-deftest python-info-closing-block-1
()
2403 (python-tests-with-temp-buffer
2405 if request.user.is_authenticated():
2407 profile = request.user.get_profile()
2408 except Profile.DoesNotExist:
2409 profile = Profile.objects.create(user=request.user)
2412 profile.recalculate_stats()
2414 profile.clear_stats()
2419 (python-tests-look-at "try:")
2420 (should (not (python-info-closing-block)))
2421 (python-tests-look-at "except Profile.DoesNotExist:")
2422 (should (= (python-tests-look-at "try:" -
1 t
)
2423 (python-info-closing-block)))
2424 (python-tests-look-at "else:")
2425 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -
1 t
)
2426 (python-info-closing-block)))
2427 (python-tests-look-at "if profile.stats:")
2428 (should (not (python-info-closing-block)))
2429 (python-tests-look-at "else:")
2430 (should (= (python-tests-look-at "if profile.stats:" -
1 t
)
2431 (python-info-closing-block)))
2432 (python-tests-look-at "finally:")
2433 (should (= (python-tests-look-at "else:" -
2 t
)
2434 (python-info-closing-block)))))
2436 (ert-deftest python-info-closing-block-2
()
2437 (python-tests-with-temp-buffer
2439 if request.user.is_authenticated():
2440 profile = Profile.objects.get_or_create(user=request.user)
2442 profile.recalculate_stats()
2449 (python-tests-look-at "'else': 'do it'")
2450 (should (not (python-info-closing-block)))
2451 (python-tests-look-at "'else'")
2452 (should (not (python-info-closing-block)))))
2454 (ert-deftest python-info-line-ends-backslash-p-1
()
2455 (python-tests-with-temp-buffer
2457 objects = Thing.objects.all() \\\\
2467 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
2468 (should (python-info-line-ends-backslash-p 3))
2469 (should (python-info-line-ends-backslash-p 4))
2470 (should (python-info-line-ends-backslash-p 5))
2471 (should (python-info-line-ends-backslash-p 6)) ; ) \...
2472 (should (python-info-line-ends-backslash-p 7))
2473 (should (python-info-line-ends-backslash-p 8))
2474 (should (python-info-line-ends-backslash-p 9))
2475 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
2477 (ert-deftest python-info-beginning-of-backslash-1
()
2478 (python-tests-with-temp-buffer
2480 objects = Thing.objects.all() \\\\
2491 (second (python-tests-look-at ".filter("))
2492 (third (python-tests-look-at ".aggregate(")))
2493 (should (= first
(python-info-beginning-of-backslash 2)))
2494 (should (= second
(python-info-beginning-of-backslash 3)))
2495 (should (= second
(python-info-beginning-of-backslash 4)))
2496 (should (= second
(python-info-beginning-of-backslash 5)))
2497 (should (= second
(python-info-beginning-of-backslash 6)))
2498 (should (= third
(python-info-beginning-of-backslash 7)))
2499 (should (= third
(python-info-beginning-of-backslash 8)))
2500 (should (= third
(python-info-beginning-of-backslash 9)))
2501 (should (not (python-info-beginning-of-backslash 10))))))
2503 (ert-deftest python-info-continuation-line-p-1
()
2504 (python-tests-with-temp-buffer
2506 if width == 0 and height == 0 and \\\\
2507 color == 'red' and emphasis == 'strong' or \\\\
2514 (python-tests-look-at "if width == 0 and height == 0 and")
2515 (should (not (python-info-continuation-line-p)))
2516 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2517 (should (python-info-continuation-line-p))
2518 (python-tests-look-at "highlight > 100:")
2519 (should (python-info-continuation-line-p))
2520 (python-tests-look-at "raise ValueError(")
2521 (should (not (python-info-continuation-line-p)))
2522 (python-tests-look-at "'sorry, you lose'")
2523 (should (python-info-continuation-line-p))
2525 (should (python-info-continuation-line-p))
2526 (python-tests-look-at ")")
2527 (should (python-info-continuation-line-p))
2529 (should (not (python-info-continuation-line-p)))))
2531 (ert-deftest python-info-block-continuation-line-p-1
()
2532 (python-tests-with-temp-buffer
2534 if width == 0 and height == 0 and \\\\
2535 color == 'red' and emphasis == 'strong' or \\\\
2542 (python-tests-look-at "if width == 0 and")
2543 (should (not (python-info-block-continuation-line-p)))
2544 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2545 (should (= (python-info-block-continuation-line-p)
2546 (python-tests-look-at "if width == 0 and" -
1 t
)))
2547 (python-tests-look-at "highlight > 100:")
2548 (should (not (python-info-block-continuation-line-p)))))
2550 (ert-deftest python-info-block-continuation-line-p-2
()
2551 (python-tests-with-temp-buffer
2558 (python-tests-look-at "def foo(a,")
2559 (should (not (python-info-block-continuation-line-p)))
2560 (python-tests-look-at "b,")
2561 (should (= (python-info-block-continuation-line-p)
2562 (python-tests-look-at "def foo(a," -
1 t
)))
2563 (python-tests-look-at "c):")
2564 (should (not (python-info-block-continuation-line-p)))))
2566 (ert-deftest python-info-assignment-continuation-line-p-1
()
2567 (python-tests-with-temp-buffer
2569 data = foo(), bar() \\\\
2573 (python-tests-look-at "data = foo(), bar()")
2574 (should (not (python-info-assignment-continuation-line-p)))
2575 (python-tests-look-at "baz(), 4")
2576 (should (= (python-info-assignment-continuation-line-p)
2577 (python-tests-look-at "foo()," -
1 t
)))
2578 (python-tests-look-at "5, 6")
2579 (should (not (python-info-assignment-continuation-line-p)))))
2581 (ert-deftest python-info-assignment-continuation-line-p-2
()
2582 (python-tests-with-temp-buffer
2584 data = (foo(), bar()
2588 (python-tests-look-at "data = (foo(), bar()")
2589 (should (not (python-info-assignment-continuation-line-p)))
2590 (python-tests-look-at "baz(), 4")
2591 (should (= (python-info-assignment-continuation-line-p)
2592 (python-tests-look-at "(foo()," -
1 t
)))
2593 (python-tests-look-at "5, 6)")
2594 (should (not (python-info-assignment-continuation-line-p)))))
2596 (ert-deftest python-info-looking-at-beginning-of-defun-1
()
2597 (python-tests-with-temp-buffer
2599 def decorat0r(deff):
2610 (python-tests-look-at "def decorat0r(deff):")
2611 (should (python-info-looking-at-beginning-of-defun))
2612 (python-tests-look-at "def foo(arg):")
2613 (should (not (python-info-looking-at-beginning-of-defun)))
2614 (python-tests-look-at "def wrap():")
2615 (should (python-info-looking-at-beginning-of-defun))
2616 (python-tests-look-at "deff()")
2617 (should (not (python-info-looking-at-beginning-of-defun)))))
2619 (ert-deftest python-info-current-line-comment-p-1
()
2620 (python-tests-with-temp-buffer
2623 foo = True # another comment
2627 print ('bar') # print bar
2629 (python-tests-look-at "# this is a comment")
2630 (should (python-info-current-line-comment-p))
2631 (python-tests-look-at "foo = True # another comment")
2632 (should (not (python-info-current-line-comment-p)))
2633 (python-tests-look-at "'#this is a string'")
2634 (should (not (python-info-current-line-comment-p)))
2635 (python-tests-look-at "# more comments")
2636 (should (python-info-current-line-comment-p))
2637 (python-tests-look-at "print ('bar') # print bar")
2638 (should (not (python-info-current-line-comment-p)))))
2640 (ert-deftest python-info-current-line-empty-p
()
2641 (python-tests-with-temp-buffer
2645 foo = True # another comment
2647 (should (python-info-current-line-empty-p))
2648 (python-tests-look-at "# this is a comment")
2649 (should (not (python-info-current-line-empty-p)))
2651 (should (python-info-current-line-empty-p))))
2654 ;;; Utility functions
2656 (ert-deftest python-util-goto-line-1
()
2657 (python-tests-with-temp-buffer
2662 pass" (make-string 20 ?
\n))
2663 (python-util-goto-line 10)
2664 (should (= (line-number-at-pos) 10))
2665 (python-util-goto-line 20)
2666 (should (= (line-number-at-pos) 20))))
2668 (ert-deftest python-util-clone-local-variables-1
()
2669 (let ((buffer (generate-new-buffer
2670 "python-util-clone-local-variables-1"))
2672 '((python-fill-docstring-style . django
)
2673 (python-shell-interpreter .
"python")
2674 (python-shell-interpreter-args .
"manage.py shell")
2675 (python-shell-prompt-regexp .
"In \\[[0-9]+\\]: ")
2676 (python-shell-prompt-output-regexp .
"Out\\[[0-9]+\\]: ")
2677 (python-shell-extra-pythonpaths "/home/user/pylib/")
2678 (python-shell-completion-setup-code
2679 .
"from IPython.core.completerlib import module_completion")
2680 (python-shell-completion-module-string-code
2681 .
"';'.join(module_completion('''%s'''))\n")
2682 (python-shell-completion-string-code
2683 .
"';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
2684 (python-shell-virtualenv-path
2685 .
"/home/user/.virtualenvs/project"))))
2686 (with-current-buffer buffer
2687 (kill-all-local-variables)
2688 (dolist (ccons varcons
)
2689 (set (make-local-variable (car ccons
)) (cdr ccons
))))
2690 (python-tests-with-temp-buffer
2692 (python-util-clone-local-variables buffer
)
2693 (dolist (ccons varcons
)
2695 (equal (symbol-value (car ccons
)) (cdr ccons
)))))
2696 (kill-buffer buffer
)))
2698 (ert-deftest python-util-forward-comment-1
()
2699 (python-tests-with-temp-buffer
2703 # bad indented comment
2704 # more comments" (make-string 9999 ?
\n))
2705 (python-util-forward-comment 1)
2706 (should (= (point) (point-max)))
2707 (python-util-forward-comment -
1)
2708 (should (= (point) (point-min)))))
2711 (provide 'python-tests
)
2715 ;; indent-tabs-mode: nil
2718 ;;; python-tests.el ends here