aboutsummaryrefslogtreecommitdiffstats
path: root/test/automated/python-tests.el
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina2014-12-27 01:30:21 -0300
committerFabián Ezequiel Gallina2014-12-27 01:30:21 -0300
commit2dd5163d764f395eb31a2306dba385d123af4aba (patch)
tree27b1ade1b6af5e984040a8acc840f81ee4f5adaa /test/automated/python-tests.el
parent7aa506eed8881788485a9774165454404bac2623 (diff)
downloademacs-2dd5163d764f395eb31a2306dba385d123af4aba.tar.gz
emacs-2dd5163d764f395eb31a2306dba385d123af4aba.zip
python.el: Handle file encoding for shell.
* lisp/progmodes/python.el (python-rx-constituents): Add coding-cookie. (python-shell--save-temp-file): Write file with proper encoding. (python-shell-buffer-substring): Add coding cookie for detected encoding to generated content. Fix blank lines when removing if-name-main block. (python-shell-send-file): Handle file encoding. (python-info-encoding-from-cookie) (python-info-encoding): New functions. * test/automated/python-tests.el (python-shell-buffer-substring-1) (python-shell-buffer-substring-2, python-shell-buffer-substring-3) (python-shell-buffer-substring-4, python-shell-buffer-substring-5) (python-shell-buffer-substring-6, python-shell-buffer-substring-7) (python-shell-buffer-substring-8) (python-info-encoding-from-cookie-1) (python-info-encoding-from-cookie-2) (python-info-encoding-from-cookie-3) (python-info-encoding-from-cookie-4) (python-info-encoding-from-cookie-5) (python-info-encoding-from-cookie-6) (python-info-encoding-from-cookie-7, python-info-encoding-1) (python-info-encoding-2): New tests.
Diffstat (limited to 'test/automated/python-tests.el')
-rw-r--r--test/automated/python-tests.el271
1 files changed, 271 insertions, 0 deletions
diff --git a/test/automated/python-tests.el b/test/automated/python-tests.el
index d1713ac1851..8fcda58e1e0 100644
--- a/test/automated/python-tests.el
+++ b/test/automated/python-tests.el
@@ -2459,6 +2459,198 @@ and `python-shell-interpreter-args' in the new shell buffer."
2459 "^\\(o\\.t \\|\\)"))) 2459 "^\\(o\\.t \\|\\)")))
2460 (ignore-errors (delete-file startup-file))))) 2460 (ignore-errors (delete-file startup-file)))))
2461 2461
2462(ert-deftest python-shell-buffer-substring-1 ()
2463 "Selecting a substring of the whole buffer must match its contents."
2464 (python-tests-with-temp-buffer
2465 "
2466class Foo(models.Model):
2467 pass
2468
2469
2470class Bar(models.Model):
2471 pass
2472"
2473 (should (string= (buffer-string)
2474 (python-shell-buffer-substring (point-min) (point-max))))))
2475
2476(ert-deftest python-shell-buffer-substring-2 ()
2477 "Main block should be removed if NOMAIN is non-nil."
2478 (python-tests-with-temp-buffer
2479 "
2480class Foo(models.Model):
2481 pass
2482
2483class Bar(models.Model):
2484 pass
2485
2486if __name__ == \"__main__\":
2487 foo = Foo()
2488 print (foo)
2489"
2490 (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
2491 "
2492class Foo(models.Model):
2493 pass
2494
2495class Bar(models.Model):
2496 pass
2497
2498
2499
2500
2501"))))
2502
2503(ert-deftest python-shell-buffer-substring-3 ()
2504 "Main block should be removed if NOMAIN is non-nil."
2505 (python-tests-with-temp-buffer
2506 "
2507class Foo(models.Model):
2508 pass
2509
2510if __name__ == \"__main__\":
2511 foo = Foo()
2512 print (foo)
2513
2514class Bar(models.Model):
2515 pass
2516"
2517 (should (string= (python-shell-buffer-substring (point-min) (point-max) t)
2518 "
2519class Foo(models.Model):
2520 pass
2521
2522
2523
2524
2525
2526class Bar(models.Model):
2527 pass
2528"))))
2529
2530(ert-deftest python-shell-buffer-substring-4 ()
2531 "Coding cookie should be added for substrings."
2532 (python-tests-with-temp-buffer
2533 "# coding: latin-1
2534
2535class Foo(models.Model):
2536 pass
2537
2538if __name__ == \"__main__\":
2539 foo = Foo()
2540 print (foo)
2541
2542class Bar(models.Model):
2543 pass
2544"
2545 (should (string= (python-shell-buffer-substring
2546 (python-tests-look-at "class Foo(models.Model):")
2547 (progn (python-nav-forward-sexp) (point)))
2548 "# -*- coding: latin-1 -*-
2549
2550class Foo(models.Model):
2551 pass"))))
2552
2553(ert-deftest python-shell-buffer-substring-5 ()
2554 "The proper amount of blank lines is added for a substring."
2555 (python-tests-with-temp-buffer
2556 "# coding: latin-1
2557
2558class Foo(models.Model):
2559 pass
2560
2561if __name__ == \"__main__\":
2562 foo = Foo()
2563 print (foo)
2564
2565class Bar(models.Model):
2566 pass
2567"
2568 (should (string= (python-shell-buffer-substring
2569 (python-tests-look-at "class Bar(models.Model):")
2570 (progn (python-nav-forward-sexp) (point)))
2571 "# -*- coding: latin-1 -*-
2572
2573
2574
2575
2576
2577
2578
2579
2580class Bar(models.Model):
2581 pass"))))
2582
2583(ert-deftest python-shell-buffer-substring-6 ()
2584 "Handle substring with coding cookie in the second line."
2585 (python-tests-with-temp-buffer
2586 "
2587# coding: latin-1
2588
2589class Foo(models.Model):
2590 pass
2591
2592if __name__ == \"__main__\":
2593 foo = Foo()
2594 print (foo)
2595
2596class Bar(models.Model):
2597 pass
2598"
2599 (should (string= (python-shell-buffer-substring
2600 (python-tests-look-at "# coding: latin-1")
2601 (python-tests-look-at "if __name__ == \"__main__\":"))
2602 "# -*- coding: latin-1 -*-
2603
2604
2605class Foo(models.Model):
2606 pass
2607
2608"))))
2609
2610(ert-deftest python-shell-buffer-substring-7 ()
2611 "Ensure first coding cookie gets precedence."
2612 (python-tests-with-temp-buffer
2613 "# coding: utf-8
2614# coding: latin-1
2615
2616class Foo(models.Model):
2617 pass
2618
2619if __name__ == \"__main__\":
2620 foo = Foo()
2621 print (foo)
2622
2623class Bar(models.Model):
2624 pass
2625"
2626 (should (string= (python-shell-buffer-substring
2627 (python-tests-look-at "# coding: latin-1")
2628 (python-tests-look-at "if __name__ == \"__main__\":"))
2629 "# -*- coding: utf-8 -*-
2630
2631
2632class Foo(models.Model):
2633 pass
2634
2635"))))
2636
2637(ert-deftest python-shell-buffer-substring-8 ()
2638 "Ensure first coding cookie gets precedence when sending whole buffer."
2639 (python-tests-with-temp-buffer
2640 "# coding: utf-8
2641# coding: latin-1
2642
2643class Foo(models.Model):
2644 pass
2645"
2646 (should (string= (python-shell-buffer-substring (point-min) (point-max))
2647 "# coding: utf-8
2648
2649
2650class Foo(models.Model):
2651 pass
2652"))))
2653
2462 2654
2463;;; Shell completion 2655;;; Shell completion
2464 2656
@@ -3773,6 +3965,85 @@ foo = True # another comment
3773 (forward-line 1) 3965 (forward-line 1)
3774 (should (python-info-current-line-empty-p)))) 3966 (should (python-info-current-line-empty-p))))
3775 3967
3968(ert-deftest python-info-encoding-from-cookie-1 ()
3969 "Should detect it on first line."
3970 (python-tests-with-temp-buffer
3971 "# coding=latin-1
3972
3973foo = True # another comment
3974"
3975 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
3976
3977(ert-deftest python-info-encoding-from-cookie-2 ()
3978 "Should detect it on second line."
3979 (python-tests-with-temp-buffer
3980 "
3981# coding=latin-1
3982
3983foo = True # another comment
3984"
3985 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
3986
3987(ert-deftest python-info-encoding-from-cookie-3 ()
3988 "Should not be detected on third line (and following ones)."
3989 (python-tests-with-temp-buffer
3990 "
3991
3992# coding=latin-1
3993foo = True # another comment
3994"
3995 (should (not (python-info-encoding-from-cookie)))))
3996
3997(ert-deftest python-info-encoding-from-cookie-4 ()
3998 "Should detect Emacs style."
3999 (python-tests-with-temp-buffer
4000 "# -*- coding: latin-1 -*-
4001
4002foo = True # another comment"
4003 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4004
4005(ert-deftest python-info-encoding-from-cookie-5 ()
4006 "Should detect Vim style."
4007 (python-tests-with-temp-buffer
4008 "# vim: set fileencoding=latin-1 :
4009
4010foo = True # another comment"
4011 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4012
4013(ert-deftest python-info-encoding-from-cookie-6 ()
4014 "First cookie wins."
4015 (python-tests-with-temp-buffer
4016 "# -*- coding: iso-8859-1 -*-
4017# vim: set fileencoding=latin-1 :
4018
4019foo = True # another comment"
4020 (should (eq (python-info-encoding-from-cookie) 'iso-8859-1))))
4021
4022(ert-deftest python-info-encoding-from-cookie-7 ()
4023 "First cookie wins."
4024 (python-tests-with-temp-buffer
4025 "# vim: set fileencoding=latin-1 :
4026# -*- coding: iso-8859-1 -*-
4027
4028foo = True # another comment"
4029 (should (eq (python-info-encoding-from-cookie) 'latin-1))))
4030
4031(ert-deftest python-info-encoding-1 ()
4032 "Should return the detected encoding from cookie."
4033 (python-tests-with-temp-buffer
4034 "# vim: set fileencoding=latin-1 :
4035
4036foo = True # another comment"
4037 (should (eq (python-info-encoding) 'latin-1))))
4038
4039(ert-deftest python-info-encoding-2 ()
4040 "Should default to utf-8."
4041 (python-tests-with-temp-buffer
4042 "# No encoding for you
4043
4044foo = True # another comment"
4045 (should (eq (python-info-encoding) 'utf-8))))
4046
3776 4047
3777;;; Utility functions 4048;;; Utility functions
3778 4049