test_editmenu.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. '''Test (selected) IDLE Edit menu items.
  2. Edit modules have their own test files
  3. '''
  4. from test.support import requires
  5. requires('gui')
  6. import tkinter as tk
  7. from tkinter import ttk
  8. import unittest
  9. from idlelib import pyshell
  10. class PasteTest(unittest.TestCase):
  11. '''Test pasting into widgets that allow pasting.
  12. On X11, replacing selections requires tk fix.
  13. '''
  14. @classmethod
  15. def setUpClass(cls):
  16. cls.root = root = tk.Tk()
  17. cls.root.withdraw()
  18. pyshell.fix_x11_paste(root)
  19. cls.text = tk.Text(root)
  20. cls.entry = tk.Entry(root)
  21. cls.tentry = ttk.Entry(root)
  22. cls.spin = tk.Spinbox(root)
  23. root.clipboard_clear()
  24. root.clipboard_append('two')
  25. @classmethod
  26. def tearDownClass(cls):
  27. del cls.text, cls.entry, cls.tentry
  28. cls.root.clipboard_clear()
  29. cls.root.update_idletasks()
  30. cls.root.destroy()
  31. del cls.root
  32. def test_paste_text(self):
  33. "Test pasting into text with and without a selection."
  34. text = self.text
  35. for tag, ans in ('', 'onetwo\n'), ('sel', 'two\n'):
  36. with self.subTest(tag=tag, ans=ans):
  37. text.delete('1.0', 'end')
  38. text.insert('1.0', 'one', tag)
  39. text.event_generate('<<Paste>>')
  40. self.assertEqual(text.get('1.0', 'end'), ans)
  41. def test_paste_entry(self):
  42. "Test pasting into an entry with and without a selection."
  43. # Generated <<Paste>> fails for tk entry without empty select
  44. # range for 'no selection'. Live widget works fine.
  45. for entry in self.entry, self.tentry:
  46. for end, ans in (0, 'onetwo'), ('end', 'two'):
  47. with self.subTest(entry=entry, end=end, ans=ans):
  48. entry.delete(0, 'end')
  49. entry.insert(0, 'one')
  50. entry.select_range(0, end)
  51. entry.event_generate('<<Paste>>')
  52. self.assertEqual(entry.get(), ans)
  53. def test_paste_spin(self):
  54. "Test pasting into a spinbox with and without a selection."
  55. # See note above for entry.
  56. spin = self.spin
  57. for end, ans in (0, 'onetwo'), ('end', 'two'):
  58. with self.subTest(end=end, ans=ans):
  59. spin.delete(0, 'end')
  60. spin.insert(0, 'one')
  61. spin.selection('range', 0, end) # see note
  62. spin.event_generate('<<Paste>>')
  63. self.assertEqual(spin.get(), ans)
  64. if __name__ == '__main__':
  65. unittest.main(verbosity=2)