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

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

#countcontent
1n/a"""Unittest for idlelib.replace.py"""
2n/afrom test.support import requires
3n/arequires('gui')
4n/a
5n/aimport unittest
6n/afrom unittest.mock import Mock
7n/afrom tkinter import Tk, Text
8n/afrom idlelib.idle_test.mock_tk import Mbox
9n/aimport idlelib.searchengine as se
10n/afrom idlelib.replace import ReplaceDialog
11n/a
12n/aorig_mbox = se.tkMessageBox
13n/ashowerror = Mbox.showerror
14n/a
15n/a
16n/aclass ReplaceDialogTest(unittest.TestCase):
17n/a
18n/a @classmethod
19n/a def setUpClass(cls):
20n/a cls.root = Tk()
21n/a cls.root.withdraw()
22n/a se.tkMessageBox = Mbox
23n/a cls.engine = se.SearchEngine(cls.root)
24n/a cls.dialog = ReplaceDialog(cls.root, cls.engine)
25n/a cls.dialog.bell = lambda: None
26n/a cls.dialog.ok = Mock()
27n/a cls.text = Text(cls.root)
28n/a cls.text.undo_block_start = Mock()
29n/a cls.text.undo_block_stop = Mock()
30n/a cls.dialog.text = cls.text
31n/a
32n/a @classmethod
33n/a def tearDownClass(cls):
34n/a se.tkMessageBox = orig_mbox
35n/a del cls.text, cls.dialog, cls.engine
36n/a cls.root.destroy()
37n/a del cls.root
38n/a
39n/a def setUp(self):
40n/a self.text.insert('insert', 'This is a sample sTring')
41n/a
42n/a def tearDown(self):
43n/a self.engine.patvar.set('')
44n/a self.dialog.replvar.set('')
45n/a self.engine.wordvar.set(False)
46n/a self.engine.casevar.set(False)
47n/a self.engine.revar.set(False)
48n/a self.engine.wrapvar.set(True)
49n/a self.engine.backvar.set(False)
50n/a showerror.title = ''
51n/a showerror.message = ''
52n/a self.text.delete('1.0', 'end')
53n/a
54n/a def test_replace_simple(self):
55n/a # Test replace function with all options at default setting.
56n/a # Wrap around - True
57n/a # Regular Expression - False
58n/a # Match case - False
59n/a # Match word - False
60n/a # Direction - Forwards
61n/a text = self.text
62n/a equal = self.assertEqual
63n/a pv = self.engine.patvar
64n/a rv = self.dialog.replvar
65n/a replace = self.dialog.replace_it
66n/a
67n/a # test accessor method
68n/a self.engine.setpat('asdf')
69n/a equal(self.engine.getpat(), pv.get())
70n/a
71n/a # text found and replaced
72n/a pv.set('a')
73n/a rv.set('asdf')
74n/a replace()
75n/a equal(text.get('1.8', '1.12'), 'asdf')
76n/a
77n/a # dont "match word" case
78n/a text.mark_set('insert', '1.0')
79n/a pv.set('is')
80n/a rv.set('hello')
81n/a replace()
82n/a equal(text.get('1.2', '1.7'), 'hello')
83n/a
84n/a # dont "match case" case
85n/a pv.set('string')
86n/a rv.set('world')
87n/a replace()
88n/a equal(text.get('1.23', '1.28'), 'world')
89n/a
90n/a # without "regular expression" case
91n/a text.mark_set('insert', 'end')
92n/a text.insert('insert', '\nline42:')
93n/a before_text = text.get('1.0', 'end')
94n/a pv.set(r'[a-z][\d]+')
95n/a replace()
96n/a after_text = text.get('1.0', 'end')
97n/a equal(before_text, after_text)
98n/a
99n/a # test with wrap around selected and complete a cycle
100n/a text.mark_set('insert', '1.9')
101n/a pv.set('i')
102n/a rv.set('j')
103n/a replace()
104n/a equal(text.get('1.8'), 'i')
105n/a equal(text.get('2.1'), 'j')
106n/a replace()
107n/a equal(text.get('2.1'), 'j')
108n/a equal(text.get('1.8'), 'j')
109n/a before_text = text.get('1.0', 'end')
110n/a replace()
111n/a after_text = text.get('1.0', 'end')
112n/a equal(before_text, after_text)
113n/a
114n/a # text not found
115n/a before_text = text.get('1.0', 'end')
116n/a pv.set('foobar')
117n/a replace()
118n/a after_text = text.get('1.0', 'end')
119n/a equal(before_text, after_text)
120n/a
121n/a # test access method
122n/a self.dialog.find_it(0)
123n/a
124n/a def test_replace_wrap_around(self):
125n/a text = self.text
126n/a equal = self.assertEqual
127n/a pv = self.engine.patvar
128n/a rv = self.dialog.replvar
129n/a replace = self.dialog.replace_it
130n/a self.engine.wrapvar.set(False)
131n/a
132n/a # replace candidate found both after and before 'insert'
133n/a text.mark_set('insert', '1.4')
134n/a pv.set('i')
135n/a rv.set('j')
136n/a replace()
137n/a equal(text.get('1.2'), 'i')
138n/a equal(text.get('1.5'), 'j')
139n/a replace()
140n/a equal(text.get('1.2'), 'i')
141n/a equal(text.get('1.20'), 'j')
142n/a replace()
143n/a equal(text.get('1.2'), 'i')
144n/a
145n/a # replace candidate found only before 'insert'
146n/a text.mark_set('insert', '1.8')
147n/a pv.set('is')
148n/a before_text = text.get('1.0', 'end')
149n/a replace()
150n/a after_text = text.get('1.0', 'end')
151n/a equal(before_text, after_text)
152n/a
153n/a def test_replace_whole_word(self):
154n/a text = self.text
155n/a equal = self.assertEqual
156n/a pv = self.engine.patvar
157n/a rv = self.dialog.replvar
158n/a replace = self.dialog.replace_it
159n/a self.engine.wordvar.set(True)
160n/a
161n/a pv.set('is')
162n/a rv.set('hello')
163n/a replace()
164n/a equal(text.get('1.0', '1.4'), 'This')
165n/a equal(text.get('1.5', '1.10'), 'hello')
166n/a
167n/a def test_replace_match_case(self):
168n/a equal = self.assertEqual
169n/a text = self.text
170n/a pv = self.engine.patvar
171n/a rv = self.dialog.replvar
172n/a replace = self.dialog.replace_it
173n/a self.engine.casevar.set(True)
174n/a
175n/a before_text = self.text.get('1.0', 'end')
176n/a pv.set('this')
177n/a rv.set('that')
178n/a replace()
179n/a after_text = self.text.get('1.0', 'end')
180n/a equal(before_text, after_text)
181n/a
182n/a pv.set('This')
183n/a replace()
184n/a equal(text.get('1.0', '1.4'), 'that')
185n/a
186n/a def test_replace_regex(self):
187n/a equal = self.assertEqual
188n/a text = self.text
189n/a pv = self.engine.patvar
190n/a rv = self.dialog.replvar
191n/a replace = self.dialog.replace_it
192n/a self.engine.revar.set(True)
193n/a
194n/a before_text = text.get('1.0', 'end')
195n/a pv.set(r'[a-z][\d]+')
196n/a rv.set('hello')
197n/a replace()
198n/a after_text = text.get('1.0', 'end')
199n/a equal(before_text, after_text)
200n/a
201n/a text.insert('insert', '\nline42')
202n/a replace()
203n/a equal(text.get('2.0', '2.8'), 'linhello')
204n/a
205n/a pv.set('')
206n/a replace()
207n/a self.assertIn('error', showerror.title)
208n/a self.assertIn('Empty', showerror.message)
209n/a
210n/a pv.set(r'[\d')
211n/a replace()
212n/a self.assertIn('error', showerror.title)
213n/a self.assertIn('Pattern', showerror.message)
214n/a
215n/a showerror.title = ''
216n/a showerror.message = ''
217n/a pv.set('[a]')
218n/a rv.set('test\\')
219n/a replace()
220n/a self.assertIn('error', showerror.title)
221n/a self.assertIn('Invalid Replace Expression', showerror.message)
222n/a
223n/a # test access method
224n/a self.engine.setcookedpat("\'")
225n/a equal(pv.get(), "\\'")
226n/a
227n/a def test_replace_backwards(self):
228n/a equal = self.assertEqual
229n/a text = self.text
230n/a pv = self.engine.patvar
231n/a rv = self.dialog.replvar
232n/a replace = self.dialog.replace_it
233n/a self.engine.backvar.set(True)
234n/a
235n/a text.insert('insert', '\nis as ')
236n/a
237n/a pv.set('is')
238n/a rv.set('was')
239n/a replace()
240n/a equal(text.get('1.2', '1.4'), 'is')
241n/a equal(text.get('2.0', '2.3'), 'was')
242n/a replace()
243n/a equal(text.get('1.5', '1.8'), 'was')
244n/a replace()
245n/a equal(text.get('1.2', '1.5'), 'was')
246n/a
247n/a def test_replace_all(self):
248n/a text = self.text
249n/a pv = self.engine.patvar
250n/a rv = self.dialog.replvar
251n/a replace_all = self.dialog.replace_all
252n/a
253n/a text.insert('insert', '\n')
254n/a text.insert('insert', text.get('1.0', 'end')*100)
255n/a pv.set('is')
256n/a rv.set('was')
257n/a replace_all()
258n/a self.assertNotIn('is', text.get('1.0', 'end'))
259n/a
260n/a self.engine.revar.set(True)
261n/a pv.set('')
262n/a replace_all()
263n/a self.assertIn('error', showerror.title)
264n/a self.assertIn('Empty', showerror.message)
265n/a
266n/a pv.set('[s][T]')
267n/a rv.set('\\')
268n/a replace_all()
269n/a
270n/a self.engine.revar.set(False)
271n/a pv.set('text which is not present')
272n/a rv.set('foobar')
273n/a replace_all()
274n/a
275n/a def test_default_command(self):
276n/a text = self.text
277n/a pv = self.engine.patvar
278n/a rv = self.dialog.replvar
279n/a replace_find = self.dialog.default_command
280n/a equal = self.assertEqual
281n/a
282n/a pv.set('This')
283n/a rv.set('was')
284n/a replace_find()
285n/a equal(text.get('sel.first', 'sel.last'), 'was')
286n/a
287n/a self.engine.revar.set(True)
288n/a pv.set('')
289n/a replace_find()
290n/a
291n/a
292n/aif __name__ == '__main__':
293n/a unittest.main(verbosity=2)