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