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

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

#countcontent
1n/a""" !Changing this line will break Test_findfile.test_found!
2n/aNon-gui unit tests for grep.GrepDialog methods.
3n/adummy_command calls grep_it calls findfiles.
4n/aAn exception raised in one method will fail callers.
5n/aOtherwise, tests are mostly independent.
6n/a*** Currently only test grep_it.
7n/a"""
8n/aimport unittest
9n/afrom test.support import captured_stdout
10n/afrom idlelib.idle_test.mock_tk import Var
11n/afrom idlelib.grep import GrepDialog
12n/aimport re
13n/a
14n/aclass Dummy_searchengine:
15n/a '''GrepDialog.__init__ calls parent SearchDiabolBase which attaches the
16n/a passed in SearchEngine instance as attribute 'engine'. Only a few of the
17n/a many possible self.engine.x attributes are needed here.
18n/a '''
19n/a def getpat(self):
20n/a return self._pat
21n/a
22n/asearchengine = Dummy_searchengine()
23n/a
24n/aclass Dummy_grep:
25n/a # Methods tested
26n/a #default_command = GrepDialog.default_command
27n/a grep_it = GrepDialog.grep_it
28n/a findfiles = GrepDialog.findfiles
29n/a # Other stuff needed
30n/a recvar = Var(False)
31n/a engine = searchengine
32n/a def close(self): # gui method
33n/a pass
34n/a
35n/agrep = Dummy_grep()
36n/a
37n/aclass FindfilesTest(unittest.TestCase):
38n/a # findfiles is really a function, not a method, could be iterator
39n/a # test that filename return filename
40n/a # test that idlelib has many .py files
41n/a # test that recursive flag adds idle_test .py files
42n/a pass
43n/a
44n/aclass Grep_itTest(unittest.TestCase):
45n/a # Test captured reports with 0 and some hits.
46n/a # Should test file names, but Windows reports have mixed / and \ separators
47n/a # from incomplete replacement, so 'later'.
48n/a
49n/a def report(self, pat):
50n/a grep.engine._pat = pat
51n/a with captured_stdout() as s:
52n/a grep.grep_it(re.compile(pat), __file__)
53n/a lines = s.getvalue().split('\n')
54n/a lines.pop() # remove bogus '' after last \n
55n/a return lines
56n/a
57n/a def test_unfound(self):
58n/a pat = 'xyz*'*7
59n/a lines = self.report(pat)
60n/a self.assertEqual(len(lines), 2)
61n/a self.assertIn(pat, lines[0])
62n/a self.assertEqual(lines[1], 'No hits.')
63n/a
64n/a def test_found(self):
65n/a
66n/a pat = '""" !Changing this line will break Test_findfile.test_found!'
67n/a lines = self.report(pat)
68n/a self.assertEqual(len(lines), 5)
69n/a self.assertIn(pat, lines[0])
70n/a self.assertIn('py: 1:', lines[1]) # line number 1
71n/a self.assertIn('2', lines[3]) # hits found 2
72n/a self.assertTrue(lines[4].startswith('(Hint:'))
73n/a
74n/aclass Default_commandTest(unittest.TestCase):
75n/a # To write this, move outwin import to top of GrepDialog
76n/a # so it can be replaced by captured_stdout in class setup/teardown.
77n/a pass
78n/a
79n/aif __name__ == '__main__':
80n/a unittest.main(verbosity=2, exit=False)