Debian Codegraph @

Lib/idlelib/idle_test/test_grep.py

raw
1 """ !Changing this line will break Test_findfile.test_found!
2 Non-gui unit tests for grep.GrepDialog methods.
3 dummy_command calls grep_it calls findfiles.
4 An exception raised in one method will fail callers.
5 Otherwise, tests are mostly independent.
6 Currently only test grep_it, coverage 51%.
7 """
8 from idlelib import grep
9 import unittest
10 from test.support import captured_stdout
11 from test.support.testcase import ExtraAssertions
12 from idlelib.idle_test.mock_tk import Var
13 import os
14 import re
15
16
17 class Dummy_searchengine:
18 '''GrepDialog.__init__ calls parent SearchDiabolBase which attaches the
19 passed in SearchEngine instance as attribute 'engine'. Only a few of the
20 many possible self.engine.x attributes are needed here.
21 '''
22 def getpat(self):
23 return self._pat
24
25 searchengine = Dummy_searchengine()
26
27
28 class Dummy_grep:
29 # Methods tested
30 #default_command = GrepDialog.default_command
31 grep_it = grep.GrepDialog.grep_it
32 # Other stuff needed
33 recvar = Var(False)
34 engine = searchengine
35 def close(self): # gui method
36 pass
37
38 _grep = Dummy_grep()
39
40
41 class FindfilesTest(unittest.TestCase):
42
43 @classmethod
44 def setUpClass(cls):
45 cls.realpath = os.path.realpath(__file__)
46 cls.path = os.path.dirname(cls.realpath)
47
48 @classmethod
49 def tearDownClass(cls):
50 del cls.realpath, cls.path
51
52 def test_invaliddir(self):
53 with captured_stdout() as s:
54 filelist = list(grep.findfiles('invaliddir', '*.*', False))
55 self.assertEqual(filelist, [])
56 self.assertIn('invalid', s.getvalue())
57
58 def test_curdir(self):
59 # Test os.curdir.
60 ff = grep.findfiles
61 save_cwd = os.getcwd()
62 os.chdir(self.path)
63 filename = 'test_grep.py'
64 filelist = list(ff(os.curdir, filename, False))
65 self.assertIn(os.path.join(os.curdir, filename), filelist)
66 os.chdir(save_cwd)
67
68 def test_base(self):
69 ff = grep.findfiles
70 readme = os.path.join(self.path, 'README.txt')
71
72 # Check for Python files in path where this file lives.
73 filelist = list(ff(self.path, '*.py', False))
74 # This directory has many Python files.
75 self.assertGreater(len(filelist), 10)
76 self.assertIn(self.realpath, filelist)
77 self.assertNotIn(readme, filelist)
78
79 # Look for .txt files in path where this file lives.
80 filelist = list(ff(self.path, '*.txt', False))
81 self.assertNotEqual(len(filelist), 0)
82 self.assertNotIn(self.realpath, filelist)
83 self.assertIn(readme, filelist)
84
85 # Look for non-matching pattern.
86 filelist = list(ff(self.path, 'grep.*', False))
87 self.assertEqual(len(filelist), 0)
88 self.assertNotIn(self.realpath, filelist)
89
90 def test_recurse(self):
91 ff = grep.findfiles
92 parent = os.path.dirname(self.path)
93 grepfile = os.path.join(parent, 'grep.py')
94 pat = '*.py'
95
96 # Get Python files only in parent directory.
97 filelist = list(ff(parent, pat, False))
98 parent_size = len(filelist)
99 # Lots of Python files in idlelib.
100 self.assertGreater(parent_size, 20)
101 self.assertIn(grepfile, filelist)
102 # Without subdirectories, this file isn't returned.
103 self.assertNotIn(self.realpath, filelist)
104
105 # Include subdirectories.
106 filelist = list(ff(parent, pat, True))
107 # More files found now.
108 self.assertGreater(len(filelist), parent_size)
109 self.assertIn(grepfile, filelist)
110 # This file exists in list now.
111 self.assertIn(self.realpath, filelist)
112
113 # Check another level up the tree.
114 parent = os.path.dirname(parent)
115 filelist = list(ff(parent, '*.py', True))
116 self.assertIn(self.realpath, filelist)
117
118
119 class Grep_itTest(unittest.TestCase, ExtraAssertions):
120 # Test captured reports with 0 and some hits.
121 # Should test file names, but Windows reports have mixed / and \ separators
122 # from incomplete replacement, so 'later'.
123
124 def report(self, pat):
125 _grep.engine._pat = pat
126 with captured_stdout() as s:
127 _grep.grep_it(re.compile(pat), __file__)
128 lines = s.getvalue().split('\n')
129 lines.pop() # remove bogus '' after last \n
130 return lines
131
132 def test_unfound(self):
133 pat = 'xyz*'*7
134 lines = self.report(pat)
135 self.assertEqual(len(lines), 2)
136 self.assertIn(pat, lines[0])
137 self.assertEqual(lines[1], 'No hits.')
138
139 def test_found(self):
140
141 pat = '""" !Changing this line will break Test_findfile.test_found!'
142 lines = self.report(pat)
143 self.assertEqual(len(lines), 5)
144 self.assertIn(pat, lines[0])
145 self.assertIn('py: 1:', lines[1]) # line number 1
146 self.assertIn('2', lines[3]) # hits found 2
147 self.assertStartsWith(lines[4], '(Hint:')
148
149
150 class Default_commandTest(unittest.TestCase):
151 # To write this, move outwin import to top of GrepDialog
152 # so it can be replaced by captured_stdout in class setup/teardown.
153 pass
154
155
156 if __name__ == '__main__':
157 unittest.main(verbosity=2)