diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2014-03-11 07:27:59 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2014-03-11 07:27:59 -0400 |
commit | 5898eeefc6c9885782ede16b65fc04086aed2fcd (patch) | |
tree | 0a0675b7321fc9fca0a793b8bb49211004cb43a2 /tests | |
parent | 8cc4ffa19dd13d8433878aab3458f6c7572e74b5 (diff) | |
download | python-coveragepy-git-5898eeefc6c9885782ede16b65fc04086aed2fcd.tar.gz |
Cleaner tests of the file matchers
Diffstat (limited to 'tests')
-rw-r--r-- | tests/coveragetest.py | 12 | ||||
-rw-r--r-- | tests/test_files.py | 54 |
2 files changed, 38 insertions, 28 deletions
diff --git a/tests/coveragetest.py b/tests/coveragetest.py index 0467d808..a309f17d 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -167,13 +167,15 @@ class CoverageTest(TestCase): return self.captured_stderr.getvalue() def make_file(self, filename, text="", newline=None): - """Create a temp file. + """Create a file for testing. - `filename` is the path to the file, including directories if desired, - and `text` is the content. If `newline` is provided, it is a string - that will be used as the line endings in the created file. + `filename` is the relative path to the file, including directories if + desired, which will be created if need be. `text` is the content to + create in the file. If `newline` is provided, it is a string that will + be used as the line endings in the created file, otherwise the line + endings are as provided in `text`. - Returns the path to the file. + Returns `filename`. """ # Tests that call `make_file` should be run in a temp environment. diff --git a/tests/test_files.py b/tests/test_files.py index 85c0ac7b..42457a5c 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -50,41 +50,49 @@ class FileLocatorTest(CoverageTest): class MatcherTest(CoverageTest): """Tests of file matchers.""" + def setUp(self): + super(MatcherTest, self).setUp() + self.fl = FileLocator() + + def assertMatches(self, matcher, filepath, matches): + """The `matcher` should agree with `matches` about `filepath`.""" + canonical = self.fl.canonical_filename(filepath) + self.assertEqual( + matcher.match(canonical), matches, + "File %s should have matched as %s" % (filepath, matches) + ) + def test_tree_matcher(self): - file1 = self.make_file("sub/file1.py") - file2 = self.make_file("sub/file2.c") - file3 = self.make_file("sub2/file3.h") - file4 = self.make_file("sub3/file4.py") - file5 = self.make_file("sub3/file5.c") + matches_to_try = [ + (self.make_file("sub/file1.py"), True), + (self.make_file("sub/file2.c"), True), + (self.make_file("sub2/file3.h"), False), + (self.make_file("sub3/file4.py"), True), + (self.make_file("sub3/file5.c"), False), + ] fl = FileLocator() trees = [ fl.canonical_filename("sub"), - fl.canonical_filename(file4), + fl.canonical_filename("sub3/file4.py"), ] tm = TreeMatcher(trees) - self.assertTrue(tm.match(fl.canonical_filename(file1))) - self.assertTrue(tm.match(fl.canonical_filename(file2))) - self.assertFalse(tm.match(fl.canonical_filename(file3))) - self.assertTrue(tm.match(fl.canonical_filename(file4))) - self.assertFalse(tm.match(fl.canonical_filename(file5))) - self.assertEqual(tm.info(), trees) + for filepath, matches in matches_to_try: + self.assertMatches(tm, filepath, matches) def test_fnmatch_matcher(self): - file1 = self.make_file("sub/file1.py") - file2 = self.make_file("sub/file2.c") - file3 = self.make_file("sub2/file3.h") - file4 = self.make_file("sub3/file4.py") - file5 = self.make_file("sub3/file5.c") + matches_to_try = [ + (self.make_file("sub/file1.py"), True), + (self.make_file("sub/file2.c"), False), + (self.make_file("sub2/file3.h"), True), + (self.make_file("sub3/file4.py"), True), + (self.make_file("sub3/file5.c"), False), + ] fl = FileLocator() fnm = FnmatchMatcher(["*.py", "*/sub2/*"]) - self.assertTrue(fnm.match(fl.canonical_filename(file1))) - self.assertFalse(fnm.match(fl.canonical_filename(file2))) - self.assertTrue(fnm.match(fl.canonical_filename(file3))) - self.assertTrue(fnm.match(fl.canonical_filename(file4))) - self.assertFalse(fnm.match(fl.canonical_filename(file5))) - self.assertEqual(fnm.info(), ["*.py", "*/sub2/*"]) + for filepath, matches in matches_to_try: + self.assertMatches(fnm, filepath, matches) class PathAliasesTest(CoverageTest): |