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