5
5
Otherwise, tests are mostly independent.
6
6
Currently only test grep_it, coverage 51%.
7
7
"""
8
- from idlelib . grep import GrepDialog
8
+ from idlelib import grep
9
9
import unittest
10
10
from test .support import captured_stdout
11
11
from idlelib .idle_test .mock_tk import Var
12
+ import os
12
13
import re
13
14
14
15
@@ -26,23 +27,92 @@ def getpat(self):
26
27
class Dummy_grep :
27
28
# Methods tested
28
29
#default_command = GrepDialog.default_command
29
- grep_it = GrepDialog .grep_it
30
- findfiles = GrepDialog .findfiles
30
+ grep_it = grep .GrepDialog .grep_it
31
31
# Other stuff needed
32
32
recvar = Var (False )
33
33
engine = searchengine
34
34
def close (self ): # gui method
35
35
pass
36
36
37
- grep = Dummy_grep ()
37
+ _grep = Dummy_grep ()
38
38
39
39
40
40
class FindfilesTest (unittest .TestCase ):
41
- # findfiles is really a function, not a method, could be iterator
42
- # test that filename return filename
43
- # test that idlelib has many .py files
44
- # test that recursive flag adds idle_test .py files
45
- pass
41
+
42
+ @classmethod
43
+ def setUpClass (cls ):
44
+ cls .realpath = os .path .realpath (__file__ )
45
+ cls .path = os .path .dirname (cls .realpath )
46
+
47
+ @classmethod
48
+ def tearDownClass (cls ):
49
+ del cls .realpath , cls .path
50
+
51
+ def test_invaliddir (self ):
52
+ with captured_stdout () as s :
53
+ filelist = list (grep .findfiles ('invaliddir' , '*.*' , False ))
54
+ self .assertEqual (filelist , [])
55
+ self .assertIn ('invalid' , s .getvalue ())
56
+
57
+ def test_curdir (self ):
58
+ # Test os.curdir.
59
+ ff = grep .findfiles
60
+ save_cwd = os .getcwd ()
61
+ os .chdir (self .path )
62
+ filename = 'test_grep.py'
63
+ filelist = list (ff (os .curdir , filename , False ))
64
+ self .assertIn (os .path .join (os .curdir , filename ), filelist )
65
+ os .chdir (save_cwd )
66
+
67
+ def test_base (self ):
68
+ ff = grep .findfiles
69
+ readme = os .path .join (self .path , 'README.txt' )
70
+
71
+ # Check for Python files in path where this file lives.
72
+ filelist = list (ff (self .path , '*.py' , False ))
73
+ # This directory has many Python files.
74
+ self .assertGreater (len (filelist ), 10 )
75
+ self .assertIn (self .realpath , filelist )
76
+ self .assertNotIn (readme , filelist )
77
+
78
+ # Look for .txt files in path where this file lives.
79
+ filelist = list (ff (self .path , '*.txt' , False ))
80
+ self .assertNotEqual (len (filelist ), 0 )
81
+ self .assertNotIn (self .realpath , filelist )
82
+ self .assertIn (readme , filelist )
83
+
84
+ # Look for non-matching pattern.
85
+ filelist = list (ff (self .path , 'grep.*' , False ))
86
+ self .assertEqual (len (filelist ), 0 )
87
+ self .assertNotIn (self .realpath , filelist )
88
+
89
+ def test_recurse (self ):
90
+ ff = grep .findfiles
91
+ parent = os .path .dirname (self .path )
92
+ grepfile = os .path .join (parent , 'grep.py' )
93
+ pat = '*.py'
94
+
95
+ # Get Python files only in parent directory.
96
+ filelist = list (ff (parent , pat , False ))
97
+ parent_size = len (filelist )
98
+ # Lots of Python files in idlelib.
99
+ self .assertGreater (parent_size , 20 )
100
+ self .assertIn (grepfile , filelist )
101
+ # Without subdirectories, this file isn't returned.
102
+ self .assertNotIn (self .realpath , filelist )
103
+
104
+ # Include subdirectories.
105
+ filelist = list (ff (parent , pat , True ))
106
+ # More files found now.
107
+ self .assertGreater (len (filelist ), parent_size )
108
+ self .assertIn (grepfile , filelist )
109
+ # This file exists in list now.
110
+ self .assertIn (self .realpath , filelist )
111
+
112
+ # Check another level up the tree.
113
+ parent = os .path .dirname (parent )
114
+ filelist = list (ff (parent , '*.py' , True ))
115
+ self .assertIn (self .realpath , filelist )
46
116
47
117
48
118
class Grep_itTest (unittest .TestCase ):
@@ -51,9 +121,9 @@ class Grep_itTest(unittest.TestCase):
51
121
# from incomplete replacement, so 'later'.
52
122
53
123
def report (self , pat ):
54
- grep .engine ._pat = pat
124
+ _grep .engine ._pat = pat
55
125
with captured_stdout () as s :
56
- grep .grep_it (re .compile (pat ), __file__ )
126
+ _grep .grep_it (re .compile (pat ), __file__ )
57
127
lines = s .getvalue ().split ('\n ' )
58
128
lines .pop () # remove bogus '' after last \n
59
129
return lines
0 commit comments