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

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

#countcontent
1n/a''' Test mock_tk.Text class against tkinter.Text class
2n/a
3n/aRun same tests with both by creating a mixin class.
4n/a'''
5n/aimport unittest
6n/afrom test.support import requires
7n/afrom _tkinter import TclError
8n/a
9n/aclass TextTest(object):
10n/a "Define items common to both sets of tests."
11n/a
12n/a hw = 'hello\nworld' # Several tests insert this after after initialization.
13n/a hwn = hw+'\n' # \n present at initialization, before insert
14n/a
15n/a # setUpClass defines cls.Text and maybe cls.root.
16n/a # setUp defines self.text from Text and maybe root.
17n/a
18n/a def test_init(self):
19n/a self.assertEqual(self.text.get('1.0'), '\n')
20n/a self.assertEqual(self.text.get('end'), '')
21n/a
22n/a def test_index_empty(self):
23n/a index = self.text.index
24n/a
25n/a for dex in (-1.0, 0.3, '1.-1', '1.0', '1.0 lineend', '1.end', '1.33',
26n/a 'insert'):
27n/a self.assertEqual(index(dex), '1.0')
28n/a
29n/a for dex in 'end', 2.0, '2.1', '33.44':
30n/a self.assertEqual(index(dex), '2.0')
31n/a
32n/a def test_index_data(self):
33n/a index = self.text.index
34n/a self.text.insert('1.0', self.hw)
35n/a
36n/a for dex in -1.0, 0.3, '1.-1', '1.0':
37n/a self.assertEqual(index(dex), '1.0')
38n/a
39n/a for dex in '1.0 lineend', '1.end', '1.33':
40n/a self.assertEqual(index(dex), '1.5')
41n/a
42n/a for dex in 'end', '33.44':
43n/a self.assertEqual(index(dex), '3.0')
44n/a
45n/a def test_get(self):
46n/a get = self.text.get
47n/a Equal = self.assertEqual
48n/a self.text.insert('1.0', self.hw)
49n/a
50n/a Equal(get('end'), '')
51n/a Equal(get('end', 'end'), '')
52n/a Equal(get('1.0'), 'h')
53n/a Equal(get('1.0', '1.1'), 'h')
54n/a Equal(get('1.0', '1.3'), 'hel')
55n/a Equal(get('1.1', '1.3'), 'el')
56n/a Equal(get('1.0', '1.0 lineend'), 'hello')
57n/a Equal(get('1.0', '1.10'), 'hello')
58n/a Equal(get('1.0 lineend'), '\n')
59n/a Equal(get('1.1', '2.3'), 'ello\nwor')
60n/a Equal(get('1.0', '2.5'), self.hw)
61n/a Equal(get('1.0', 'end'), self.hwn)
62n/a Equal(get('0.0', '5.0'), self.hwn)
63n/a
64n/a def test_insert(self):
65n/a insert = self.text.insert
66n/a get = self.text.get
67n/a Equal = self.assertEqual
68n/a
69n/a insert('1.0', self.hw)
70n/a Equal(get('1.0', 'end'), self.hwn)
71n/a
72n/a insert('1.0', '') # nothing
73n/a Equal(get('1.0', 'end'), self.hwn)
74n/a
75n/a insert('1.0', '*')
76n/a Equal(get('1.0', 'end'), '*hello\nworld\n')
77n/a
78n/a insert('1.0 lineend', '*')
79n/a Equal(get('1.0', 'end'), '*hello*\nworld\n')
80n/a
81n/a insert('2.3', '*')
82n/a Equal(get('1.0', 'end'), '*hello*\nwor*ld\n')
83n/a
84n/a insert('end', 'x')
85n/a Equal(get('1.0', 'end'), '*hello*\nwor*ldx\n')
86n/a
87n/a insert('1.4', 'x\n')
88n/a Equal(get('1.0', 'end'), '*helx\nlo*\nwor*ldx\n')
89n/a
90n/a def test_no_delete(self):
91n/a # if index1 == 'insert' or 'end' or >= end, there is no deletion
92n/a delete = self.text.delete
93n/a get = self.text.get
94n/a Equal = self.assertEqual
95n/a self.text.insert('1.0', self.hw)
96n/a
97n/a delete('insert')
98n/a Equal(get('1.0', 'end'), self.hwn)
99n/a
100n/a delete('end')
101n/a Equal(get('1.0', 'end'), self.hwn)
102n/a
103n/a delete('insert', 'end')
104n/a Equal(get('1.0', 'end'), self.hwn)
105n/a
106n/a delete('insert', '5.5')
107n/a Equal(get('1.0', 'end'), self.hwn)
108n/a
109n/a delete('1.4', '1.0')
110n/a Equal(get('1.0', 'end'), self.hwn)
111n/a
112n/a delete('1.4', '1.4')
113n/a Equal(get('1.0', 'end'), self.hwn)
114n/a
115n/a def test_delete_char(self):
116n/a delete = self.text.delete
117n/a get = self.text.get
118n/a Equal = self.assertEqual
119n/a self.text.insert('1.0', self.hw)
120n/a
121n/a delete('1.0')
122n/a Equal(get('1.0', '1.end'), 'ello')
123n/a
124n/a delete('1.0', '1.1')
125n/a Equal(get('1.0', '1.end'), 'llo')
126n/a
127n/a # delete \n and combine 2 lines into 1
128n/a delete('1.end')
129n/a Equal(get('1.0', '1.end'), 'lloworld')
130n/a
131n/a self.text.insert('1.3', '\n')
132n/a delete('1.10')
133n/a Equal(get('1.0', '1.end'), 'lloworld')
134n/a
135n/a self.text.insert('1.3', '\n')
136n/a delete('1.3', '2.0')
137n/a Equal(get('1.0', '1.end'), 'lloworld')
138n/a
139n/a def test_delete_slice(self):
140n/a delete = self.text.delete
141n/a get = self.text.get
142n/a Equal = self.assertEqual
143n/a self.text.insert('1.0', self.hw)
144n/a
145n/a delete('1.0', '1.0 lineend')
146n/a Equal(get('1.0', 'end'), '\nworld\n')
147n/a
148n/a delete('1.0', 'end')
149n/a Equal(get('1.0', 'end'), '\n')
150n/a
151n/a self.text.insert('1.0', self.hw)
152n/a delete('1.0', '2.0')
153n/a Equal(get('1.0', 'end'), 'world\n')
154n/a
155n/a delete('1.0', 'end')
156n/a Equal(get('1.0', 'end'), '\n')
157n/a
158n/a self.text.insert('1.0', self.hw)
159n/a delete('1.2', '2.3')
160n/a Equal(get('1.0', 'end'), 'held\n')
161n/a
162n/a def test_multiple_lines(self): # insert and delete
163n/a self.text.insert('1.0', 'hello')
164n/a
165n/a self.text.insert('1.3', '1\n2\n3\n4\n5')
166n/a self.assertEqual(self.text.get('1.0', 'end'), 'hel1\n2\n3\n4\n5lo\n')
167n/a
168n/a self.text.delete('1.3', '5.1')
169n/a self.assertEqual(self.text.get('1.0', 'end'), 'hello\n')
170n/a
171n/a def test_compare(self):
172n/a compare = self.text.compare
173n/a Equal = self.assertEqual
174n/a # need data so indexes not squished to 1,0
175n/a self.text.insert('1.0', 'First\nSecond\nThird\n')
176n/a
177n/a self.assertRaises(TclError, compare, '2.2', 'op', '2.2')
178n/a
179n/a for op, less1, less0, equal, greater0, greater1 in (
180n/a ('<', True, True, False, False, False),
181n/a ('<=', True, True, True, False, False),
182n/a ('>', False, False, False, True, True),
183n/a ('>=', False, False, True, True, True),
184n/a ('==', False, False, True, False, False),
185n/a ('!=', True, True, False, True, True),
186n/a ):
187n/a Equal(compare('1.1', op, '2.2'), less1, op)
188n/a Equal(compare('2.1', op, '2.2'), less0, op)
189n/a Equal(compare('2.2', op, '2.2'), equal, op)
190n/a Equal(compare('2.3', op, '2.2'), greater0, op)
191n/a Equal(compare('3.3', op, '2.2'), greater1, op)
192n/a
193n/a
194n/aclass MockTextTest(TextTest, unittest.TestCase):
195n/a
196n/a @classmethod
197n/a def setUpClass(cls):
198n/a from idlelib.idle_test.mock_tk import Text
199n/a cls.Text = Text
200n/a
201n/a def setUp(self):
202n/a self.text = self.Text()
203n/a
204n/a
205n/a def test_decode(self):
206n/a # test endflags (-1, 0) not tested by test_index (which uses +1)
207n/a decode = self.text._decode
208n/a Equal = self.assertEqual
209n/a self.text.insert('1.0', self.hw)
210n/a
211n/a Equal(decode('end', -1), (2, 5))
212n/a Equal(decode('3.1', -1), (2, 5))
213n/a Equal(decode('end', 0), (2, 6))
214n/a Equal(decode('3.1', 0), (2, 6))
215n/a
216n/a
217n/aclass TkTextTest(TextTest, unittest.TestCase):
218n/a
219n/a @classmethod
220n/a def setUpClass(cls):
221n/a requires('gui')
222n/a from tkinter import Tk, Text
223n/a cls.Text = Text
224n/a cls.root = Tk()
225n/a
226n/a @classmethod
227n/a def tearDownClass(cls):
228n/a cls.root.destroy()
229n/a del cls.root
230n/a
231n/a def setUp(self):
232n/a self.text = self.Text(self.root)
233n/a
234n/a
235n/aif __name__ == '__main__':
236n/a unittest.main(verbosity=2, exit=False)