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