]> code.delx.au - gnu-emacs/blob - test/automated/python-tests.el
* test/automated/python-tests.el (python-indent-block-enders):
[gnu-emacs] / test / automated / python-tests.el
1 ;;; python-tests.el --- Test suite for python.el
2
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
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.
11
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.
16
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/>.
19
20 ;;; Commentary:
21
22 ;;; Code:
23
24 (require 'ert)
25 (require 'python)
26
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))
32 `(with-temp-buffer
33 (python-mode)
34 (insert ,contents)
35 (goto-char (point-min))
36 ,@body))
37
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)))
46 (unwind-protect
47 (with-current-buffer buffer
48 (python-mode)
49 (insert ,contents)
50 (goto-char (point-min))
51 ,@body)
52 (and buffer (kill-buffer buffer))
53 (delete-file temp-file))))
54
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+))
68 (found-point))
69 (prog2
70 (catch 'exit
71 (while (not (= num 0))
72 (when (and (> num 0)
73 (looking-at string))
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))
77 (throw 'exit t))
78 (when (> num 0)
79 ;; `re-search-forward' leaves point at the end of the
80 ;; occurrence, move back so point is at the beginning
81 ;; instead.
82 (forward-char (- (length (match-string-no-properties 0)))))
83 (setq
84 num (funcall deinc-fn num)
85 found-point (point))))
86 found-point
87 (and restore-point (goto-char starting-point)))))
88
89 \f
90 ;;; Tests for your tests, so you can test while you test.
91
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
97 aliqua."
98 (let ((expected (save-excursion
99 (dotimes (i 3)
100 (re-search-forward "et" nil t))
101 (forward-char -2)
102 (point))))
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"))
109 (forward-char -2)
110 (should (= (python-tests-look-at "et") expected)))))
111
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
117 aliqua."
118 (let ((expected
119 (save-excursion
120 (re-search-forward "et" nil t)
121 (forward-char -2)
122 (point))))
123 (dotimes (i 3)
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)))))
127
128 \f
129 ;;; Bindings
130
131 \f
132 ;;; Python specialized rx
133
134 \f
135 ;;; Font-lock and syntax
136
137 \f
138 ;;; Indentation
139
140 ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
141
142 (ert-deftest python-indent-pep8-1 ()
143 "First pep8 case."
144 (python-tests-with-temp-buffer
145 "# Aligned with opening delimiter
146 foo = long_function_name(var_one, var_two,
147 var_three, var_four)
148 "
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))))
157
158 (ert-deftest python-indent-pep8-2 ()
159 "Second pep8 case."
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,
164 var_four):
165 print (var_one)
166 "
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))))
181
182 (ert-deftest python-indent-pep8-3 ()
183 "Third pep8 case."
184 (python-tests-with-temp-buffer
185 "# Extra indentation is not necessary.
186 foo = long_function_name(
187 var_one, var_two,
188 var_three, var_four)
189 "
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))))
201
202 (ert-deftest python-indent-inside-paren-1 ()
203 "The most simple inside-paren case that shouldn't fail."
204 (python-tests-with-temp-buffer
205 "
206 data = {
207 'key':
208 {
209 'objlist': [
210 {
211 'pk': 1,
212 'name': 'first',
213 },
214 {
215 'pk': 2,
216 'name': 'second',
217 }
218 ]
219 }
220 }
221 "
222 (python-tests-look-at "data = {")
223 (should (eq (car (python-indent-context)) 'after-line))
224 (should (= (python-indent-calculate-indentation) 0))
225 (python-tests-look-at "'key':")
226 (should (eq (car (python-indent-context)) 'inside-paren))
227 (should (= (python-indent-calculate-indentation) 4))
228 (python-tests-look-at "{")
229 (should (eq (car (python-indent-context)) 'inside-paren))
230 (should (= (python-indent-calculate-indentation) 4))
231 (python-tests-look-at "'objlist': [")
232 (should (eq (car (python-indent-context)) 'inside-paren))
233 (should (= (python-indent-calculate-indentation) 8))
234 (python-tests-look-at "{")
235 (should (eq (car (python-indent-context)) 'inside-paren))
236 (should (= (python-indent-calculate-indentation) 12))
237 (python-tests-look-at "'pk': 1,")
238 (should (eq (car (python-indent-context)) 'inside-paren))
239 (should (= (python-indent-calculate-indentation) 16))
240 (python-tests-look-at "'name': 'first',")
241 (should (eq (car (python-indent-context)) 'inside-paren))
242 (should (= (python-indent-calculate-indentation) 16))
243 (python-tests-look-at "},")
244 (should (eq (car (python-indent-context)) 'inside-paren))
245 (should (= (python-indent-calculate-indentation) 12))
246 (python-tests-look-at "{")
247 (should (eq (car (python-indent-context)) 'inside-paren))
248 (should (= (python-indent-calculate-indentation) 12))
249 (python-tests-look-at "'pk': 2,")
250 (should (eq (car (python-indent-context)) 'inside-paren))
251 (should (= (python-indent-calculate-indentation) 16))
252 (python-tests-look-at "'name': 'second',")
253 (should (eq (car (python-indent-context)) 'inside-paren))
254 (should (= (python-indent-calculate-indentation) 16))
255 (python-tests-look-at "}")
256 (should (eq (car (python-indent-context)) 'inside-paren))
257 (should (= (python-indent-calculate-indentation) 12))
258 (python-tests-look-at "]")
259 (should (eq (car (python-indent-context)) 'inside-paren))
260 (should (= (python-indent-calculate-indentation) 8))
261 (python-tests-look-at "}")
262 (should (eq (car (python-indent-context)) 'inside-paren))
263 (should (= (python-indent-calculate-indentation) 4))
264 (python-tests-look-at "}")
265 (should (eq (car (python-indent-context)) 'inside-paren))
266 (should (= (python-indent-calculate-indentation) 0))))
267
268 (ert-deftest python-indent-inside-paren-2 ()
269 "Another more compact paren group style."
270 (python-tests-with-temp-buffer
271 "
272 data = {'key': {
273 'objlist': [
274 {'pk': 1,
275 'name': 'first'},
276 {'pk': 2,
277 'name': 'second'}
278 ]
279 }}
280 "
281 (python-tests-look-at "data = {")
282 (should (eq (car (python-indent-context)) 'after-line))
283 (should (= (python-indent-calculate-indentation) 0))
284 (python-tests-look-at "'objlist': [")
285 (should (eq (car (python-indent-context)) 'inside-paren))
286 (should (= (python-indent-calculate-indentation) 4))
287 (python-tests-look-at "{'pk': 1,")
288 (should (eq (car (python-indent-context)) 'inside-paren))
289 (should (= (python-indent-calculate-indentation) 8))
290 (python-tests-look-at "'name': 'first'},")
291 (should (eq (car (python-indent-context)) 'inside-paren))
292 (should (= (python-indent-calculate-indentation) 9))
293 (python-tests-look-at "{'pk': 2,")
294 (should (eq (car (python-indent-context)) 'inside-paren))
295 (should (= (python-indent-calculate-indentation) 8))
296 (python-tests-look-at "'name': 'second'}")
297 (should (eq (car (python-indent-context)) 'inside-paren))
298 (should (= (python-indent-calculate-indentation) 9))
299 (python-tests-look-at "]")
300 (should (eq (car (python-indent-context)) 'inside-paren))
301 (should (= (python-indent-calculate-indentation) 4))
302 (python-tests-look-at "}}")
303 (should (eq (car (python-indent-context)) 'inside-paren))
304 (should (= (python-indent-calculate-indentation) 0))
305 (python-tests-look-at "}")
306 (should (eq (car (python-indent-context)) 'inside-paren))
307 (should (= (python-indent-calculate-indentation) 0))))
308
309 (ert-deftest python-indent-after-block-1 ()
310 "The most simple after-block case that shouldn't fail."
311 (python-tests-with-temp-buffer
312 "
313 def foo(a, b, c=True):
314 "
315 (should (eq (car (python-indent-context)) 'no-indent))
316 (should (= (python-indent-calculate-indentation) 0))
317 (goto-char (point-max))
318 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
319 (should (= (python-indent-calculate-indentation) 4))))
320
321 (ert-deftest python-indent-after-block-2 ()
322 "A weird (malformed) multiline block statement."
323 (python-tests-with-temp-buffer
324 "
325 def foo(a, b, c={
326 'a':
327 }):
328 "
329 (goto-char (point-max))
330 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
331 (should (= (python-indent-calculate-indentation) 4))))
332
333 (ert-deftest python-indent-dedenters-1 ()
334 "Check all dedenters."
335 (python-tests-with-temp-buffer
336 "
337 def foo(a, b, c):
338 if a:
339 print (a)
340 elif b:
341 print (b)
342 else:
343 try:
344 print (c.pop())
345 except (IndexError, AttributeError):
346 print (c)
347 finally:
348 print ('nor a, nor b are true')
349 "
350 (python-tests-look-at "if a:")
351 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
352 (should (= (python-indent-calculate-indentation) 4))
353 (python-tests-look-at "print (a)")
354 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
355 (should (= (python-indent-calculate-indentation) 8))
356 (python-tests-look-at "elif b:")
357 (should (eq (car (python-indent-context)) 'after-line))
358 (should (= (python-indent-calculate-indentation) 4))
359 (python-tests-look-at "print (b)")
360 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
361 (should (= (python-indent-calculate-indentation) 8))
362 (python-tests-look-at "else:")
363 (should (eq (car (python-indent-context)) 'after-line))
364 (should (= (python-indent-calculate-indentation) 4))
365 (python-tests-look-at "try:")
366 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
367 (should (= (python-indent-calculate-indentation) 8))
368 (python-tests-look-at "print (c.pop())")
369 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
370 (should (= (python-indent-calculate-indentation) 12))
371 (python-tests-look-at "except (IndexError, AttributeError):")
372 (should (eq (car (python-indent-context)) 'after-line))
373 (should (= (python-indent-calculate-indentation) 8))
374 (python-tests-look-at "print (c)")
375 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
376 (should (= (python-indent-calculate-indentation) 12))
377 (python-tests-look-at "finally:")
378 (should (eq (car (python-indent-context)) 'after-line))
379 (should (= (python-indent-calculate-indentation) 8))
380 (python-tests-look-at "print ('nor a, nor b are true')")
381 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
382 (should (= (python-indent-calculate-indentation) 12))))
383
384 (ert-deftest python-indent-after-backslash-1 ()
385 "The most common case."
386 (python-tests-with-temp-buffer
387 "
388 from foo.bar.baz import something, something_1 \\\\
389 something_2 something_3, \\\\
390 something_4, something_5
391 "
392 (python-tests-look-at "from foo.bar.baz import something, something_1")
393 (should (eq (car (python-indent-context)) 'after-line))
394 (should (= (python-indent-calculate-indentation) 0))
395 (python-tests-look-at "something_2 something_3,")
396 (should (eq (car (python-indent-context)) 'after-backslash))
397 (should (= (python-indent-calculate-indentation) 4))
398 (python-tests-look-at "something_4, something_5")
399 (should (eq (car (python-indent-context)) 'after-backslash))
400 (should (= (python-indent-calculate-indentation) 4))
401 (goto-char (point-max))
402 (should (eq (car (python-indent-context)) 'after-line))
403 (should (= (python-indent-calculate-indentation) 0))))
404
405 (ert-deftest python-indent-after-backslash-2 ()
406 "A pretty extreme complicated case."
407 (python-tests-with-temp-buffer
408 "
409 objects = Thing.objects.all() \\\\
410 .filter(
411 type='toy',
412 status='bought'
413 ) \\\\
414 .aggregate(
415 Sum('amount')
416 ) \\\\
417 .values_list()
418 "
419 (python-tests-look-at "objects = Thing.objects.all()")
420 (should (eq (car (python-indent-context)) 'after-line))
421 (should (= (python-indent-calculate-indentation) 0))
422 (python-tests-look-at ".filter(")
423 (should (eq (car (python-indent-context)) 'after-backslash))
424 (should (= (python-indent-calculate-indentation) 23))
425 (python-tests-look-at "type='toy',")
426 (should (eq (car (python-indent-context)) 'inside-paren))
427 (should (= (python-indent-calculate-indentation) 27))
428 (python-tests-look-at "status='bought'")
429 (should (eq (car (python-indent-context)) 'inside-paren))
430 (should (= (python-indent-calculate-indentation) 27))
431 (python-tests-look-at ") \\\\")
432 (should (eq (car (python-indent-context)) 'inside-paren))
433 (should (= (python-indent-calculate-indentation) 23))
434 (python-tests-look-at ".aggregate(")
435 (should (eq (car (python-indent-context)) 'after-backslash))
436 (should (= (python-indent-calculate-indentation) 23))
437 (python-tests-look-at "Sum('amount')")
438 (should (eq (car (python-indent-context)) 'inside-paren))
439 (should (= (python-indent-calculate-indentation) 27))
440 (python-tests-look-at ") \\\\")
441 (should (eq (car (python-indent-context)) 'inside-paren))
442 (should (= (python-indent-calculate-indentation) 23))
443 (python-tests-look-at ".values_list()")
444 (should (eq (car (python-indent-context)) 'after-backslash))
445 (should (= (python-indent-calculate-indentation) 23))
446 (forward-line 1)
447 (should (eq (car (python-indent-context)) 'after-line))
448 (should (= (python-indent-calculate-indentation) 0))))
449
450 (ert-deftest python-indent-block-enders ()
451 "Test `python-indent-block-enders' value honoring."
452 (python-tests-with-temp-buffer
453 "
454 Class foo(object):
455
456 def bar(self):
457 if self.baz:
458 return (1,
459 2,
460 3)
461
462 else:
463 pass
464 "
465 (python-tests-look-at "3)")
466 (forward-line 1)
467 (should (= (python-indent-calculate-indentation) 8))
468 (python-tests-look-at "pass")
469 (forward-line 1)
470 (should (= (python-indent-calculate-indentation) 8))))
471
472 \f
473 ;;; Navigation
474
475 (ert-deftest python-nav-beginning-of-defun-1 ()
476 (python-tests-with-temp-buffer
477 "
478 def decoratorFunctionWithArguments(arg1, arg2, arg3):
479 '''print decorated function call data to stdout.
480
481 Usage:
482
483 @decoratorFunctionWithArguments('arg1', 'arg2')
484 def func(a, b, c=True):
485 pass
486 '''
487
488 def wwrap(f):
489 print 'Inside wwrap()'
490 def wrapped_f(*args):
491 print 'Inside wrapped_f()'
492 print 'Decorator arguments:', arg1, arg2, arg3
493 f(*args)
494 print 'After f(*args)'
495 return wrapped_f
496 return wwrap
497 "
498 (python-tests-look-at "return wrap")
499 (should (= (save-excursion
500 (python-nav-beginning-of-defun)
501 (point))
502 (save-excursion
503 (python-tests-look-at "def wrapped_f(*args):" -1)
504 (beginning-of-line)
505 (point))))
506 (python-tests-look-at "def wrapped_f(*args):" -1)
507 (should (= (save-excursion
508 (python-nav-beginning-of-defun)
509 (point))
510 (save-excursion
511 (python-tests-look-at "def wwrap(f):" -1)
512 (beginning-of-line)
513 (point))))
514 (python-tests-look-at "def wwrap(f):" -1)
515 (should (= (save-excursion
516 (python-nav-beginning-of-defun)
517 (point))
518 (save-excursion
519 (python-tests-look-at "def decoratorFunctionWithArguments" -1)
520 (beginning-of-line)
521 (point))))))
522
523 (ert-deftest python-nav-beginning-of-defun-2 ()
524 (python-tests-with-temp-buffer
525 "
526 class C(object):
527
528 def m(self):
529 self.c()
530
531 def b():
532 pass
533
534 def a():
535 pass
536
537 def c(self):
538 pass
539 "
540 ;; Nested defuns, are handled with care.
541 (python-tests-look-at "def c(self):")
542 (should (= (save-excursion
543 (python-nav-beginning-of-defun)
544 (point))
545 (save-excursion
546 (python-tests-look-at "def m(self):" -1)
547 (beginning-of-line)
548 (point))))
549 ;; Defuns on same levels should be respected.
550 (python-tests-look-at "def a():" -1)
551 (should (= (save-excursion
552 (python-nav-beginning-of-defun)
553 (point))
554 (save-excursion
555 (python-tests-look-at "def b():" -1)
556 (beginning-of-line)
557 (point))))
558 ;; Jump to a top level defun.
559 (python-tests-look-at "def b():" -1)
560 (should (= (save-excursion
561 (python-nav-beginning-of-defun)
562 (point))
563 (save-excursion
564 (python-tests-look-at "def m(self):" -1)
565 (beginning-of-line)
566 (point))))
567 ;; Jump to a top level defun again.
568 (python-tests-look-at "def m(self):" -1)
569 (should (= (save-excursion
570 (python-nav-beginning-of-defun)
571 (point))
572 (save-excursion
573 (python-tests-look-at "class C(object):" -1)
574 (beginning-of-line)
575 (point))))))
576
577 (ert-deftest python-nav-end-of-defun-1 ()
578 (python-tests-with-temp-buffer
579 "
580 class C(object):
581
582 def m(self):
583 self.c()
584
585 def b():
586 pass
587
588 def a():
589 pass
590
591 def c(self):
592 pass
593 "
594 (should (= (save-excursion
595 (python-tests-look-at "class C(object):")
596 (python-nav-end-of-defun)
597 (point))
598 (save-excursion
599 (point-max))))
600 (should (= (save-excursion
601 (python-tests-look-at "def m(self):")
602 (python-nav-end-of-defun)
603 (point))
604 (save-excursion
605 (python-tests-look-at "def c(self):")
606 (forward-line -1)
607 (point))))
608 (should (= (save-excursion
609 (python-tests-look-at "def b():")
610 (python-nav-end-of-defun)
611 (point))
612 (save-excursion
613 (python-tests-look-at "def b():")
614 (forward-line 2)
615 (point))))
616 (should (= (save-excursion
617 (python-tests-look-at "def c(self):")
618 (python-nav-end-of-defun)
619 (point))
620 (save-excursion
621 (point-max))))))
622
623 (ert-deftest python-nav-end-of-defun-2 ()
624 (python-tests-with-temp-buffer
625 "
626 def decoratorFunctionWithArguments(arg1, arg2, arg3):
627 '''print decorated function call data to stdout.
628
629 Usage:
630
631 @decoratorFunctionWithArguments('arg1', 'arg2')
632 def func(a, b, c=True):
633 pass
634 '''
635
636 def wwrap(f):
637 print 'Inside wwrap()'
638 def wrapped_f(*args):
639 print 'Inside wrapped_f()'
640 print 'Decorator arguments:', arg1, arg2, arg3
641 f(*args)
642 print 'After f(*args)'
643 return wrapped_f
644 return wwrap
645 "
646 (should (= (save-excursion
647 (python-tests-look-at "def decoratorFunctionWithArguments")
648 (python-nav-end-of-defun)
649 (point))
650 (save-excursion
651 (point-max))))
652 (should (= (save-excursion
653 (python-tests-look-at "@decoratorFunctionWithArguments")
654 (python-nav-end-of-defun)
655 (point))
656 (save-excursion
657 (point-max))))
658 (should (= (save-excursion
659 (python-tests-look-at "def wwrap(f):")
660 (python-nav-end-of-defun)
661 (point))
662 (save-excursion
663 (python-tests-look-at "return wwrap")
664 (line-beginning-position))))
665 (should (= (save-excursion
666 (python-tests-look-at "def wrapped_f(*args):")
667 (python-nav-end-of-defun)
668 (point))
669 (save-excursion
670 (python-tests-look-at "return wrapped_f")
671 (line-beginning-position))))
672 (should (= (save-excursion
673 (python-tests-look-at "f(*args)")
674 (python-nav-end-of-defun)
675 (point))
676 (save-excursion
677 (python-tests-look-at "return wrapped_f")
678 (line-beginning-position))))))
679
680 (ert-deftest python-nav-backward-defun-1 ()
681 (python-tests-with-temp-buffer
682 "
683 class A(object): # A
684
685 def a(self): # a
686 pass
687
688 def b(self): # b
689 pass
690
691 class B(object): # B
692
693 class C(object): # C
694
695 def d(self): # d
696 pass
697
698 # def e(self): # e
699 # pass
700
701 def c(self): # c
702 pass
703
704 # def d(self): # d
705 # pass
706 "
707 (goto-char (point-max))
708 (should (= (save-excursion (python-nav-backward-defun))
709 (python-tests-look-at " def c(self): # c" -1)))
710 (should (= (save-excursion (python-nav-backward-defun))
711 (python-tests-look-at " def d(self): # d" -1)))
712 (should (= (save-excursion (python-nav-backward-defun))
713 (python-tests-look-at " class C(object): # C" -1)))
714 (should (= (save-excursion (python-nav-backward-defun))
715 (python-tests-look-at " class B(object): # B" -1)))
716 (should (= (save-excursion (python-nav-backward-defun))
717 (python-tests-look-at " def b(self): # b" -1)))
718 (should (= (save-excursion (python-nav-backward-defun))
719 (python-tests-look-at " def a(self): # a" -1)))
720 (should (= (save-excursion (python-nav-backward-defun))
721 (python-tests-look-at "class A(object): # A" -1)))
722 (should (not (python-nav-backward-defun)))))
723
724 (ert-deftest python-nav-backward-defun-2 ()
725 (python-tests-with-temp-buffer
726 "
727 def decoratorFunctionWithArguments(arg1, arg2, arg3):
728 '''print decorated function call data to stdout.
729
730 Usage:
731
732 @decoratorFunctionWithArguments('arg1', 'arg2')
733 def func(a, b, c=True):
734 pass
735 '''
736
737 def wwrap(f):
738 print 'Inside wwrap()'
739 def wrapped_f(*args):
740 print 'Inside wrapped_f()'
741 print 'Decorator arguments:', arg1, arg2, arg3
742 f(*args)
743 print 'After f(*args)'
744 return wrapped_f
745 return wwrap
746 "
747 (goto-char (point-max))
748 (should (= (save-excursion (python-nav-backward-defun))
749 (python-tests-look-at " def wrapped_f(*args):" -1)))
750 (should (= (save-excursion (python-nav-backward-defun))
751 (python-tests-look-at " def wwrap(f):" -1)))
752 (should (= (save-excursion (python-nav-backward-defun))
753 (python-tests-look-at "def decoratorFunctionWithArguments(arg1, arg2, arg3):" -1)))
754 (should (not (python-nav-backward-defun)))))
755
756 (ert-deftest python-nav-backward-defun-3 ()
757 (python-tests-with-temp-buffer
758 "
759 '''
760 def u(self):
761 pass
762
763 def v(self):
764 pass
765
766 def w(self):
767 pass
768 '''
769
770 class A(object):
771 pass
772 "
773 (goto-char (point-min))
774 (let ((point (python-tests-look-at "class A(object):")))
775 (should (not (python-nav-backward-defun)))
776 (should (= point (point))))))
777
778 (ert-deftest python-nav-forward-defun-1 ()
779 (python-tests-with-temp-buffer
780 "
781 class A(object): # A
782
783 def a(self): # a
784 pass
785
786 def b(self): # b
787 pass
788
789 class B(object): # B
790
791 class C(object): # C
792
793 def d(self): # d
794 pass
795
796 # def e(self): # e
797 # pass
798
799 def c(self): # c
800 pass
801
802 # def d(self): # d
803 # pass
804 "
805 (goto-char (point-min))
806 (should (= (save-excursion (python-nav-forward-defun))
807 (python-tests-look-at "(object): # A")))
808 (should (= (save-excursion (python-nav-forward-defun))
809 (python-tests-look-at "(self): # a")))
810 (should (= (save-excursion (python-nav-forward-defun))
811 (python-tests-look-at "(self): # b")))
812 (should (= (save-excursion (python-nav-forward-defun))
813 (python-tests-look-at "(object): # B")))
814 (should (= (save-excursion (python-nav-forward-defun))
815 (python-tests-look-at "(object): # C")))
816 (should (= (save-excursion (python-nav-forward-defun))
817 (python-tests-look-at "(self): # d")))
818 (should (= (save-excursion (python-nav-forward-defun))
819 (python-tests-look-at "(self): # c")))
820 (should (not (python-nav-forward-defun)))))
821
822 (ert-deftest python-nav-forward-defun-2 ()
823 (python-tests-with-temp-buffer
824 "
825 def decoratorFunctionWithArguments(arg1, arg2, arg3):
826 '''print decorated function call data to stdout.
827
828 Usage:
829
830 @decoratorFunctionWithArguments('arg1', 'arg2')
831 def func(a, b, c=True):
832 pass
833 '''
834
835 def wwrap(f):
836 print 'Inside wwrap()'
837 def wrapped_f(*args):
838 print 'Inside wrapped_f()'
839 print 'Decorator arguments:', arg1, arg2, arg3
840 f(*args)
841 print 'After f(*args)'
842 return wrapped_f
843 return wwrap
844 "
845 (goto-char (point-min))
846 (should (= (save-excursion (python-nav-forward-defun))
847 (python-tests-look-at "(arg1, arg2, arg3):")))
848 (should (= (save-excursion (python-nav-forward-defun))
849 (python-tests-look-at "(f):")))
850 (should (= (save-excursion (python-nav-forward-defun))
851 (python-tests-look-at "(*args):")))
852 (should (not (python-nav-forward-defun)))))
853
854 (ert-deftest python-nav-forward-defun-3 ()
855 (python-tests-with-temp-buffer
856 "
857 class A(object):
858 pass
859
860 '''
861 def u(self):
862 pass
863
864 def v(self):
865 pass
866
867 def w(self):
868 pass
869 '''
870 "
871 (goto-char (point-min))
872 (let ((point (python-tests-look-at "(object):")))
873 (should (not (python-nav-forward-defun)))
874 (should (= point (point))))))
875
876 (ert-deftest python-nav-beginning-of-statement-1 ()
877 (python-tests-with-temp-buffer
878 "
879 v1 = 123 + \
880 456 + \
881 789
882 v2 = (value1,
883 value2,
884
885 value3,
886 value4)
887 v3 = ('this is a string'
888
889 'that is continued'
890 'between lines'
891 'within a paren',
892 # this is a comment, yo
893 'continue previous line')
894 v4 = '''
895 a very long
896 string
897 '''
898 "
899 (python-tests-look-at "v2 =")
900 (python-util-forward-comment -1)
901 (should (= (save-excursion
902 (python-nav-beginning-of-statement)
903 (point))
904 (python-tests-look-at "v1 =" -1 t)))
905 (python-tests-look-at "v3 =")
906 (python-util-forward-comment -1)
907 (should (= (save-excursion
908 (python-nav-beginning-of-statement)
909 (point))
910 (python-tests-look-at "v2 =" -1 t)))
911 (python-tests-look-at "v4 =")
912 (python-util-forward-comment -1)
913 (should (= (save-excursion
914 (python-nav-beginning-of-statement)
915 (point))
916 (python-tests-look-at "v3 =" -1 t)))
917 (goto-char (point-max))
918 (python-util-forward-comment -1)
919 (should (= (save-excursion
920 (python-nav-beginning-of-statement)
921 (point))
922 (python-tests-look-at "v4 =" -1 t)))))
923
924 (ert-deftest python-nav-end-of-statement-1 ()
925 (python-tests-with-temp-buffer
926 "
927 v1 = 123 + \
928 456 + \
929 789
930 v2 = (value1,
931 value2,
932
933 value3,
934 value4)
935 v3 = ('this is a string'
936
937 'that is continued'
938 'between lines'
939 'within a paren',
940 # this is a comment, yo
941 'continue previous line')
942 v4 = '''
943 a very long
944 string
945 '''
946 "
947 (python-tests-look-at "v1 =")
948 (should (= (save-excursion
949 (python-nav-end-of-statement)
950 (point))
951 (save-excursion
952 (python-tests-look-at "789")
953 (line-end-position))))
954 (python-tests-look-at "v2 =")
955 (should (= (save-excursion
956 (python-nav-end-of-statement)
957 (point))
958 (save-excursion
959 (python-tests-look-at "value4)")
960 (line-end-position))))
961 (python-tests-look-at "v3 =")
962 (should (= (save-excursion
963 (python-nav-end-of-statement)
964 (point))
965 (save-excursion
966 (python-tests-look-at
967 "'continue previous line')")
968 (line-end-position))))
969 (python-tests-look-at "v4 =")
970 (should (= (save-excursion
971 (python-nav-end-of-statement)
972 (point))
973 (save-excursion
974 (goto-char (point-max))
975 (python-util-forward-comment -1)
976 (point))))))
977
978 (ert-deftest python-nav-forward-statement-1 ()
979 (python-tests-with-temp-buffer
980 "
981 v1 = 123 + \
982 456 + \
983 789
984 v2 = (value1,
985 value2,
986
987 value3,
988 value4)
989 v3 = ('this is a string'
990
991 'that is continued'
992 'between lines'
993 'within a paren',
994 # this is a comment, yo
995 'continue previous line')
996 v4 = '''
997 a very long
998 string
999 '''
1000 "
1001 (python-tests-look-at "v1 =")
1002 (should (= (save-excursion
1003 (python-nav-forward-statement)
1004 (point))
1005 (python-tests-look-at "v2 =")))
1006 (should (= (save-excursion
1007 (python-nav-forward-statement)
1008 (point))
1009 (python-tests-look-at "v3 =")))
1010 (should (= (save-excursion
1011 (python-nav-forward-statement)
1012 (point))
1013 (python-tests-look-at "v4 =")))
1014 (should (= (save-excursion
1015 (python-nav-forward-statement)
1016 (point))
1017 (point-max)))))
1018
1019 (ert-deftest python-nav-backward-statement-1 ()
1020 (python-tests-with-temp-buffer
1021 "
1022 v1 = 123 + \
1023 456 + \
1024 789
1025 v2 = (value1,
1026 value2,
1027
1028 value3,
1029 value4)
1030 v3 = ('this is a string'
1031
1032 'that is continued'
1033 'between lines'
1034 'within a paren',
1035 # this is a comment, yo
1036 'continue previous line')
1037 v4 = '''
1038 a very long
1039 string
1040 '''
1041 "
1042 (goto-char (point-max))
1043 (should (= (save-excursion
1044 (python-nav-backward-statement)
1045 (point))
1046 (python-tests-look-at "v4 =" -1)))
1047 (should (= (save-excursion
1048 (python-nav-backward-statement)
1049 (point))
1050 (python-tests-look-at "v3 =" -1)))
1051 (should (= (save-excursion
1052 (python-nav-backward-statement)
1053 (point))
1054 (python-tests-look-at "v2 =" -1)))
1055 (should (= (save-excursion
1056 (python-nav-backward-statement)
1057 (point))
1058 (python-tests-look-at "v1 =" -1)))))
1059
1060 (ert-deftest python-nav-backward-statement-2 ()
1061 :expected-result :failed
1062 (python-tests-with-temp-buffer
1063 "
1064 v1 = 123 + \
1065 456 + \
1066 789
1067 v2 = (value1,
1068 value2,
1069
1070 value3,
1071 value4)
1072 "
1073 ;; FIXME: For some reason `python-nav-backward-statement' is moving
1074 ;; back two sentences when starting from 'value4)'.
1075 (goto-char (point-max))
1076 (python-util-forward-comment -1)
1077 (should (= (save-excursion
1078 (python-nav-backward-statement)
1079 (point))
1080 (python-tests-look-at "v2 =" -1 t)))))
1081
1082 (ert-deftest python-nav-beginning-of-block-1 ()
1083 (python-tests-with-temp-buffer
1084 "
1085 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1086 '''print decorated function call data to stdout.
1087
1088 Usage:
1089
1090 @decoratorFunctionWithArguments('arg1', 'arg2')
1091 def func(a, b, c=True):
1092 pass
1093 '''
1094
1095 def wwrap(f):
1096 print 'Inside wwrap()'
1097 def wrapped_f(*args):
1098 print 'Inside wrapped_f()'
1099 print 'Decorator arguments:', arg1, arg2, arg3
1100 f(*args)
1101 print 'After f(*args)'
1102 return wrapped_f
1103 return wwrap
1104 "
1105 (python-tests-look-at "return wwrap")
1106 (should (= (save-excursion
1107 (python-nav-beginning-of-block)
1108 (point))
1109 (python-tests-look-at "def decoratorFunctionWithArguments" -1)))
1110 (python-tests-look-at "print 'Inside wwrap()'")
1111 (should (= (save-excursion
1112 (python-nav-beginning-of-block)
1113 (point))
1114 (python-tests-look-at "def wwrap(f):" -1)))
1115 (python-tests-look-at "print 'After f(*args)'")
1116 (end-of-line)
1117 (should (= (save-excursion
1118 (python-nav-beginning-of-block)
1119 (point))
1120 (python-tests-look-at "def wrapped_f(*args):" -1)))
1121 (python-tests-look-at "return wrapped_f")
1122 (should (= (save-excursion
1123 (python-nav-beginning-of-block)
1124 (point))
1125 (python-tests-look-at "def wwrap(f):" -1)))))
1126
1127 (ert-deftest python-nav-end-of-block-1 ()
1128 (python-tests-with-temp-buffer
1129 "
1130 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1131 '''print decorated function call data to stdout.
1132
1133 Usage:
1134
1135 @decoratorFunctionWithArguments('arg1', 'arg2')
1136 def func(a, b, c=True):
1137 pass
1138 '''
1139
1140 def wwrap(f):
1141 print 'Inside wwrap()'
1142 def wrapped_f(*args):
1143 print 'Inside wrapped_f()'
1144 print 'Decorator arguments:', arg1, arg2, arg3
1145 f(*args)
1146 print 'After f(*args)'
1147 return wrapped_f
1148 return wwrap
1149 "
1150 (python-tests-look-at "def decoratorFunctionWithArguments")
1151 (should (= (save-excursion
1152 (python-nav-end-of-block)
1153 (point))
1154 (save-excursion
1155 (goto-char (point-max))
1156 (python-util-forward-comment -1)
1157 (point))))
1158 (python-tests-look-at "def wwrap(f):")
1159 (should (= (save-excursion
1160 (python-nav-end-of-block)
1161 (point))
1162 (save-excursion
1163 (python-tests-look-at "return wrapped_f")
1164 (line-end-position))))
1165 (end-of-line)
1166 (should (= (save-excursion
1167 (python-nav-end-of-block)
1168 (point))
1169 (save-excursion
1170 (python-tests-look-at "return wrapped_f")
1171 (line-end-position))))
1172 (python-tests-look-at "f(*args)")
1173 (should (= (save-excursion
1174 (python-nav-end-of-block)
1175 (point))
1176 (save-excursion
1177 (python-tests-look-at "print 'After f(*args)'")
1178 (line-end-position))))))
1179
1180 (ert-deftest python-nav-forward-block-1 ()
1181 "This also accounts as a test for `python-nav-backward-block'."
1182 (python-tests-with-temp-buffer
1183 "
1184 if request.user.is_authenticated():
1185 # def block():
1186 # pass
1187 try:
1188 profile = request.user.get_profile()
1189 except Profile.DoesNotExist:
1190 profile = Profile.objects.create(user=request.user)
1191 else:
1192 if profile.stats:
1193 profile.recalculate_stats()
1194 else:
1195 profile.clear_stats()
1196 finally:
1197 profile.views += 1
1198 profile.save()
1199 "
1200 (should (= (save-excursion (python-nav-forward-block))
1201 (python-tests-look-at "if request.user.is_authenticated():")))
1202 (should (= (save-excursion (python-nav-forward-block))
1203 (python-tests-look-at "try:")))
1204 (should (= (save-excursion (python-nav-forward-block))
1205 (python-tests-look-at "except Profile.DoesNotExist:")))
1206 (should (= (save-excursion (python-nav-forward-block))
1207 (python-tests-look-at "else:")))
1208 (should (= (save-excursion (python-nav-forward-block))
1209 (python-tests-look-at "if profile.stats:")))
1210 (should (= (save-excursion (python-nav-forward-block))
1211 (python-tests-look-at "else:")))
1212 (should (= (save-excursion (python-nav-forward-block))
1213 (python-tests-look-at "finally:")))
1214 ;; When point is at the last block, leave it there and return nil
1215 (should (not (save-excursion (python-nav-forward-block))))
1216 ;; Move backwards, and even if the number of moves is less than the
1217 ;; provided argument return the point.
1218 (should (= (save-excursion (python-nav-forward-block -10))
1219 (python-tests-look-at
1220 "if request.user.is_authenticated():" -1)))))
1221
1222 (ert-deftest python-nav-lisp-forward-sexp-safe-1 ()
1223 (python-tests-with-temp-buffer
1224 "
1225 profile = Profile.objects.create(user=request.user)
1226 profile.notify()
1227 "
1228 (python-tests-look-at "profile =")
1229 (python-nav-lisp-forward-sexp-safe 4)
1230 (should (looking-at "(user=request.user)"))
1231 (python-tests-look-at "user=request.user")
1232 (python-nav-lisp-forward-sexp-safe -1)
1233 (should (looking-at "(user=request.user)"))
1234 (python-nav-lisp-forward-sexp-safe -4)
1235 (should (looking-at "profile ="))
1236 (python-tests-look-at "user=request.user")
1237 (python-nav-lisp-forward-sexp-safe 3)
1238 (should (looking-at ")"))
1239 (python-nav-lisp-forward-sexp-safe 1)
1240 (should (looking-at "$"))
1241 (python-nav-lisp-forward-sexp-safe 1)
1242 (should (looking-at ".notify()"))))
1243
1244 (ert-deftest python-nav-forward-sexp-1 ()
1245 (python-tests-with-temp-buffer
1246 "
1247 a()
1248 b()
1249 c()
1250 "
1251 (python-tests-look-at "a()")
1252 (python-nav-forward-sexp)
1253 (should (looking-at "$"))
1254 (should (save-excursion
1255 (beginning-of-line)
1256 (looking-at "a()")))
1257 (python-nav-forward-sexp)
1258 (should (looking-at "$"))
1259 (should (save-excursion
1260 (beginning-of-line)
1261 (looking-at "b()")))
1262 (python-nav-forward-sexp)
1263 (should (looking-at "$"))
1264 (should (save-excursion
1265 (beginning-of-line)
1266 (looking-at "c()")))
1267 ;; Movement next to a paren should do what lisp does and
1268 ;; unfortunately It can't change, because otherwise
1269 ;; `blink-matching-open' breaks.
1270 (python-nav-forward-sexp -1)
1271 (should (looking-at "()"))
1272 (should (save-excursion
1273 (beginning-of-line)
1274 (looking-at "c()")))
1275 (python-nav-forward-sexp -1)
1276 (should (looking-at "c()"))
1277 (python-nav-forward-sexp -1)
1278 (should (looking-at "b()"))
1279 (python-nav-forward-sexp -1)
1280 (should (looking-at "a()"))))
1281
1282 (ert-deftest python-nav-forward-sexp-2 ()
1283 (python-tests-with-temp-buffer
1284 "
1285 def func():
1286 if True:
1287 aaa = bbb
1288 ccc = ddd
1289 eee = fff
1290 return ggg
1291 "
1292 (python-tests-look-at "aa =")
1293 (python-nav-forward-sexp)
1294 (should (looking-at " = bbb"))
1295 (python-nav-forward-sexp)
1296 (should (looking-at "$"))
1297 (should (save-excursion
1298 (back-to-indentation)
1299 (looking-at "aaa = bbb")))
1300 (python-nav-forward-sexp)
1301 (should (looking-at "$"))
1302 (should (save-excursion
1303 (back-to-indentation)
1304 (looking-at "ccc = ddd")))
1305 (python-nav-forward-sexp)
1306 (should (looking-at "$"))
1307 (should (save-excursion
1308 (back-to-indentation)
1309 (looking-at "eee = fff")))
1310 (python-nav-forward-sexp)
1311 (should (looking-at "$"))
1312 (should (save-excursion
1313 (back-to-indentation)
1314 (looking-at "return ggg")))
1315 (python-nav-forward-sexp -1)
1316 (should (looking-at "def func():"))))
1317
1318 (ert-deftest python-nav-forward-sexp-3 ()
1319 (python-tests-with-temp-buffer
1320 "
1321 from some_module import some_sub_module
1322 from another_module import another_sub_module
1323
1324 def another_statement():
1325 pass
1326 "
1327 (python-tests-look-at "some_module")
1328 (python-nav-forward-sexp)
1329 (should (looking-at " import"))
1330 (python-nav-forward-sexp)
1331 (should (looking-at " some_sub_module"))
1332 (python-nav-forward-sexp)
1333 (should (looking-at "$"))
1334 (should
1335 (save-excursion
1336 (back-to-indentation)
1337 (looking-at
1338 "from some_module import some_sub_module")))
1339 (python-nav-forward-sexp)
1340 (should (looking-at "$"))
1341 (should
1342 (save-excursion
1343 (back-to-indentation)
1344 (looking-at
1345 "from another_module import another_sub_module")))
1346 (python-nav-forward-sexp)
1347 (should (looking-at "$"))
1348 (should
1349 (save-excursion
1350 (back-to-indentation)
1351 (looking-at
1352 "pass")))
1353 (python-nav-forward-sexp -1)
1354 (should (looking-at "def another_statement():"))
1355 (python-nav-forward-sexp -1)
1356 (should (looking-at "from another_module import another_sub_module"))
1357 (python-nav-forward-sexp -1)
1358 (should (looking-at "from some_module import some_sub_module"))))
1359
1360 (ert-deftest python-nav-up-list-1 ()
1361 (python-tests-with-temp-buffer
1362 "
1363 def f():
1364 if True:
1365 return [i for i in range(3)]
1366 "
1367 (python-tests-look-at "3)]")
1368 (python-nav-up-list)
1369 (should (looking-at "]"))
1370 (python-nav-up-list)
1371 (should (looking-at "$"))))
1372
1373 (ert-deftest python-nav-backward-up-list-1 ()
1374 :expected-result :failed
1375 (python-tests-with-temp-buffer
1376 "
1377 def f():
1378 if True:
1379 return [i for i in range(3)]
1380 "
1381 (python-tests-look-at "3)]")
1382 (python-nav-backward-up-list)
1383 (should (looking-at "(3)\\]"))
1384 (python-nav-backward-up-list)
1385 (should (looking-at
1386 "\\[i for i in range(3)\\]"))
1387 ;; FIXME: Need to move to beginning-of-statement.
1388 (python-nav-backward-up-list)
1389 (should (looking-at
1390 "return \\[i for i in range(3)\\]"))
1391 (python-nav-backward-up-list)
1392 (should (looking-at "if True:"))
1393 (python-nav-backward-up-list)
1394 (should (looking-at "def f():"))))
1395
1396 \f
1397 ;;; Shell integration
1398
1399 (defvar python-tests-shell-interpreter "python")
1400
1401 (ert-deftest python-shell-get-process-name-1 ()
1402 "Check process name calculation on different scenarios."
1403 (python-tests-with-temp-buffer
1404 ""
1405 (should (string= (python-shell-get-process-name nil)
1406 python-shell-buffer-name))
1407 ;; When the `current-buffer' doesn't have `buffer-file-name', even
1408 ;; if dedicated flag is non-nil should not include its name.
1409 (should (string= (python-shell-get-process-name t)
1410 python-shell-buffer-name)))
1411 (python-tests-with-temp-file
1412 ""
1413 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1414 ;; should be respected.
1415 (should (string= (python-shell-get-process-name nil)
1416 python-shell-buffer-name))
1417 (should (string=
1418 (python-shell-get-process-name t)
1419 (format "%s[%s]" python-shell-buffer-name buffer-file-name)))))
1420
1421 (ert-deftest python-shell-internal-get-process-name-1 ()
1422 "Check the internal process name is config-unique."
1423 (let* ((python-shell-interpreter python-tests-shell-interpreter)
1424 (python-shell-interpreter-args "")
1425 (python-shell-prompt-regexp ">>> ")
1426 (python-shell-prompt-block-regexp "[.][.][.] ")
1427 (python-shell-setup-codes "")
1428 (python-shell-process-environment "")
1429 (python-shell-extra-pythonpaths "")
1430 (python-shell-exec-path "")
1431 (python-shell-virtualenv-path "")
1432 (expected (python-tests-with-temp-buffer
1433 "" (python-shell-internal-get-process-name))))
1434 ;; Same configurations should match.
1435 (should
1436 (string= expected
1437 (python-tests-with-temp-buffer
1438 "" (python-shell-internal-get-process-name))))
1439 (let ((python-shell-interpreter-args "-B"))
1440 ;; A minimal change should generate different names.
1441 (should
1442 (not (string=
1443 expected
1444 (python-tests-with-temp-buffer
1445 "" (python-shell-internal-get-process-name))))))))
1446
1447 (ert-deftest python-shell-parse-command-1 ()
1448 "Check the command to execute is calculated correctly.
1449 Using `python-shell-interpreter' and
1450 `python-shell-interpreter-args'."
1451 :expected-result (if (executable-find python-tests-shell-interpreter)
1452 :passed
1453 :failed)
1454 (let ((python-shell-interpreter (executable-find
1455 python-tests-shell-interpreter))
1456 (python-shell-interpreter-args "-B"))
1457 (should (string=
1458 (format "%s %s"
1459 python-shell-interpreter
1460 python-shell-interpreter-args)
1461 (python-shell-parse-command)))))
1462
1463 (ert-deftest python-shell-calculate-process-environment-1 ()
1464 "Test `python-shell-process-environment' modification."
1465 (let* ((original-process-environment process-environment)
1466 (python-shell-process-environment
1467 '("TESTVAR1=value1" "TESTVAR2=value2"))
1468 (process-environment
1469 (python-shell-calculate-process-environment)))
1470 (should (equal (getenv "TESTVAR1") "value1"))
1471 (should (equal (getenv "TESTVAR2") "value2"))))
1472
1473 (ert-deftest python-shell-calculate-process-environment-2 ()
1474 "Test `python-shell-extra-pythonpaths' modification."
1475 (let* ((original-process-environment process-environment)
1476 (original-pythonpath (getenv "PYTHONPATH"))
1477 (paths '("path1" "path2"))
1478 (python-shell-extra-pythonpaths paths)
1479 (process-environment
1480 (python-shell-calculate-process-environment)))
1481 (should (equal (getenv "PYTHONPATH")
1482 (concat
1483 (mapconcat 'identity paths path-separator)
1484 path-separator original-pythonpath)))))
1485
1486 (ert-deftest python-shell-calculate-process-environment-3 ()
1487 "Test `python-shell-virtualenv-path' modification."
1488 (let* ((original-process-environment process-environment)
1489 (original-path (or (getenv "PATH") ""))
1490 (python-shell-virtualenv-path
1491 (directory-file-name user-emacs-directory))
1492 (process-environment
1493 (python-shell-calculate-process-environment)))
1494 (should (not (getenv "PYTHONHOME")))
1495 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-path))
1496 (should (equal (getenv "PATH")
1497 (format "%s/bin%s%s"
1498 python-shell-virtualenv-path
1499 path-separator original-path)))))
1500
1501 (ert-deftest python-shell-calculate-exec-path-1 ()
1502 "Test `python-shell-exec-path' modification."
1503 (let* ((original-exec-path exec-path)
1504 (python-shell-exec-path '("path1" "path2"))
1505 (exec-path (python-shell-calculate-exec-path)))
1506 (should (equal
1507 exec-path
1508 (append python-shell-exec-path
1509 original-exec-path)))))
1510
1511 (ert-deftest python-shell-calculate-exec-path-2 ()
1512 "Test `python-shell-exec-path' modification."
1513 (let* ((original-exec-path exec-path)
1514 (python-shell-virtualenv-path
1515 (directory-file-name user-emacs-directory))
1516 (exec-path (python-shell-calculate-exec-path)))
1517 (should (equal
1518 exec-path
1519 (append (cons
1520 (format "%s/bin" python-shell-virtualenv-path)
1521 original-exec-path))))))
1522
1523 (ert-deftest python-shell-make-comint-1 ()
1524 "Check comint creation for global shell buffer."
1525 :expected-result (if (executable-find python-tests-shell-interpreter)
1526 :passed
1527 :failed)
1528 (let* ((python-shell-interpreter
1529 (executable-find python-tests-shell-interpreter))
1530 (proc-name (python-shell-get-process-name nil))
1531 (shell-buffer
1532 (python-tests-with-temp-buffer
1533 "" (python-shell-make-comint
1534 (python-shell-parse-command) proc-name)))
1535 (process (get-buffer-process shell-buffer)))
1536 (unwind-protect
1537 (progn
1538 (set-process-query-on-exit-flag process nil)
1539 (should (process-live-p process))
1540 (with-current-buffer shell-buffer
1541 (should (eq major-mode 'inferior-python-mode))
1542 (should (string= (buffer-name) (format "*%s*" proc-name)))))
1543 (kill-buffer shell-buffer))))
1544
1545 (ert-deftest python-shell-make-comint-2 ()
1546 "Check comint creation for internal shell buffer."
1547 :expected-result (if (executable-find python-tests-shell-interpreter)
1548 :passed
1549 :failed)
1550 (let* ((python-shell-interpreter
1551 (executable-find python-tests-shell-interpreter))
1552 (proc-name (python-shell-internal-get-process-name))
1553 (shell-buffer
1554 (python-tests-with-temp-buffer
1555 "" (python-shell-make-comint
1556 (python-shell-parse-command) proc-name nil t)))
1557 (process (get-buffer-process shell-buffer)))
1558 (unwind-protect
1559 (progn
1560 (set-process-query-on-exit-flag process nil)
1561 (should (process-live-p process))
1562 (with-current-buffer shell-buffer
1563 (should (eq major-mode 'inferior-python-mode))
1564 (should (string= (buffer-name) (format " *%s*" proc-name)))))
1565 (kill-buffer shell-buffer))))
1566
1567 (ert-deftest python-shell-get-process-1 ()
1568 "Check dedicated shell process preference over global."
1569 :expected-result (if (executable-find python-tests-shell-interpreter)
1570 :passed
1571 :failed)
1572 (python-tests-with-temp-file
1573 ""
1574 (let* ((python-shell-interpreter
1575 (executable-find python-tests-shell-interpreter))
1576 (global-proc-name (python-shell-get-process-name nil))
1577 (dedicated-proc-name (python-shell-get-process-name t))
1578 (global-shell-buffer
1579 (python-shell-make-comint
1580 (python-shell-parse-command) global-proc-name))
1581 (dedicated-shell-buffer
1582 (python-shell-make-comint
1583 (python-shell-parse-command) dedicated-proc-name))
1584 (global-process (get-buffer-process global-shell-buffer))
1585 (dedicated-process (get-buffer-process dedicated-shell-buffer)))
1586 (unwind-protect
1587 (progn
1588 (set-process-query-on-exit-flag global-process nil)
1589 (set-process-query-on-exit-flag dedicated-process nil)
1590 ;; Prefer dedicated if global also exists.
1591 (should (equal (python-shell-get-process) dedicated-process))
1592 (kill-buffer dedicated-shell-buffer)
1593 ;; If there's only global, use it.
1594 (should (equal (python-shell-get-process) global-process))
1595 (kill-buffer global-shell-buffer)
1596 ;; No buffer available.
1597 (should (not (python-shell-get-process))))
1598 (ignore-errors (kill-buffer global-shell-buffer))
1599 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
1600
1601 (ert-deftest python-shell-get-or-create-process-1 ()
1602 "Check shell process creation fallback."
1603 :expected-result :failed
1604 (python-tests-with-temp-file
1605 ""
1606 ;; XXX: Break early until we can skip stuff. We need to mimic
1607 ;; user interaction because `python-shell-get-or-create-process'
1608 ;; asks for all arguments interactively when a shell process
1609 ;; doesn't exist.
1610 (should nil)
1611 (let* ((python-shell-interpreter
1612 (executable-find python-tests-shell-interpreter))
1613 (use-dialog-box)
1614 (dedicated-process-name (python-shell-get-process-name t))
1615 (dedicated-process (python-shell-get-or-create-process))
1616 (dedicated-shell-buffer (process-buffer dedicated-process)))
1617 (unwind-protect
1618 (progn
1619 (set-process-query-on-exit-flag dedicated-process nil)
1620 ;; Prefer dedicated if not buffer exist.
1621 (should (equal (process-name dedicated-process)
1622 dedicated-process-name))
1623 (kill-buffer dedicated-shell-buffer)
1624 ;; No buffer available.
1625 (should (not (python-shell-get-process))))
1626 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
1627
1628 (ert-deftest python-shell-internal-get-or-create-process-1 ()
1629 "Check internal shell process creation fallback."
1630 :expected-result (if (executable-find python-tests-shell-interpreter)
1631 :passed
1632 :failed)
1633 (python-tests-with-temp-file
1634 ""
1635 (should (not (process-live-p (python-shell-internal-get-process-name))))
1636 (let* ((python-shell-interpreter
1637 (executable-find python-tests-shell-interpreter))
1638 (internal-process-name (python-shell-internal-get-process-name))
1639 (internal-process (python-shell-internal-get-or-create-process))
1640 (internal-shell-buffer (process-buffer internal-process)))
1641 (unwind-protect
1642 (progn
1643 (set-process-query-on-exit-flag internal-process nil)
1644 (should (equal (process-name internal-process)
1645 internal-process-name))
1646 (should (equal internal-process
1647 (python-shell-internal-get-or-create-process)))
1648 ;; No user buffer available.
1649 (should (not (python-shell-get-process)))
1650 (kill-buffer internal-shell-buffer))
1651 (ignore-errors (kill-buffer internal-shell-buffer))))))
1652
1653 \f
1654 ;;; Shell completion
1655
1656 \f
1657 ;;; PDB Track integration
1658
1659 \f
1660 ;;; Symbol completion
1661
1662 \f
1663 ;;; Fill paragraph
1664
1665 \f
1666 ;;; Skeletons
1667
1668 \f
1669 ;;; FFAP
1670
1671 \f
1672 ;;; Code check
1673
1674 \f
1675 ;;; Eldoc
1676
1677 \f
1678 ;;; Imenu
1679
1680 (ert-deftest python-imenu-create-index-1 ()
1681 (python-tests-with-temp-buffer
1682 "
1683 class Foo(models.Model):
1684 pass
1685
1686
1687 class Bar(models.Model):
1688 pass
1689
1690
1691 def decorator(arg1, arg2, arg3):
1692 '''print decorated function call data to stdout.
1693
1694 Usage:
1695
1696 @decorator('arg1', 'arg2')
1697 def func(a, b, c=True):
1698 pass
1699 '''
1700
1701 def wrap(f):
1702 print ('wrap')
1703 def wrapped_f(*args):
1704 print ('wrapped_f')
1705 print ('Decorator arguments:', arg1, arg2, arg3)
1706 f(*args)
1707 print ('called f(*args)')
1708 return wrapped_f
1709 return wrap
1710
1711
1712 class Baz(object):
1713
1714 def a(self):
1715 pass
1716
1717 def b(self):
1718 pass
1719
1720 class Frob(object):
1721
1722 def c(self):
1723 pass
1724 "
1725 (goto-char (point-max))
1726 (should (equal
1727 (list
1728 (cons "Foo (class)" (copy-marker 2))
1729 (cons "Bar (class)" (copy-marker 38))
1730 (list
1731 "decorator (def)"
1732 (cons "*function definition*" (copy-marker 74))
1733 (list
1734 "wrap (def)"
1735 (cons "*function definition*" (copy-marker 254))
1736 (cons "wrapped_f (def)" (copy-marker 294))))
1737 (list
1738 "Baz (class)"
1739 (cons "*class definition*" (copy-marker 519))
1740 (cons "a (def)" (copy-marker 539))
1741 (cons "b (def)" (copy-marker 570))
1742 (list
1743 "Frob (class)"
1744 (cons "*class definition*" (copy-marker 601))
1745 (cons "c (def)" (copy-marker 626)))))
1746 (python-imenu-create-index)))))
1747
1748 (ert-deftest python-imenu-create-flat-index-1 ()
1749 (python-tests-with-temp-buffer
1750 "
1751 class Foo(models.Model):
1752 pass
1753
1754
1755 class Bar(models.Model):
1756 pass
1757
1758
1759 def decorator(arg1, arg2, arg3):
1760 '''print decorated function call data to stdout.
1761
1762 Usage:
1763
1764 @decorator('arg1', 'arg2')
1765 def func(a, b, c=True):
1766 pass
1767 '''
1768
1769 def wrap(f):
1770 print ('wrap')
1771 def wrapped_f(*args):
1772 print ('wrapped_f')
1773 print ('Decorator arguments:', arg1, arg2, arg3)
1774 f(*args)
1775 print ('called f(*args)')
1776 return wrapped_f
1777 return wrap
1778
1779
1780 class Baz(object):
1781
1782 def a(self):
1783 pass
1784
1785 def b(self):
1786 pass
1787
1788 class Frob(object):
1789
1790 def c(self):
1791 pass
1792 "
1793 (goto-char (point-max))
1794 (should (equal
1795 (list (cons "Foo" (copy-marker 2))
1796 (cons "Bar" (copy-marker 38))
1797 (cons "decorator" (copy-marker 74))
1798 (cons "decorator.wrap" (copy-marker 254))
1799 (cons "decorator.wrap.wrapped_f" (copy-marker 294))
1800 (cons "Baz" (copy-marker 519))
1801 (cons "Baz.a" (copy-marker 539))
1802 (cons "Baz.b" (copy-marker 570))
1803 (cons "Baz.Frob" (copy-marker 601))
1804 (cons "Baz.Frob.c" (copy-marker 626)))
1805 (python-imenu-create-flat-index)))))
1806
1807 \f
1808 ;;; Misc helpers
1809
1810 (ert-deftest python-info-current-defun-1 ()
1811 (python-tests-with-temp-buffer
1812 "
1813 def foo(a, b):
1814 "
1815 (forward-line 1)
1816 (should (string= "foo" (python-info-current-defun)))
1817 (should (string= "def foo" (python-info-current-defun t)))
1818 (forward-line 1)
1819 (should (not (python-info-current-defun)))
1820 (indent-for-tab-command)
1821 (should (string= "foo" (python-info-current-defun)))
1822 (should (string= "def foo" (python-info-current-defun t)))))
1823
1824 (ert-deftest python-info-current-defun-2 ()
1825 (python-tests-with-temp-buffer
1826 "
1827 class C(object):
1828
1829 def m(self):
1830 if True:
1831 return [i for i in range(3)]
1832 else:
1833 return []
1834
1835 def b():
1836 do_b()
1837
1838 def a():
1839 do_a()
1840
1841 def c(self):
1842 do_c()
1843 "
1844 (forward-line 1)
1845 (should (string= "C" (python-info-current-defun)))
1846 (should (string= "class C" (python-info-current-defun t)))
1847 (python-tests-look-at "return [i for ")
1848 (should (string= "C.m" (python-info-current-defun)))
1849 (should (string= "def C.m" (python-info-current-defun t)))
1850 (python-tests-look-at "def b():")
1851 (should (string= "C.m.b" (python-info-current-defun)))
1852 (should (string= "def C.m.b" (python-info-current-defun t)))
1853 (forward-line 2)
1854 (indent-for-tab-command)
1855 (python-indent-dedent-line-backspace 1)
1856 (should (string= "C.m" (python-info-current-defun)))
1857 (should (string= "def C.m" (python-info-current-defun t)))
1858 (python-tests-look-at "def c(self):")
1859 (forward-line -1)
1860 (indent-for-tab-command)
1861 (should (string= "C.m.a" (python-info-current-defun)))
1862 (should (string= "def C.m.a" (python-info-current-defun t)))
1863 (python-indent-dedent-line-backspace 1)
1864 (should (string= "C.m" (python-info-current-defun)))
1865 (should (string= "def C.m" (python-info-current-defun t)))
1866 (python-indent-dedent-line-backspace 1)
1867 (should (string= "C" (python-info-current-defun)))
1868 (should (string= "class C" (python-info-current-defun t)))
1869 (python-tests-look-at "def c(self):")
1870 (should (string= "C.c" (python-info-current-defun)))
1871 (should (string= "def C.c" (python-info-current-defun t)))
1872 (python-tests-look-at "do_c()")
1873 (should (string= "C.c" (python-info-current-defun)))
1874 (should (string= "def C.c" (python-info-current-defun t)))))
1875
1876 (ert-deftest python-info-current-defun-3 ()
1877 (python-tests-with-temp-buffer
1878 "
1879 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1880 '''print decorated function call data to stdout.
1881
1882 Usage:
1883
1884 @decoratorFunctionWithArguments('arg1', 'arg2')
1885 def func(a, b, c=True):
1886 pass
1887 '''
1888
1889 def wwrap(f):
1890 print 'Inside wwrap()'
1891 def wrapped_f(*args):
1892 print 'Inside wrapped_f()'
1893 print 'Decorator arguments:', arg1, arg2, arg3
1894 f(*args)
1895 print 'After f(*args)'
1896 return wrapped_f
1897 return wwrap
1898 "
1899 (python-tests-look-at "def wwrap(f):")
1900 (forward-line -1)
1901 (should (not (python-info-current-defun)))
1902 (indent-for-tab-command 1)
1903 (should (string= (python-info-current-defun)
1904 "decoratorFunctionWithArguments"))
1905 (should (string= (python-info-current-defun t)
1906 "def decoratorFunctionWithArguments"))
1907 (python-tests-look-at "def wrapped_f(*args):")
1908 (should (string= (python-info-current-defun)
1909 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
1910 (should (string= (python-info-current-defun t)
1911 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
1912 (python-tests-look-at "return wrapped_f")
1913 (should (string= (python-info-current-defun)
1914 "decoratorFunctionWithArguments.wwrap"))
1915 (should (string= (python-info-current-defun t)
1916 "def decoratorFunctionWithArguments.wwrap"))
1917 (end-of-line 1)
1918 (python-tests-look-at "return wwrap")
1919 (should (string= (python-info-current-defun)
1920 "decoratorFunctionWithArguments"))
1921 (should (string= (python-info-current-defun t)
1922 "def decoratorFunctionWithArguments"))))
1923
1924 (ert-deftest python-info-current-symbol-1 ()
1925 (python-tests-with-temp-buffer
1926 "
1927 class C(object):
1928
1929 def m(self):
1930 self.c()
1931
1932 def c(self):
1933 print ('a')
1934 "
1935 (python-tests-look-at "self.c()")
1936 (should (string= "self.c" (python-info-current-symbol)))
1937 (should (string= "C.c" (python-info-current-symbol t)))))
1938
1939 (ert-deftest python-info-current-symbol-2 ()
1940 (python-tests-with-temp-buffer
1941 "
1942 class C(object):
1943
1944 class M(object):
1945
1946 def a(self):
1947 self.c()
1948
1949 def c(self):
1950 pass
1951 "
1952 (python-tests-look-at "self.c()")
1953 (should (string= "self.c" (python-info-current-symbol)))
1954 (should (string= "C.M.c" (python-info-current-symbol t)))))
1955
1956 (ert-deftest python-info-current-symbol-3 ()
1957 "Keywords should not be considered symbols."
1958 :expected-result :failed
1959 (python-tests-with-temp-buffer
1960 "
1961 class C(object):
1962 pass
1963 "
1964 ;; FIXME: keywords are not symbols.
1965 (python-tests-look-at "class C")
1966 (should (not (python-info-current-symbol)))
1967 (should (not (python-info-current-symbol t)))
1968 (python-tests-look-at "C(object)")
1969 (should (string= "C" (python-info-current-symbol)))
1970 (should (string= "class C" (python-info-current-symbol t)))))
1971
1972 (ert-deftest python-info-statement-starts-block-p-1 ()
1973 (python-tests-with-temp-buffer
1974 "
1975 def long_function_name(
1976 var_one, var_two, var_three,
1977 var_four):
1978 print (var_one)
1979 "
1980 (python-tests-look-at "def long_function_name")
1981 (should (python-info-statement-starts-block-p))
1982 (python-tests-look-at "print (var_one)")
1983 (python-util-forward-comment -1)
1984 (should (python-info-statement-starts-block-p))))
1985
1986 (ert-deftest python-info-statement-starts-block-p-2 ()
1987 (python-tests-with-temp-buffer
1988 "
1989 if width == 0 and height == 0 and \\\\
1990 color == 'red' and emphasis == 'strong' or \\\\
1991 highlight > 100:
1992 raise ValueError('sorry, you lose')
1993 "
1994 (python-tests-look-at "if width == 0 and")
1995 (should (python-info-statement-starts-block-p))
1996 (python-tests-look-at "raise ValueError(")
1997 (python-util-forward-comment -1)
1998 (should (python-info-statement-starts-block-p))))
1999
2000 (ert-deftest python-info-statement-ends-block-p-1 ()
2001 (python-tests-with-temp-buffer
2002 "
2003 def long_function_name(
2004 var_one, var_two, var_three,
2005 var_four):
2006 print (var_one)
2007 "
2008 (python-tests-look-at "print (var_one)")
2009 (should (python-info-statement-ends-block-p))))
2010
2011 (ert-deftest python-info-statement-ends-block-p-2 ()
2012 (python-tests-with-temp-buffer
2013 "
2014 if width == 0 and height == 0 and \\\\
2015 color == 'red' and emphasis == 'strong' or \\\\
2016 highlight > 100:
2017 raise ValueError(
2018 'sorry, you lose'
2019
2020 )
2021 "
2022 (python-tests-look-at "raise ValueError(")
2023 (should (python-info-statement-ends-block-p))))
2024
2025 (ert-deftest python-info-beginning-of-statement-p-1 ()
2026 (python-tests-with-temp-buffer
2027 "
2028 def long_function_name(
2029 var_one, var_two, var_three,
2030 var_four):
2031 print (var_one)
2032 "
2033 (python-tests-look-at "def long_function_name")
2034 (should (python-info-beginning-of-statement-p))
2035 (forward-char 10)
2036 (should (not (python-info-beginning-of-statement-p)))
2037 (python-tests-look-at "print (var_one)")
2038 (should (python-info-beginning-of-statement-p))
2039 (goto-char (line-beginning-position))
2040 (should (not (python-info-beginning-of-statement-p)))))
2041
2042 (ert-deftest python-info-beginning-of-statement-p-2 ()
2043 (python-tests-with-temp-buffer
2044 "
2045 if width == 0 and height == 0 and \\\\
2046 color == 'red' and emphasis == 'strong' or \\\\
2047 highlight > 100:
2048 raise ValueError(
2049 'sorry, you lose'
2050
2051 )
2052 "
2053 (python-tests-look-at "if width == 0 and")
2054 (should (python-info-beginning-of-statement-p))
2055 (forward-char 10)
2056 (should (not (python-info-beginning-of-statement-p)))
2057 (python-tests-look-at "raise ValueError(")
2058 (should (python-info-beginning-of-statement-p))
2059 (goto-char (line-beginning-position))
2060 (should (not (python-info-beginning-of-statement-p)))))
2061
2062 (ert-deftest python-info-end-of-statement-p-1 ()
2063 (python-tests-with-temp-buffer
2064 "
2065 def long_function_name(
2066 var_one, var_two, var_three,
2067 var_four):
2068 print (var_one)
2069 "
2070 (python-tests-look-at "def long_function_name")
2071 (should (not (python-info-end-of-statement-p)))
2072 (end-of-line)
2073 (should (not (python-info-end-of-statement-p)))
2074 (python-tests-look-at "print (var_one)")
2075 (python-util-forward-comment -1)
2076 (should (python-info-end-of-statement-p))
2077 (python-tests-look-at "print (var_one)")
2078 (should (not (python-info-end-of-statement-p)))
2079 (end-of-line)
2080 (should (python-info-end-of-statement-p))))
2081
2082 (ert-deftest python-info-end-of-statement-p-2 ()
2083 (python-tests-with-temp-buffer
2084 "
2085 if width == 0 and height == 0 and \\\\
2086 color == 'red' and emphasis == 'strong' or \\\\
2087 highlight > 100:
2088 raise ValueError(
2089 'sorry, you lose'
2090
2091 )
2092 "
2093 (python-tests-look-at "if width == 0 and")
2094 (should (not (python-info-end-of-statement-p)))
2095 (end-of-line)
2096 (should (not (python-info-end-of-statement-p)))
2097 (python-tests-look-at "raise ValueError(")
2098 (python-util-forward-comment -1)
2099 (should (python-info-end-of-statement-p))
2100 (python-tests-look-at "raise ValueError(")
2101 (should (not (python-info-end-of-statement-p)))
2102 (end-of-line)
2103 (should (not (python-info-end-of-statement-p)))
2104 (goto-char (point-max))
2105 (python-util-forward-comment -1)
2106 (should (python-info-end-of-statement-p))))
2107
2108 (ert-deftest python-info-beginning-of-block-p-1 ()
2109 (python-tests-with-temp-buffer
2110 "
2111 def long_function_name(
2112 var_one, var_two, var_three,
2113 var_four):
2114 print (var_one)
2115 "
2116 (python-tests-look-at "def long_function_name")
2117 (should (python-info-beginning-of-block-p))
2118 (python-tests-look-at "var_one, var_two, var_three,")
2119 (should (not (python-info-beginning-of-block-p)))
2120 (python-tests-look-at "print (var_one)")
2121 (should (not (python-info-beginning-of-block-p)))))
2122
2123 (ert-deftest python-info-beginning-of-block-p-2 ()
2124 (python-tests-with-temp-buffer
2125 "
2126 if width == 0 and height == 0 and \\\\
2127 color == 'red' and emphasis == 'strong' or \\\\
2128 highlight > 100:
2129 raise ValueError(
2130 'sorry, you lose'
2131
2132 )
2133 "
2134 (python-tests-look-at "if width == 0 and")
2135 (should (python-info-beginning-of-block-p))
2136 (python-tests-look-at "color == 'red' and emphasis")
2137 (should (not (python-info-beginning-of-block-p)))
2138 (python-tests-look-at "raise ValueError(")
2139 (should (not (python-info-beginning-of-block-p)))))
2140
2141 (ert-deftest python-info-end-of-block-p-1 ()
2142 (python-tests-with-temp-buffer
2143 "
2144 def long_function_name(
2145 var_one, var_two, var_three,
2146 var_four):
2147 print (var_one)
2148 "
2149 (python-tests-look-at "def long_function_name")
2150 (should (not (python-info-end-of-block-p)))
2151 (python-tests-look-at "var_one, var_two, var_three,")
2152 (should (not (python-info-end-of-block-p)))
2153 (python-tests-look-at "var_four):")
2154 (end-of-line)
2155 (should (not (python-info-end-of-block-p)))
2156 (python-tests-look-at "print (var_one)")
2157 (should (not (python-info-end-of-block-p)))
2158 (end-of-line 1)
2159 (should (python-info-end-of-block-p))))
2160
2161 (ert-deftest python-info-end-of-block-p-2 ()
2162 (python-tests-with-temp-buffer
2163 "
2164 if width == 0 and height == 0 and \\\\
2165 color == 'red' and emphasis == 'strong' or \\\\
2166 highlight > 100:
2167 raise ValueError(
2168 'sorry, you lose'
2169
2170 )
2171 "
2172 (python-tests-look-at "if width == 0 and")
2173 (should (not (python-info-end-of-block-p)))
2174 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2175 (should (not (python-info-end-of-block-p)))
2176 (python-tests-look-at "highlight > 100:")
2177 (end-of-line)
2178 (should (not (python-info-end-of-block-p)))
2179 (python-tests-look-at "raise ValueError(")
2180 (should (not (python-info-end-of-block-p)))
2181 (end-of-line 1)
2182 (should (not (python-info-end-of-block-p)))
2183 (goto-char (point-max))
2184 (python-util-forward-comment -1)
2185 (should (python-info-end-of-block-p))))
2186
2187 (ert-deftest python-info-closing-block-1 ()
2188 (python-tests-with-temp-buffer
2189 "
2190 if request.user.is_authenticated():
2191 try:
2192 profile = request.user.get_profile()
2193 except Profile.DoesNotExist:
2194 profile = Profile.objects.create(user=request.user)
2195 else:
2196 if profile.stats:
2197 profile.recalculate_stats()
2198 else:
2199 profile.clear_stats()
2200 finally:
2201 profile.views += 1
2202 profile.save()
2203 "
2204 (python-tests-look-at "try:")
2205 (should (not (python-info-closing-block)))
2206 (python-tests-look-at "except Profile.DoesNotExist:")
2207 (should (= (python-tests-look-at "try:" -1 t)
2208 (python-info-closing-block)))
2209 (python-tests-look-at "else:")
2210 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -1 t)
2211 (python-info-closing-block)))
2212 (python-tests-look-at "if profile.stats:")
2213 (should (not (python-info-closing-block)))
2214 (python-tests-look-at "else:")
2215 (should (= (python-tests-look-at "if profile.stats:" -1 t)
2216 (python-info-closing-block)))
2217 (python-tests-look-at "finally:")
2218 (should (= (python-tests-look-at "else:" -2 t)
2219 (python-info-closing-block)))))
2220
2221 (ert-deftest python-info-closing-block-2 ()
2222 (python-tests-with-temp-buffer
2223 "
2224 if request.user.is_authenticated():
2225 profile = Profile.objects.get_or_create(user=request.user)
2226 if profile.stats:
2227 profile.recalculate_stats()
2228
2229 data = {
2230 'else': 'do it'
2231 }
2232 'else'
2233 "
2234 (python-tests-look-at "'else': 'do it'")
2235 (should (not (python-info-closing-block)))
2236 (python-tests-look-at "'else'")
2237 (should (not (python-info-closing-block)))))
2238
2239 (ert-deftest python-info-line-ends-backslash-p-1 ()
2240 (python-tests-with-temp-buffer
2241 "
2242 objects = Thing.objects.all() \\\\
2243 .filter(
2244 type='toy',
2245 status='bought'
2246 ) \\\\
2247 .aggregate(
2248 Sum('amount')
2249 ) \\\\
2250 .values_list()
2251 "
2252 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
2253 (should (python-info-line-ends-backslash-p 3))
2254 (should (python-info-line-ends-backslash-p 4))
2255 (should (python-info-line-ends-backslash-p 5))
2256 (should (python-info-line-ends-backslash-p 6)) ; ) \...
2257 (should (python-info-line-ends-backslash-p 7))
2258 (should (python-info-line-ends-backslash-p 8))
2259 (should (python-info-line-ends-backslash-p 9))
2260 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
2261
2262 (ert-deftest python-info-beginning-of-backslash-1 ()
2263 (python-tests-with-temp-buffer
2264 "
2265 objects = Thing.objects.all() \\\\
2266 .filter(
2267 type='toy',
2268 status='bought'
2269 ) \\\\
2270 .aggregate(
2271 Sum('amount')
2272 ) \\\\
2273 .values_list()
2274 "
2275 (let ((first 2)
2276 (second (python-tests-look-at ".filter("))
2277 (third (python-tests-look-at ".aggregate(")))
2278 (should (= first (python-info-beginning-of-backslash 2)))
2279 (should (= second (python-info-beginning-of-backslash 3)))
2280 (should (= second (python-info-beginning-of-backslash 4)))
2281 (should (= second (python-info-beginning-of-backslash 5)))
2282 (should (= second (python-info-beginning-of-backslash 6)))
2283 (should (= third (python-info-beginning-of-backslash 7)))
2284 (should (= third (python-info-beginning-of-backslash 8)))
2285 (should (= third (python-info-beginning-of-backslash 9)))
2286 (should (not (python-info-beginning-of-backslash 10))))))
2287
2288 (ert-deftest python-info-continuation-line-p-1 ()
2289 (python-tests-with-temp-buffer
2290 "
2291 if width == 0 and height == 0 and \\\\
2292 color == 'red' and emphasis == 'strong' or \\\\
2293 highlight > 100:
2294 raise ValueError(
2295 'sorry, you lose'
2296
2297 )
2298 "
2299 (python-tests-look-at "if width == 0 and height == 0 and")
2300 (should (not (python-info-continuation-line-p)))
2301 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2302 (should (python-info-continuation-line-p))
2303 (python-tests-look-at "highlight > 100:")
2304 (should (python-info-continuation-line-p))
2305 (python-tests-look-at "raise ValueError(")
2306 (should (not (python-info-continuation-line-p)))
2307 (python-tests-look-at "'sorry, you lose'")
2308 (should (python-info-continuation-line-p))
2309 (forward-line 1)
2310 (should (python-info-continuation-line-p))
2311 (python-tests-look-at ")")
2312 (should (python-info-continuation-line-p))
2313 (forward-line 1)
2314 (should (not (python-info-continuation-line-p)))))
2315
2316 (ert-deftest python-info-block-continuation-line-p-1 ()
2317 (python-tests-with-temp-buffer
2318 "
2319 if width == 0 and height == 0 and \\\\
2320 color == 'red' and emphasis == 'strong' or \\\\
2321 highlight > 100:
2322 raise ValueError(
2323 'sorry, you lose'
2324
2325 )
2326 "
2327 (python-tests-look-at "if width == 0 and")
2328 (should (not (python-info-block-continuation-line-p)))
2329 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2330 (should (= (python-info-block-continuation-line-p)
2331 (python-tests-look-at "if width == 0 and" -1 t)))
2332 (python-tests-look-at "highlight > 100:")
2333 (should (not (python-info-block-continuation-line-p)))))
2334
2335 (ert-deftest python-info-block-continuation-line-p-2 ()
2336 (python-tests-with-temp-buffer
2337 "
2338 def foo(a,
2339 b,
2340 c):
2341 pass
2342 "
2343 (python-tests-look-at "def foo(a,")
2344 (should (not (python-info-block-continuation-line-p)))
2345 (python-tests-look-at "b,")
2346 (should (= (python-info-block-continuation-line-p)
2347 (python-tests-look-at "def foo(a," -1 t)))
2348 (python-tests-look-at "c):")
2349 (should (not (python-info-block-continuation-line-p)))))
2350
2351 (ert-deftest python-info-assignment-continuation-line-p-1 ()
2352 (python-tests-with-temp-buffer
2353 "
2354 data = foo(), bar() \\\\
2355 baz(), 4 \\\\
2356 5, 6
2357 "
2358 (python-tests-look-at "data = foo(), bar()")
2359 (should (not (python-info-assignment-continuation-line-p)))
2360 (python-tests-look-at "baz(), 4")
2361 (should (= (python-info-assignment-continuation-line-p)
2362 (python-tests-look-at "foo()," -1 t)))
2363 (python-tests-look-at "5, 6")
2364 (should (not (python-info-assignment-continuation-line-p)))))
2365
2366 (ert-deftest python-info-assignment-continuation-line-p-2 ()
2367 (python-tests-with-temp-buffer
2368 "
2369 data = (foo(), bar()
2370 baz(), 4
2371 5, 6)
2372 "
2373 (python-tests-look-at "data = (foo(), bar()")
2374 (should (not (python-info-assignment-continuation-line-p)))
2375 (python-tests-look-at "baz(), 4")
2376 (should (= (python-info-assignment-continuation-line-p)
2377 (python-tests-look-at "(foo()," -1 t)))
2378 (python-tests-look-at "5, 6)")
2379 (should (not (python-info-assignment-continuation-line-p)))))
2380
2381 (ert-deftest python-info-looking-at-beginning-of-defun-1 ()
2382 (python-tests-with-temp-buffer
2383 "
2384 def decorat0r(deff):
2385 '''decorates stuff.
2386
2387 @decorat0r
2388 def foo(arg):
2389 ...
2390 '''
2391 def wrap():
2392 deff()
2393 return wwrap
2394 "
2395 (python-tests-look-at "def decorat0r(deff):")
2396 (should (python-info-looking-at-beginning-of-defun))
2397 (python-tests-look-at "def foo(arg):")
2398 (should (not (python-info-looking-at-beginning-of-defun)))
2399 (python-tests-look-at "def wrap():")
2400 (should (python-info-looking-at-beginning-of-defun))
2401 (python-tests-look-at "deff()")
2402 (should (not (python-info-looking-at-beginning-of-defun)))))
2403
2404 (ert-deftest python-info-current-line-comment-p-1 ()
2405 (python-tests-with-temp-buffer
2406 "
2407 # this is a comment
2408 foo = True # another comment
2409 '#this is a string'
2410 if foo:
2411 # more comments
2412 print ('bar') # print bar
2413 "
2414 (python-tests-look-at "# this is a comment")
2415 (should (python-info-current-line-comment-p))
2416 (python-tests-look-at "foo = True # another comment")
2417 (should (not (python-info-current-line-comment-p)))
2418 (python-tests-look-at "'#this is a string'")
2419 (should (not (python-info-current-line-comment-p)))
2420 (python-tests-look-at "# more comments")
2421 (should (python-info-current-line-comment-p))
2422 (python-tests-look-at "print ('bar') # print bar")
2423 (should (not (python-info-current-line-comment-p)))))
2424
2425 (ert-deftest python-info-current-line-empty-p ()
2426 (python-tests-with-temp-buffer
2427 "
2428 # this is a comment
2429
2430 foo = True # another comment
2431 "
2432 (should (python-info-current-line-empty-p))
2433 (python-tests-look-at "# this is a comment")
2434 (should (not (python-info-current-line-empty-p)))
2435 (forward-line 1)
2436 (should (python-info-current-line-empty-p))))
2437
2438 \f
2439 ;;; Utility functions
2440
2441 (ert-deftest python-util-goto-line-1 ()
2442 (python-tests-with-temp-buffer
2443 (concat
2444 "# a comment
2445 # another comment
2446 def foo(a, b, c):
2447 pass" (make-string 20 ?\n))
2448 (python-util-goto-line 10)
2449 (should (= (line-number-at-pos) 10))
2450 (python-util-goto-line 20)
2451 (should (= (line-number-at-pos) 20))))
2452
2453 (ert-deftest python-util-clone-local-variables-1 ()
2454 (let ((buffer (generate-new-buffer
2455 "python-util-clone-local-variables-1"))
2456 (varcons
2457 '((python-fill-docstring-style . django)
2458 (python-shell-interpreter . "python")
2459 (python-shell-interpreter-args . "manage.py shell")
2460 (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
2461 (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
2462 (python-shell-extra-pythonpaths "/home/user/pylib/")
2463 (python-shell-completion-setup-code
2464 . "from IPython.core.completerlib import module_completion")
2465 (python-shell-completion-module-string-code
2466 . "';'.join(module_completion('''%s'''))\n")
2467 (python-shell-completion-string-code
2468 . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
2469 (python-shell-virtualenv-path
2470 . "/home/user/.virtualenvs/project"))))
2471 (with-current-buffer buffer
2472 (kill-all-local-variables)
2473 (dolist (ccons varcons)
2474 (set (make-local-variable (car ccons)) (cdr ccons))))
2475 (python-tests-with-temp-buffer
2476 ""
2477 (python-util-clone-local-variables buffer)
2478 (dolist (ccons varcons)
2479 (should
2480 (equal (symbol-value (car ccons)) (cdr ccons)))))
2481 (kill-buffer buffer)))
2482
2483 (ert-deftest python-util-forward-comment-1 ()
2484 (python-tests-with-temp-buffer
2485 (concat
2486 "# a comment
2487 # another comment
2488 # bad indented comment
2489 # more comments" (make-string 9999 ?\n))
2490 (python-util-forward-comment 1)
2491 (should (= (point) (point-max)))
2492 (python-util-forward-comment -1)
2493 (should (= (point) (point-min)))))
2494
2495
2496 (provide 'python-tests)
2497
2498 ;; Local Variables:
2499 ;; coding: utf-8
2500 ;; indent-tabs-mode: nil
2501 ;; End:
2502
2503 ;;; python-tests.el ends here