| 1 |
""" !Changing this line will break Test_findfile.test_found! |
| 2 |
Non-gui unit tests for idlelib.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. |
| 7 |
""" |
| 8 |
import unittest |
| 9 |
from test.test_support import captured_stdout, findfile |
| 10 |
from idlelib.idle_test.mock_tk import Var |
| 11 |
from idlelib.GrepDialog import GrepDialog |
| 12 |
import re |
| 13 |
|
| 14 |
__file__ = findfile('idlelib/idle_test') + '/test_grep.py' |
| 15 |
|
| 16 |
class Dummy_searchengine: |
| 17 |
'''GrepDialog.__init__ calls parent SearchDiabolBase which attaches the |
| 18 |
passed in SearchEngine instance as attribute 'engine'. Only a few of the |
| 19 |
many possible self.engine.x attributes are needed here. |
| 20 |
''' |
| 21 |
def getpat(self): |
| 22 |
return self._pat |
| 23 |
|
| 24 |
searchengine = Dummy_searchengine() |
| 25 |
|
| 26 |
class Dummy_grep: |
| 27 |
|
| 28 |
|
| 29 |
grep_it = GrepDialog.grep_it.im_func |
| 30 |
findfiles = GrepDialog.findfiles.im_func |
| 31 |
|
| 32 |
recvar = Var(False) |
| 33 |
engine = searchengine |
| 34 |
def close(self): |
| 35 |
pass |
| 36 |
|
| 37 |
grep = Dummy_grep() |
| 38 |
|
| 39 |
class FindfilesTest(unittest.TestCase): |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
pass |
| 45 |
|
| 46 |
class Grep_itTest(unittest.TestCase): |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
def report(self, pat): |
| 52 |
grep.engine._pat = pat |
| 53 |
with captured_stdout() as s: |
| 54 |
grep.grep_it(re.compile(pat), __file__) |
| 55 |
lines = s.getvalue().split('\n') |
| 56 |
lines.pop() |
| 57 |
return lines |
| 58 |
|
| 59 |
def test_unfound(self): |
| 60 |
pat = 'xyz*'*7 |
| 61 |
lines = self.report(pat) |
| 62 |
self.assertEqual(len(lines), 2) |
| 63 |
self.assertIn(pat, lines[0]) |
| 64 |
self.assertEqual(lines[1], 'No hits.') |
| 65 |
|
| 66 |
def test_found(self): |
| 67 |
|
| 68 |
pat = '""" !Changing this line will break Test_findfile.test_found!' |
| 69 |
lines = self.report(pat) |
| 70 |
self.assertEqual(len(lines), 5) |
| 71 |
self.assertIn(pat, lines[0]) |
| 72 |
self.assertIn('py: 1:', lines[1]) |
| 73 |
self.assertIn('2', lines[3]) |
| 74 |
self.assertTrue(lines[4].startswith('(Hint:')) |
| 75 |
|
| 76 |
class Default_commandTest(unittest.TestCase): |
| 77 |
|
| 78 |
|
| 79 |
pass |
| 80 |
|
| 81 |
if __name__ == '__main__': |
| 82 |
unittest.main(verbosity=2, exit=False) |