aboutsummaryrefslogtreecommitdiffstats
path: root/test/lisp/progmodes/python-tests.el
diff options
context:
space:
mode:
authorkobarity2023-02-14 00:30:15 +0900
committerEli Zaretskii2023-02-18 18:43:11 +0200
commit5190ea6259a5fd13ba5e87b92b20f450658cf532 (patch)
tree2ba3e8c9b0476c88d833a604a0a993064a9eb970 /test/lisp/progmodes/python-tests.el
parent6c0d8210175e72dcd7cef2ad77b8f8b680b240bc (diff)
downloademacs-5190ea6259a5fd13ba5e87b92b20f450658cf532.tar.gz
emacs-5190ea6259a5fd13ba5e87b92b20f450658cf532.zip
Fix point moving when calling python-shell-send-region
* lisp/progmodes/python.el (python-shell-buffer-substring): Add `save-excursion' to prevent the point from moving. * test/lisp/progmodes/python-tests.el (python-tests-should-not-move): New helper function to assert that point does not move while calling a function. (python-shell-buffer-substring-*): Use `python-tests-should-not-move'. (Bug#61463)
Diffstat (limited to 'test/lisp/progmodes/python-tests.el')
-rw-r--r--test/lisp/progmodes/python-tests.el62
1 files changed, 44 insertions, 18 deletions
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
index df71990278e..4f24c042c6a 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -189,6 +189,14 @@ default to `point-min' and `point-max' respectively."
189 (overlay-end overlay)))) 189 (overlay-end overlay))))
190 (buffer-substring-no-properties (point-min) (point-max))))) 190 (buffer-substring-no-properties (point-min) (point-max)))))
191 191
192(defun python-tests-should-not-move (func &rest args)
193 "Assert that point does not move while calling FUNC with ARGS.
194Returns the value returned by FUNC."
195 (let ((pos (point))
196 (ret (apply func args)))
197 (should (= pos (point)))
198 ret))
199
192(defun python-virt-bin (&optional virt-root) 200(defun python-virt-bin (&optional virt-root)
193 "Return the virtualenv bin dir, starting from VIRT-ROOT. 201 "Return the virtualenv bin dir, starting from VIRT-ROOT.
194If nil, VIRT-ROOT defaults to `python-shell-virtualenv-root'. 202If nil, VIRT-ROOT defaults to `python-shell-virtualenv-root'.
@@ -4213,7 +4221,8 @@ class Bar(models.Model):
4213 pass 4221 pass
4214" 4222"
4215 (should (string= (buffer-string) 4223 (should (string= (buffer-string)
4216 (python-shell-buffer-substring (point-min) (point-max)))))) 4224 (python-tests-should-not-move
4225 #'python-shell-buffer-substring (point-min) (point-max))))))
4217 4226
4218(ert-deftest python-shell-buffer-substring-2 () 4227(ert-deftest python-shell-buffer-substring-2 ()
4219 "Main block should be removed if NOMAIN is non-nil." 4228 "Main block should be removed if NOMAIN is non-nil."
@@ -4229,7 +4238,8 @@ if __name__ == \"__main__\":
4229 foo = Foo() 4238 foo = Foo()
4230 print (foo) 4239 print (foo)
4231" 4240"
4232 (should (string= (python-shell-buffer-substring (point-min) (point-max) t) 4241 (should (string= (python-tests-should-not-move
4242 #'python-shell-buffer-substring (point-min) (point-max) t)
4233 " 4243 "
4234class Foo(models.Model): 4244class Foo(models.Model):
4235 pass 4245 pass
@@ -4256,7 +4266,8 @@ if __name__ == \"__main__\":
4256class Bar(models.Model): 4266class Bar(models.Model):
4257 pass 4267 pass
4258" 4268"
4259 (should (string= (python-shell-buffer-substring (point-min) (point-max) t) 4269 (should (string= (python-tests-should-not-move
4270 #'python-shell-buffer-substring (point-min) (point-max) t)
4260 " 4271 "
4261class Foo(models.Model): 4272class Foo(models.Model):
4262 pass 4273 pass
@@ -4284,7 +4295,8 @@ if __name__ == \"__main__\":
4284class Bar(models.Model): 4295class Bar(models.Model):
4285 pass 4296 pass
4286" 4297"
4287 (should (string= (python-shell-buffer-substring 4298 (should (string= (python-tests-should-not-move
4299 #'python-shell-buffer-substring
4288 (python-tests-look-at "class Foo(models.Model):") 4300 (python-tests-look-at "class Foo(models.Model):")
4289 (progn (python-nav-forward-sexp) (point))) 4301 (progn (python-nav-forward-sexp) (point)))
4290 "# -*- coding: latin-1 -*- 4302 "# -*- coding: latin-1 -*-
@@ -4307,7 +4319,8 @@ if __name__ == \"__main__\":
4307class Bar(models.Model): 4319class Bar(models.Model):
4308 pass 4320 pass
4309" 4321"
4310 (should (string= (python-shell-buffer-substring 4322 (should (string= (python-tests-should-not-move
4323 #'python-shell-buffer-substring
4311 (python-tests-look-at "class Bar(models.Model):") 4324 (python-tests-look-at "class Bar(models.Model):")
4312 (progn (python-nav-forward-sexp) (point))) 4325 (progn (python-nav-forward-sexp) (point)))
4313 "# -*- coding: latin-1 -*- 4326 "# -*- coding: latin-1 -*-
@@ -4338,7 +4351,8 @@ if __name__ == \"__main__\":
4338class Bar(models.Model): 4351class Bar(models.Model):
4339 pass 4352 pass
4340" 4353"
4341 (should (string= (python-shell-buffer-substring 4354 (should (string= (python-tests-should-not-move
4355 #'python-shell-buffer-substring
4342 (python-tests-look-at "# coding: latin-1") 4356 (python-tests-look-at "# coding: latin-1")
4343 (python-tests-look-at "if __name__ == \"__main__\":")) 4357 (python-tests-look-at "if __name__ == \"__main__\":"))
4344 "# -*- coding: latin-1 -*- 4358 "# -*- coding: latin-1 -*-
@@ -4365,7 +4379,8 @@ if __name__ == \"__main__\":
4365class Bar(models.Model): 4379class Bar(models.Model):
4366 pass 4380 pass
4367" 4381"
4368 (should (string= (python-shell-buffer-substring 4382 (should (string= (python-tests-should-not-move
4383 #'python-shell-buffer-substring
4369 (python-tests-look-at "# coding: latin-1") 4384 (python-tests-look-at "# coding: latin-1")
4370 (python-tests-look-at "if __name__ == \"__main__\":")) 4385 (python-tests-look-at "if __name__ == \"__main__\":"))
4371 "# -*- coding: utf-8 -*- 4386 "# -*- coding: utf-8 -*-
@@ -4385,7 +4400,8 @@ class Foo(models.Model):
4385class Foo(models.Model): 4400class Foo(models.Model):
4386 pass 4401 pass
4387" 4402"
4388 (should (string= (python-shell-buffer-substring (point-min) (point-max)) 4403 (should (string= (python-tests-should-not-move
4404 #'python-shell-buffer-substring (point-min) (point-max))
4389 "# coding: utf-8 4405 "# coding: utf-8
4390 4406
4391 4407
@@ -4404,7 +4420,8 @@ class Foo(models.Model):
4404class Bar(models.Model): 4420class Bar(models.Model):
4405 pass 4421 pass
4406" 4422"
4407 (should (string= (python-shell-buffer-substring 4423 (should (string= (python-tests-should-not-move
4424 #'python-shell-buffer-substring
4408 (point-min) 4425 (point-min)
4409 (python-tests-look-at "class Bar(models.Model):")) 4426 (python-tests-look-at "class Bar(models.Model):"))
4410 "# coding: utf-8 4427 "# coding: utf-8
@@ -4421,7 +4438,8 @@ class Foo(models.Model):
4421def foo(): 4438def foo():
4422 print ('a') 4439 print ('a')
4423" 4440"
4424 (should (string= (python-shell-buffer-substring 4441 (should (string= (python-tests-should-not-move
4442 #'python-shell-buffer-substring
4425 (python-tests-look-at "print ('a')") 4443 (python-tests-look-at "print ('a')")
4426 (point-max)) 4444 (point-max))
4427 "# -*- coding: utf-8 -*-\nif True:\n print ('a')\n\n")))) 4445 "# -*- coding: utf-8 -*-\nif True:\n print ('a')\n\n"))))
@@ -4433,7 +4451,8 @@ def foo():
4433def foo(): 4451def foo():
4434 print ('a') 4452 print ('a')
4435" 4453"
4436 (should (string= (python-shell-buffer-substring 4454 (should (string= (python-tests-should-not-move
4455 #'python-shell-buffer-substring
4437 (progn 4456 (progn
4438 (python-tests-look-at "print ('a')") 4457 (python-tests-look-at "print ('a')")
4439 (backward-char 1) 4458 (backward-char 1)
@@ -4451,7 +4470,8 @@ def foo():
4451 4470
4452 print ('a') 4471 print ('a')
4453" 4472"
4454 (should (string= (python-shell-buffer-substring 4473 (should (string= (python-tests-should-not-move
4474 #'python-shell-buffer-substring
4455 (python-tests-look-at "# Whitespace") 4475 (python-tests-look-at "# Whitespace")
4456 (point-max)) 4476 (point-max))
4457 "# -*- coding: utf-8 -*-\n\nif True:\n # Whitespace\n\n print ('a')\n\n")))) 4477 "# -*- coding: utf-8 -*-\n\nif True:\n # Whitespace\n\n print ('a')\n\n"))))
@@ -4463,7 +4483,8 @@ def foo():
4463def foo(): 4483def foo():
4464 a = 1 4484 a = 1
4465" 4485"
4466 (should (string= (python-shell-buffer-substring 4486 (should (string= (python-tests-should-not-move
4487 #'python-shell-buffer-substring
4467 (python-tests-look-at "a = 1") 4488 (python-tests-look-at "a = 1")
4468 (pos-eol)) 4489 (pos-eol))
4469 "# -*- coding: utf-8 -*-\n\na = 1")))) 4490 "# -*- coding: utf-8 -*-\n\na = 1"))))
@@ -4476,7 +4497,8 @@ def foo():
4476 a = \"\"\"Some 4497 a = \"\"\"Some
4477 string\"\"\" 4498 string\"\"\"
4478" 4499"
4479 (should (string= (python-shell-buffer-substring 4500 (should (string= (python-tests-should-not-move
4501 #'python-shell-buffer-substring
4480 (python-tests-look-at "a = \"\"\"Some") 4502 (python-tests-look-at "a = \"\"\"Some")
4481 (pos-eol 2)) 4503 (pos-eol 2))
4482 "# -*- coding: utf-8 -*-\n\na = \"\"\"Some\n string\"\"\"")))) 4504 "# -*- coding: utf-8 -*-\n\na = \"\"\"Some\n string\"\"\""))))
@@ -4488,7 +4510,8 @@ def foo():
4488def foo(): 4510def foo():
4489 a = 1 4511 a = 1
4490" 4512"
4491 (should (string= (python-shell-buffer-substring 4513 (should (string= (python-tests-should-not-move
4514 #'python-shell-buffer-substring
4492 (python-tests-look-at " a = 1") 4515 (python-tests-look-at " a = 1")
4493 (python-tests-look-at " = 1")) 4516 (python-tests-look-at " = 1"))
4494 "# -*- coding: utf-8 -*-\n\na")))) 4517 "# -*- coding: utf-8 -*-\n\na"))))
@@ -4500,7 +4523,8 @@ def foo():
4500def foo(): 4523def foo():
4501 a = 1 4524 a = 1
4502" 4525"
4503 (should (string= (python-shell-buffer-substring 4526 (should (string= (python-tests-should-not-move
4527 #'python-shell-buffer-substring
4504 (python-tests-look-at "1") 4528 (python-tests-look-at "1")
4505 (1+ (point))) 4529 (1+ (point)))
4506 "# -*- coding: utf-8 -*-\n\n1")))) 4530 "# -*- coding: utf-8 -*-\n\n1"))))
@@ -4515,7 +4539,8 @@ def foo():
4515 b = 2 4539 b = 2
4516\"\"\" 4540\"\"\"
4517" 4541"
4518 (should (string= (python-shell-buffer-substring 4542 (should (string= (python-tests-should-not-move
4543 #'python-shell-buffer-substring
4519 (python-tests-look-at "a = 1") 4544 (python-tests-look-at "a = 1")
4520 (python-tests-look-at "\"\"\"")) 4545 (python-tests-look-at "\"\"\""))
4521 "# -*- coding: utf-8 -*-\n\nif True:\n a = 1\n b = 2\n\n")))) 4546 "# -*- coding: utf-8 -*-\n\nif True:\n a = 1\n b = 2\n\n"))))
@@ -4525,7 +4550,8 @@ def foo():
4525 (python-tests-with-temp-buffer 4550 (python-tests-with-temp-buffer
4526 "s = 'test' 4551 "s = 'test'
4527" 4552"
4528 (should (string= (python-shell-buffer-substring 4553 (should (string= (python-tests-should-not-move
4554 #'python-shell-buffer-substring
4529 (python-tests-look-at "'test'") 4555 (python-tests-look-at "'test'")
4530 (pos-eol)) 4556 (pos-eol))
4531 "'test'")))) 4557 "'test'"))))