ยปCore Development>Code coverage>Lib/idlelib/idle_test/test_history.py

Python code coverage for Lib/idlelib/idle_test/test_history.py

#countcontent
1n/aimport unittest
2n/afrom test.support import requires
3n/a
4n/aimport tkinter as tk
5n/afrom tkinter import Text as tkText
6n/afrom idlelib.idle_test.mock_tk import Text as mkText
7n/afrom idlelib.history import History
8n/afrom idlelib.config import idleConf
9n/a
10n/aline1 = 'a = 7'
11n/aline2 = 'b = a'
12n/a
13n/aclass StoreTest(unittest.TestCase):
14n/a '''Tests History.__init__ and History.store with mock Text'''
15n/a
16n/a @classmethod
17n/a def setUpClass(cls):
18n/a cls.text = mkText()
19n/a cls.history = History(cls.text)
20n/a
21n/a def tearDown(self):
22n/a self.text.delete('1.0', 'end')
23n/a self.history.history = []
24n/a
25n/a def test_init(self):
26n/a self.assertIs(self.history.text, self.text)
27n/a self.assertEqual(self.history.history, [])
28n/a self.assertIsNone(self.history.prefix)
29n/a self.assertIsNone(self.history.pointer)
30n/a self.assertEqual(self.history.cyclic,
31n/a idleConf.GetOption("main", "History", "cyclic", 1, "bool"))
32n/a
33n/a def test_store_short(self):
34n/a self.history.store('a')
35n/a self.assertEqual(self.history.history, [])
36n/a self.history.store(' a ')
37n/a self.assertEqual(self.history.history, [])
38n/a
39n/a def test_store_dup(self):
40n/a self.history.store(line1)
41n/a self.assertEqual(self.history.history, [line1])
42n/a self.history.store(line2)
43n/a self.assertEqual(self.history.history, [line1, line2])
44n/a self.history.store(line1)
45n/a self.assertEqual(self.history.history, [line2, line1])
46n/a
47n/a def test_store_reset(self):
48n/a self.history.prefix = line1
49n/a self.history.pointer = 0
50n/a self.history.store(line2)
51n/a self.assertIsNone(self.history.prefix)
52n/a self.assertIsNone(self.history.pointer)
53n/a
54n/a
55n/aclass TextWrapper:
56n/a def __init__(self, master):
57n/a self.text = tkText(master=master)
58n/a self._bell = False
59n/a def __getattr__(self, name):
60n/a return getattr(self.text, name)
61n/a def bell(self):
62n/a self._bell = True
63n/a
64n/aclass FetchTest(unittest.TestCase):
65n/a '''Test History.fetch with wrapped tk.Text.
66n/a '''
67n/a @classmethod
68n/a def setUpClass(cls):
69n/a requires('gui')
70n/a cls.root = tk.Tk()
71n/a cls.root.withdraw()
72n/a
73n/a def setUp(self):
74n/a self.text = text = TextWrapper(self.root)
75n/a text.insert('1.0', ">>> ")
76n/a text.mark_set('iomark', '1.4')
77n/a text.mark_gravity('iomark', 'left')
78n/a self.history = History(text)
79n/a self.history.history = [line1, line2]
80n/a
81n/a @classmethod
82n/a def tearDownClass(cls):
83n/a cls.root.destroy()
84n/a del cls.root
85n/a
86n/a def fetch_test(self, reverse, line, prefix, index, *, bell=False):
87n/a # Perform one fetch as invoked by Alt-N or Alt-P
88n/a # Test the result. The line test is the most important.
89n/a # The last two are diagnostic of fetch internals.
90n/a History = self.history
91n/a History.fetch(reverse)
92n/a
93n/a Equal = self.assertEqual
94n/a Equal(self.text.get('iomark', 'end-1c'), line)
95n/a Equal(self.text._bell, bell)
96n/a if bell:
97n/a self.text._bell = False
98n/a Equal(History.prefix, prefix)
99n/a Equal(History.pointer, index)
100n/a Equal(self.text.compare("insert", '==', "end-1c"), 1)
101n/a
102n/a def test_fetch_prev_cyclic(self):
103n/a prefix = ''
104n/a test = self.fetch_test
105n/a test(True, line2, prefix, 1)
106n/a test(True, line1, prefix, 0)
107n/a test(True, prefix, None, None, bell=True)
108n/a
109n/a def test_fetch_next_cyclic(self):
110n/a prefix = ''
111n/a test = self.fetch_test
112n/a test(False, line1, prefix, 0)
113n/a test(False, line2, prefix, 1)
114n/a test(False, prefix, None, None, bell=True)
115n/a
116n/a # Prefix 'a' tests skip line2, which starts with 'b'
117n/a def test_fetch_prev_prefix(self):
118n/a prefix = 'a'
119n/a self.text.insert('iomark', prefix)
120n/a self.fetch_test(True, line1, prefix, 0)
121n/a self.fetch_test(True, prefix, None, None, bell=True)
122n/a
123n/a def test_fetch_next_prefix(self):
124n/a prefix = 'a'
125n/a self.text.insert('iomark', prefix)
126n/a self.fetch_test(False, line1, prefix, 0)
127n/a self.fetch_test(False, prefix, None, None, bell=True)
128n/a
129n/a def test_fetch_prev_noncyclic(self):
130n/a prefix = ''
131n/a self.history.cyclic = False
132n/a test = self.fetch_test
133n/a test(True, line2, prefix, 1)
134n/a test(True, line1, prefix, 0)
135n/a test(True, line1, prefix, 0, bell=True)
136n/a
137n/a def test_fetch_next_noncyclic(self):
138n/a prefix = ''
139n/a self.history.cyclic = False
140n/a test = self.fetch_test
141n/a test(False, prefix, None, None, bell=True)
142n/a test(True, line2, prefix, 1)
143n/a test(False, prefix, None, None, bell=True)
144n/a test(False, prefix, None, None, bell=True)
145n/a
146n/a def test_fetch_cursor_move(self):
147n/a # Move cursor after fetch
148n/a self.history.fetch(reverse=True) # initialization
149n/a self.text.mark_set('insert', 'iomark')
150n/a self.fetch_test(True, line2, None, None, bell=True)
151n/a
152n/a def test_fetch_edit(self):
153n/a # Edit after fetch
154n/a self.history.fetch(reverse=True) # initialization
155n/a self.text.delete('iomark', 'insert', )
156n/a self.text.insert('iomark', 'a =')
157n/a self.fetch_test(True, line1, 'a =', 0) # prefix is reset
158n/a
159n/a def test_history_prev_next(self):
160n/a # Minimally test functions bound to events
161n/a self.history.history_prev('dummy event')
162n/a self.assertEqual(self.history.pointer, 1)
163n/a self.history.history_next('dummy event')
164n/a self.assertEqual(self.history.pointer, None)
165n/a
166n/a
167n/aif __name__ == '__main__':
168n/a unittest.main(verbosity=2, exit=2)